mio-validators
v1.1.3
Published
Validators for Mio.
Downloads
1
Readme
mio-validators
Validators for Mio models.
Installation
Using npm:
npm install --save mio-validators
Using bower:
bower install --save mio/validators
Using browser script tag and global (UMD wrapper):
// Available via window.mio.validators
<script src="dist/mio-validators.js"></script>
Usage
Only attributes that have values other than null
or undefined
are validated,
and those marked with required: true
will return a validation error if their
value is null
or undefined
.
var mio = require('mio');
var validators = require('mio-validators');
var User = mio.Resource.extend();
User
.use(validators())
.attr('id', {
primary: true,
constraints: [
validators.Assert.TypeOf('string'),
validators.Assert.Length({ min: 1, max: 32 })
]
})
.attr('name', {
required: true,
contraints: [
validators.Assert.TypeOf('string'),
validators.Assert.Length({ min: 2, max: 32 })
]
})
.attr('email', {
required: true,
constraints: [
validators.Assert.Email()
]
});
mio-validators uses asserted for assertions. Refer to the asserted documentation for information on available assertions or creating custom assertions.
"validate" hook
mio-validators adds a "validate" asynchronous event and calls its handlers in
series before put
, patch
, and post
hooks.
Resource#validate(callback)
Params
- callback
Function(ValidationError)
ValidationError
Passed to callback if validation(s) failed.
Properties
- message
String
defaults to "Validation(s) failed." - violations
Object.<String, Array>
map of attribute names to array of violation messages - stack
String
stack trace