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.
import validate from "my-validator";
import utils from "can-validate";
// Normalize errors into a flat structure
const errors = utils.formatErrors( validate( obj, constraints ), "flat" );
Usage
The formatErrors method can be used to convert errors into something more useful.
import { formatErrors } from "can-validate";
const 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.