0.1.31 • Published 3 months ago

modelok v0.1.31

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

modelok

Pronounced 'model okay' is a simple JavaScript object validation library.

The primary goal is to be lightweight and super easy to use in JavaScript environments regardless of your framework of choice (React, Vue, Angular etc).

Let's say Jon Doe is filling out some info on your app but has omitted his country which is required. Thankfully you use modelok to ensure the data entered is valid before posting it to your backend.

how to use

  1. First install using npm i modelok and import into your project.

    import validate from 'modelok'
  2. Instantiate with your data model and chain your desired validation rules.

    const model = { 
        firstName: 'Jon', 
        surname: 'Doe', 
        country: '',
        email: 'jon.doe@' 
    }
    const results = validate(model).
            required('firstName').
            length('firstName', { minLength: 4 }).
            required('country').
            email('email', 'Please enter a valid email').
            done()

IMPORTANT NOTE to ensure you call .done() at the end to get results.

  1. Receive your model back with any validation errors.

    const model = { 
        firstName: 'Jon', 
        surname: 'Doe', 
        country: '',
        email: 'jon.doe@', 
        errors: {
            'firstName': ['Invalid length for firstName'],
            'country: ['The country field is required'],
            'email': ['Please enter a valid email']
        } 
    }

parameters

ValidatorParameterDescriptionExample
.required()fieldThe first parameter is required to be the property string on your model holding the value to be validated.'country'
messageThe second parameter is an optional message string.'Please enter your country.'
.length()fieldThe first parameter is required to be the property string on your model holding the value to be validated.'firstName'
rulesThe second parameter is an optional object containing message, minLength and maxLength, all of which are also optional.{ message: 'Your first name must be between 4 and 100 characters long.', minLength: 4, maxLength: 100 }

. . .

frequent questions

- Why are the errors placed in an array?

A single field may fail multiple validation rules therefore 
an array is used to collate error messages for each field.

- Can I validate an array field?

This feature is currently under development and will be available soon.

- Where can I place a feature request?

If you need a feature that is not yet available. Feel free to 
create an issue on the github repo, github.com/jomeno/modelok.
0.1.30

3 months ago

0.1.31

3 months ago

0.1.29

3 months ago

0.1.13

3 months ago

0.1.14

3 months ago

0.1.15

3 months ago

0.1.27

3 months ago

0.1.28

3 months ago

0.1.20

3 months ago

0.1.21

3 months ago

0.1.22

3 months ago

0.1.23

3 months ago

0.1.24

3 months ago

0.1.25

3 months ago

0.1.26

3 months ago

0.1.16

3 months ago

0.1.17

3 months ago

0.1.18

3 months ago

0.1.19

3 months ago

0.1.10

3 months ago

0.1.11

3 months ago

0.1.12

3 months ago

0.1.8

3 months ago

0.1.7

3 months ago

0.1.9

3 months ago

0.1.6

3 months ago

0.1.5

3 months ago

0.1.4

3 months ago

0.1.3

3 months ago

0.1.2

3 months ago

0.1.1

3 months ago

0.1.0

3 months ago