File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments