modelok
v0.1.31
Published
A simple JavaScript object validation library.
Downloads
4
Maintainers
Readme
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
First install using
npm i modelok
and import into your project.import validate from 'modelok'
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.
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
| Validator | Parameter | Description | Example
| --- | --- | --- | ---
| .required() | field | The first parameter is required
to be the property string on your model holding the value to be validated. | 'country'
| | message | The second parameter is an optional
message string. | 'Please enter your country.'
| .length() | field | The first parameter is required
to be the property string on your model holding the value to be validated. | 'firstName'
| | rules | The 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.