Dataspec
Specify how data should look like, assert validity and generate examples.
Specifications are built from functions (returning true
or false
depending
on wether the specification is met or not) and can be composed together.
Once some specification is built, it can be used to validate data and to generate example data.
Example
const spec arrayOf objectOf = // A specification for stringsconst string = // A specification for missing values (null or undefined)const absent = // Specifications can be composed, for example to specify a string that can be// absent, the previous two specifications can be combinedconst maybeString = string // A specification for integersconst integer = // A byte is specified as an integer greater or equal than 0 and smaller than 256const byte = integer // An rgb value is an array of 3 bytesconst rgb = // A color is an object that has an optional name and a rgb valueconst color = // Validate: color // => truecolor // => throws SpecErrorcolor // => [ [ [ 'rgb', '0' ], // '-2 does not satisfy predicate x => x >= 0 && x < 256' ] ] // Generate: color // => returns a random example, e.g. { name: 'qux', rgb: [ 101, 3, 0 ] }