https://github.com/polytypic/schemation
https://github.com/polytypic/schemation
json schema validation
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/polytypic/schemation
- Owner: polytypic
- Created: 2016-01-06T20:11:18.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-07-11T07:20:59.000Z (over 8 years ago)
- Last Synced: 2025-03-30T20:43:15.048Z (10 months ago)
- Topics: json, schema, validation
- Language: JavaScript
- Homepage:
- Size: 41 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](http://badge.fury.io/js/schemation) [](https://travis-ci.org/polytypic/schemation) [](https://david-dm.org/polytypic/schemation) [](https://david-dm.org/polytypic/schemation?type=dev)
## Schema grammar
```javascript
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"
```
```jsx
::=
|
|
|
|
::= any
| boolean
| number
| string
::= lazy( () => )
::= and( , ... )
| not( )
| or( , ... )
::= /.../
| where( )
::= false | true
| "..."
|
| [ ]
| null
| { , ... }
::= "...":
| "...": optional( )
```
## Entry points
```javascript
import {matches, tryMatch, validate} from "schemation"
```
```javascript
matches(schema)(json)
=> true
| false
```
```javascript
tryMatch(schema, onMatch, onMismatch)(json)
=> onMatch(json)
| onMismatch(mismatch)
```
```javascript
validate(schema)(json)
=> json
| throw new Error(mismatch)
```
## Mismatches
```javascript
import {Mismatch, MismatchAt, Mismatches} from "schemation"
```
```javascript
mismatch ::= Mismatch {value}
| MismatchAt {mismatch, index}
| Mismatches {mismatches}
```
```javascript
mismatch.toString()
```
## Extending
For example, given
```javascript
const empty = where(x => x.length === 0)
```
the expression
```javascript
and([any], not(empty))
```
matches any non-empty array.
## Example
```javascript
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
})
}
]
```