Models
Every model consists of one or more declarative rules (see this project for a nodejs reference implementation that also works in the browser).
Models are compiled to a cachable json structure that is used to validate data.
Rules
Every rule in a model is defined by at least a type and a path. The type describes what kind of data is expected and the path describes where to find it. Consider the following data and model.
example.json
{ created: 1480801018654, user: { name: 'Glen Danzig' } }
example.model
Date created String user.name
A rule can also have a default error message.
Date created "Must be a valid date"
Validators
In addition to a type, a rule can have zero or more validators. A validator is a function (it is possible to create custom validators). Validators are declared inside curly braces immediately following the type and path. A validator has two or more parts — a function name, a value, and an optional error message. The data must satisfy criteria defined by the rule's validators.
example.model
String user.name required gt 2 "Must be greater than 2 characters" lte 256 "Must be less than or equal to 256 characters"
Rules that have validators can also have a default error message.
String user.name "Must be a valid name" required true
Built-in validators can also handle relative and absolute dates.
Date birthday gt +1week "Must be greater than one week from now"
Built-in validators can also handle regular expressions.
String uuid regExp /[0-9]{1,3}.[0-9]{1,3}/
Comments
C-style inline comments are supported.
Date created Number id String name required true // this is a comment gt 2 "Must be greater than 2 characters" lte 256 "Must be less than or equal to 256 characters" String bio "A bio must be a string" lte 140 "A bio must fit into a tweet" Boolean accountType
Custom Types
Often you need to define a single type that you will use accross more than one model.
example.model
def Password type String gte 3 lte 15 regexp /\d+/ "must contain at least one digit" Password mypass require 'a pwd is reqd'
Directives
A model can contain the @clean directive which will ensure that any values not defined in the model are removed from the result.
input.json
{ created: 1480801018654, height: 'Short, AF', user: { name: 'Glen Danzig' } }
example.model
@clean Date created String user.name
Given the above data and model, the result will not include the height property.
output.json
{ created: '2016-10-02T13:56:44.931Z', user: { name: 'Glen Danzig' } }