VDN

Main validation class.
Examples
const vdn = require('vdn');

const schema = vdn.number().default(42);

vdn.attempt(undefined, schema); // Result == 42 (default used)
vdn.attempt(3, schema);         // Result == 3 (valid)
vdn.attempt('5', schema);       // Result == 5 (string conversion)
vdn.attempt('f', schema);       // Throws ValidationError
const vdn = require('vdn');

const schema = vdn.object().entries({
  id: vdn.number().integer().required(),
  mail: vdn.string().email().required(),
}).required();

vdn.attempt({ id:2, mail:'a@b.com'   }, schema); // Valid
vdn.attempt({ id:2.3, mail:'a@b.com' }, schema); // Throws ValidationError
vdn.attempt({ id:2,   mail:'b.com'   }, schema); // Throws ValidationError
vdn.attempt({ undefined, undefined   }, schema); // Throws ValidationError
vdn.attempt(undefined,                  schema); // Throws ValidationError

Extends

Members

options :Object

Options used during validation. Currently unused. Possible future use.

(readonly) ValidationError :ValidationError

The validation error class.

(readonly) validators :Object

Access the validators available.
Example
vdn.validators.any    // Access the 'any' type validator.
vdn.validators.string // Access the 'string' type validator.

(readonly) version :string

The package version number.
Example
vdn.version // Returns semantic version number, eg: '1.0.2'

Methods

attempt(value, schema) → {*}

Attempt to validate a value against a schema.
Parameters:
Name Type Description
value * The value to validate.
schema Object The schema to validate against.
Throws:
Type
ValidationError
Thrown if validation fails.
Returns:
Type
*
The validated value (may have been cloned).
Example
const schema = vdn.array().items(vdn.string())

vdn.attempt(['a', 'b'], schema) // Result is ['a', 'b']
vdn.attempt(['a', 2], schema)   // Throws ValidationError

clearDefaults(type)

Clear defaults for a type.
Parameters:
Name Type Description
type string The type to clear defaults for.
Example
vdn.clearDefaults('any')
vdn.clearDefaults('string')

setDefaults(type, defaults)

Set defaults for a type.
Parameters:
Name Type Description
type string The type to set defaults for.
defaults Object The defaults to use.
Example
// All types will be required by default.
vdn.setDefaults('any', { required: true })

// Strings will now NOT be required by default. Limit string length.
const stringDefaults = {
    required: false, 
    maxLength: 1024  
}
vdn.setDefaults('string', stringDefaults)