Skip to content

component/model-validate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

model-validate

A plugin that adds validators to a model.

Installation

$ component install component/model-validate

Example

var model = require('model');
var validate = require('model-validate');
var isEmail = require('is-email');
var isUrl = require('is-url');

/**
 * User model.
 */

var User = model()
  .use(validate())
  .attr('id')
  .attr('email', { validators: [isEmail] })
  .attr('website', { validators: [isUrl] });

/**
 * Usage...
 */

var user = new User({ email: 'invalid' });
var valid = user.validate();

if (!valid) {}
  var error = user.errors[0];
  throw new Error('invalid attribute "' + error.attr + '"');
}

API

.validator([attr], fn)

Register a validator fn. It can be either:

  • attribute specific - passing an attr, in which case it will be called with the value of attr. Attribute-specific validators can also be specified by just adding a validators property to an attribute's options.
  • global - omitting the attr, in which case it will be called with the model itself.

#validate

Validate the model, returning a boolean of its validity, and populating model.errors for each failed validation.

#invalid(attr, message [context])

Mark an attr as invalid, with a message and optional extra context.

About

A plugin that adds validators to a model.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published