0.2.2 • Published 8 years ago
schemation v0.2.2
Schema grammar
import {any, boolean, number, string} from "schemation"
import {and, not, or} from "schemation"
import {where} from "schemation"
import {optional} from "schemation"
import {lazy} from "schemation"
<schema> ::= <class>
| <lazy>
| <logical>
| <predicate>
| <shape>
<class> ::= any
| boolean
| number
| string
<lazy> ::= lazy( () => <schema> )
<logical> ::= and( <schema>, ... )
| not( <schema> )
| or( <schema>, ... )
<predicate> ::= /.../
| where( <unary-predicate> )
<shape> ::= false | true
| "..."
| <number>
| [ <schema> ]
| null
| { <property>, ... }
<property> ::= "...": <schema>
| "...": optional( <schema> )
Entry points
import {matches, tryMatch, validate} from "schemation"
matches(schema)(json)
=> true
| false
tryMatch(schema, onMatch, onMismatch)(json)
=> onMatch(json)
| onMismatch(mismatch)
validate(schema)(json)
=> json
| throw new Error(mismatch)
Mismatches
import {Mismatch, MismatchAt, Mismatches} from "schemation"
mismatch ::= Mismatch {value}
| MismatchAt {mismatch, index}
| Mismatches {mismatches}
mismatch.toString()
Extending
For example, given
const empty = where(x => x.length === 0)
the expression
and([any], not(empty))
matches any non-empty array.
Example
const SetOfProducts = [
{
id: number,
name: string,
price: and(number, where(x => 0 < x)),
tags: optional(and([string], not(empty))),
dimensions: optional({
length: number,
width: number,
height: number
}),
warehouselocation: optional({
latitude: number,
longitude: number
})
}
]