API

import {define, conform, explain, explainData, spec} from 'js.spec'

define(id, spec)

Bind a spec to an identifier, preferably a Symbol. This identifier can be used everywhere instead of the spec itself.

const str_or_int = Symbol()
define(str_or_int, spec.or({
  'str': spec.string,
  'int': spec.integer
}))

conform(spec/id, value)

Returns the conformed value to this spec. In case of or it will be an array with the matched branches.

conform(str_or_int, 5) // => ['int', 5]
conform(str_or_int, 'five') // => ['str', 'five']

explain(spec/id, value)

Prints reasons why the value did not conform to this spec.

explain(str_or_int, false)
// Or(isString, isInteger)->isString: isString failed for false at []
// Or(isString, isInteger)->isInteger: isInteger failed for false at []

explainData(spec/id, value)

Like explain, but returns problems as data structure.

explainData(str_or_int, false)
/* =>
[ { path: [],
    via: [ 'Or(isString, isInteger)', 'isString'],
    value: false,
    predicate: [Function: isString],
    predicateName: 'isString' },
  { path: [],
    via: [ 'Or(isString, isInteger)', 'isInteger'],
    value: false,
    predicate: [Function: isInteger],
    predicateName: 'isInteger' } ]
*/

results matching ""

    No results matching ""