The data validation library for Node.js and the Browser
This readme is currently incomplete
The way dival works is you create a template:
const template =// fields here
This template can then be used to test wether data conforms to the template
template// returns either true or false
What is a template?
A template is an object that has a test method, this method takes data and returns
true if the data conforms to the template, otherwise it returns
a template can be created as a class with the
const template =// fields
An optional second parameter can be given to the Template constructor, a custom rules Map, this is a Map with rule identifiers as map keys and the rules themselves as values, more on what rules are further down.
What is a field?
a field is a key of a template, it is an object with rules identifiers as keys and rule settings as values, an example of a field:
const plainTemplate =// fields here:name: // the key of a field matches a property in the datatype: 'string'age:type: 'number'const template = plainTemplatetemplate// => truetemplate// => false
What is a rule?
A rule is a key to a field, it will check the fields corresponding value in the data. The value of a rule is called a
The test method on a template will only return
true if all rules pass
There are several types of rules:
- type: checks the type of the data property
- required: ensures the property is present (not
- child: this rule is used to test deeper data, more on this rule below
The type rule
This rule is fairly straight-forward, it checks that a value is of type
NOTE: this rule does not work like the required rule, when passed undefined it always returns
possible settings for this rule are:
Note that these are all lower-case
the required rule
The most simple rule, takes a
boolean as a setting and checks that a value is not undefined
- child rule
- custom rules