can-validate
Shared utilities and type definitions to process validation errors.
Object
The can-validate
module exports helpful methods used for validation and also describes
the different types used during validation.
var validate = require('my-validator');
var utils = require('can-validate');
// Normalize errors into a flat structure
var errors = utils.formatErrors(validate(obj, constraints), 'flat');
Usage
The formatErrors method can be used to convert errors into something more useful.
var formatErrors = require('can-validate').formatErrors;
var errors = [
'is required',
{
message: 'must be a number',
related: 'age'
}
];
formatErrors(errors, 'object');
//=> [{'*': ['is required']}, {'age': ['must be a number']}]
formatErrors(errors, 'flat');
//=> ['is required', 'must be a number']
formatErrors(errors, 'errors');
//=> [{message: 'is required', related: '*'}, {'age': ['must be a number']}]
Types
Core definitions of types used in validation.