1.1.3 • Published 10 years ago

mio-validators v1.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

mio-validators Build Status Coverage Status Bower version NPM version

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

MIT Licensed

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.3

10 years ago

1.0.2

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago

0.3.0

11 years ago

0.1.0

11 years ago