vstack-validator
Declarative validator for complex deep structures
Intro
Validator is a simple function that receives a value and returns boolean result of validation. Validator could be asynchronous.
type boolean | Promise<boolean>
Constraint is a function that receives a value and returns validation metadata. Constraint should be asynchronous.
type Promise<ConstraintResult> type ConstraintResult = valid: boolean message: string | void children: ConstraintResultMap | ConstraintResultArray | void type ConstraintResultMap = key: string: ConstraintResult type ConstraintResultArray = Array<ConstraintResult>
Schema is a declaration of constraints tree.
type: string; check: Constraint; children: SchemaChildren; type SchemaChildren = ?key: string: Schema | Array<Schema> | Schema;
Usage
; var userSchema = schematype'user' email: notEmpty: schema email: schema password: schema; var itemSchema = schematype'item' name: notEmpty: schema ; var cartSchema = schematype'cart' user: userSchema items: schema; cartSchema ;
{ valid: false,
message: 'Object is not valid',
children:
{ user:
{ valid: false,
message: 'Object is not valid',
children:
{ email:
{ valid: false,
message: 'Value is not valid',
children:
{ notEmpty: { valid: false, message: 'Email is empty', children: null },
email: { valid: false, message: 'Email is not valid', children: null } } },
password:
{ valid: false,
message: 'Value is not valid',
children:
{ notEmpty: { valid: false, message: 'Password is empty', children: null },
minLength:
{ valid: false,
message: 'Password is less than 3',
children: null } } } } },
items:
{ valid: false,
message: 'Array is not valid',
children:
[ { valid: false,
message: 'Object is not valid',
children:
{ name:
{ valid: false,
message: 'Value is not valid',
children: { notEmpty: { valid: false, message: 'Name is empty', children: null } } } } } ] } } }