Skip to content

Commit e0629d9

Browse files
committed
Merge branch 'master' of https://github.com/kawasima/json-spec
2 parents c4a47f1 + 86d4ee7 commit e0629d9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
JSON Spec
2+
===========
3+
4+
JSON Spec is a tool for validation and generation of JSON data. This is a porting of a part of clojure.spec.
5+
6+
## Validate
7+
8+
Spec is defined as follows
9+
10+
```javascript
11+
const s = require('json-spec');
12+
const bigEven = s.and(x => !isNaN(Number(x)), x => x%2 === 0, x => x > 1000);
13+
```
14+
15+
And you can validate using the spec
16+
17+
```javascript
18+
s.isValid(bigEven, 'foo'); // false
19+
s.isValid(bigEven, 10); // false
20+
s.isValid(bigEven, 10000); // true
21+
```
22+
23+
The example of validating a JSON Object is as follows.
24+
25+
```javascript
26+
const isString = x => typeof(x) === 'string';
27+
const emailMatch = x => new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/).test(x);
28+
const emailType = s.and(isString, emailMatch);
29+
30+
const person = s.object({
31+
first_name: isString,
32+
last_name: isString,
33+
email: emailType
34+
});
35+
36+
s.isValid(person, {
37+
first_name: 'Yoshitaka',
38+
last_name: 'Kawashima',
39+
email: 'kawasima@example.com'
40+
}); // true
41+
42+
```

0 commit comments

Comments
 (0)