ejv 1.1.9 → 1.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,600 +1,595 @@
1
- ![npm](https://img.shields.io/npm/v/ejv?logo=npm)
2
-
3
- # ejv - Easy JSON Validator
4
-
5
- [한국어](https://github.com/han41858/ejv/blob/master/README-KR.md)
6
-
7
- ejv is JSON validation library. Check your JSON object with simple syntax.
8
-
9
- > ejv is written by TypeScript, and published by JavaScript.
10
- So you can use this library in TypeScript code and JavaScript code also.
11
-
12
- ## Install
13
-
14
- ```bash
15
- npm install ejv
16
- ```
17
-
18
- ## `ejv(data : object, schemes : Scheme[])`
19
-
20
- ejv provides only one function.
21
- All validation use this function.
22
-
23
- `ejv()` is pure sync function.
24
- So you can use this function with Promise or Observable easily.
25
- This function does not change original JSON object.
26
-
27
- ### Load symbol
28
-
29
- - TypeScript, JavaScript (after ES6)
30
-
31
- ```typescript
32
- import { ejv, EjvError } from 'ejv';
33
- ```
34
-
35
- - JavaScript (before ES6)
36
-
37
- ```javascript
38
- var _ejv = require('ejv');
39
- var ejv = _ejv.ejv;
40
- ```
41
-
42
- ### Usage
43
-
44
- - TypeScript
45
-
46
- ```typescript
47
- const error : null | EjvError = ejv({
48
- a : 10
49
- }, [{
50
- key : 'a',
51
- type : 'number'
52
- }]);
53
-
54
- if (!error) {
55
- console.log('success');
56
- } else {
57
- console.log('failed');
58
- }
59
- ```
60
-
61
- - JavaScript
62
-
63
- ```javascript
64
- var error = ejv({
65
- a : 10
66
- }, [{
67
- key : 'a',
68
- type : 'number'
69
- }]);
70
-
71
- if (!error) {
72
- console.log('success');
73
- } else {
74
- console.log('failed');
75
- }
76
- ```
77
-
78
- ## `Scheme`
79
-
80
- `ejv()` needs *validation rules*.
81
- So, you should pass schemes to second parameter.
82
-
83
- Validation rules declared by array of object.
84
- ejv use this rule in order of array, you can check orderly, and result is always same.
85
-
86
- ### Mandatory keys
87
-
88
- #### `key` : `string`
89
-
90
- Specify the property to check.
91
- For example, if you want to check 'a' property in JSON object, set `key : a`
92
-
93
- > This property is omitted to check `array` with `items` option.
94
-
95
-
96
- #### `type` : [`DataType`](#DataType) | `DataType[]`
97
-
98
- Specify the type of property to check.
99
- If only one type is specified, Checksfor that type.
100
- And if specified as an array, checks if it corresponds to one of the items in the array.
101
-
102
-
103
- ### Optional keys
104
-
105
- #### Common
106
-
107
- - `optional : boolean`
108
-
109
- If you set it to `true`, ejv will allow the `undefined` value.
110
- This option is available for all validation rules.
111
-
112
- ```typescript
113
- ejv({
114
- // empty object
115
- }, [{
116
- key : 'a',
117
- optional : true // Error does not occur without proffering declared.
118
- }]);
119
- ```
120
-
121
- - `nullable : boolean`
122
-
123
- If you set it to `true`, ejv will allow the `null` value.
124
- This option is available for all validation rules.
125
-
126
- ```typescript
127
- ejv({
128
- a : null
129
- }, [{
130
- key : 'a',
131
- nullable : true
132
- }]);
133
- ```
134
-
135
- - `enum : number[] | string[]`
136
-
137
- Allows only the values that are delivered in an array.
138
- This option is available for the rules of validation: `type: number` and `type: string`.
139
-
140
- ```typescript
141
- ejv({
142
- a : 1,
143
- b : 'hello'
144
- }, [{
145
- key : 'a',
146
- type : 'number',
147
- enum : [1, 2, 3] // allow 1, 2, 3
148
- }, {
149
- key : 'b',
150
- type : 'string',
151
- enum : ['hello', 'ejv'] // allow 'hello', 'ejv'
152
- }]);
153
- ```
154
-
155
- - `enumReverse : number[] | string[]`
156
-
157
- Not allows the values that are delivered in an array. This result is reverse of the option `enum`.
158
- This option is available for the rules of validation: `type: number` and `type: string`.
159
-
160
- ```typescript
161
- ejv({
162
- a : 1,
163
- b : 'hello'
164
- }, [{
165
- key : 'a',
166
- type : 'number',
167
- enumReverse : [1, 2, 3] // not allow 1, 2, 3
168
- }, {
169
- key : 'b',
170
- type : 'string',
171
- enumReverse : ['hello', 'ejv'] // not allow 'hello', 'ejv'
172
- }]);
173
- ```
174
-
175
- #### `'number'` options
176
-
177
- - `min : number`
178
-
179
- Checks ths minimum value.
180
- Error occurs if the value is smaller than this value.
181
-
182
- - `exclusiveMin : boolean`
183
-
184
- If you specify `true`, ejv will not allow the same value as the minimum limit.
185
- If you omit this option or specify it as `false`, ejv will allow the same value as the minimum limit.
186
- This option is used only when the `min` option is used.
187
-
188
- ```typescript
189
- ejv({
190
- num1 : 10,
191
- num2 : 10
192
- }, [{
193
- key : 'num1',
194
- type : 'number',
195
- min : 10 // success
196
- }, {
197
- key : 'num2',
198
- type : 'number',
199
- min : 10,
200
- exclusiveMin : true // failed
201
- }]);
202
- ```
203
-
204
- - `max : number`
205
-
206
- Checks the maximum value.
207
- Error occurs if the number is greater than this value.
208
-
209
- - `exclusiveMax : boolean`
210
-
211
- If you specify `true`, ejv will not allow the same value as the maximum limit.
212
- If you omit this option or specify it as `false`, ejv will allow the same value as the maximum limit.
213
- This option is used only when the `min` option is used.
214
-
215
- ```typescript
216
- ejv({
217
- num1 : 10,
218
- num2 : 10
219
- }, [{
220
- key : 'num1',
221
- type : 'number',
222
- max : 10 // success
223
- }, {
224
- key : 'num2',
225
- type : 'number',
226
- max : 10,
227
- exclusiveMax : true // failed
228
- }]);
229
- ```
230
-
231
- - `format : NumberFormat | NumberFormat[]`
232
-
233
- Checks the format of the number.
234
- If specified as an array, ejv allow the value if it corresponds to one of the given formats.
235
- The available formats are as follows.
236
-
237
- format|example
238
- ---|---
239
- `'integer'`|Allows only integer. ex) -1, 0, 1, ...
240
- `'index'`|Allows only index. This format is same rule with `format : 'integer', min : 0`. ex) 0, 1, 2, ...
241
-
242
- #### `'string'` options
243
-
244
- - `format : StringFormat | StringFormat[]`
245
-
246
- Checks the format of string.
247
- If specified as an array, ejv will allow the value if it corresponds to one of the given formats.
248
- The available formats are as follows.
249
-
250
- format|example
251
- ---|---
252
- `'email'`|Allows only email. This is based on [RFC 5322 3.4.1](https://tools.ietf.org/html/rfc5322#section-3.4.1). ex) `'email@domain.com'`
253
- `'date'`|Allows only date string format. This is based on [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'2018-12-29'`
254
- `'time'`|Allows only time string format. This is based on [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'21:07:35'`
255
- `'date-time'`|Allows only date-time string format. This is based on [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) and [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'2018-12-29T21:07:35Z'`
256
-
257
- - `length : number`
258
-
259
- Checks the length of string.
260
-
261
- ```typescript
262
- ejv({
263
- str : 'hello'
264
- }, [{
265
- key : 'str',
266
- type : 'string',
267
- length : 5
268
- }]);
269
- ````
270
-
271
- - `minLength : number`
272
-
273
- Checks the minimum length of string.
274
-
275
- ```typescript
276
- ejv({
277
- str : 'hello'
278
- }, [{
279
- key : 'str',
280
- type : 'string',
281
- minLength : 5
282
- }]);
283
- ````
284
-
285
- - `maxLength : string`
286
-
287
- Checks the maximum length of string.
288
-
289
- ```typescript
290
- ejv({
291
- str : 'hello'
292
- }, [{
293
- key : 'str',
294
- type : 'string',
295
- maxLength : 5
296
- }]);
297
- ````
298
-
299
- - `pattern : string | string[] | RegExp | RegExp[]`
300
-
301
- Checks the pattern of string.
302
- If specified as a string, the string is converted to a regular expression and checked, and if specified as a regular expression, it checks whether it passes the regular expression.
303
- If the value of this option is specified as an array, pass the check if one of the rule passes.
304
-
305
- ```typescript
306
- ejv({
307
- str : 'abc'
308
- }, [{
309
- key : 'str',
310
- type : 'string',
311
- pattern : 'abc'
312
- }, {
313
- key : 'str',
314
- type : 'string',
315
- pattern : ['abc', 'ac']
316
- }, {
317
- key : 'str',
318
- type : 'string',
319
- pattern : /abc/
320
- }, {
321
- key : 'str',
322
- type : 'string',
323
- pattern : [/abc/, /ac/]
324
- }]);
325
- ```
326
-
327
- #### `'object'` options
328
-
329
- - `allowNoProperty : boolean`
330
-
331
- Checks if object has at least one property.
332
- If you specify `false`, ejv will not allow the empty object.
333
- If you omit this option or specify it as `true`, ejv will allow the empty object has no property.
334
-
335
- ```typescript
336
- ejv({
337
- obj : {}
338
- }, [{
339
- key : 'obj',
340
- type : 'object',
341
- allowNoProperty : false // failed
342
- }]);
343
- ```
344
-
345
- - `properties : Scheme[]`
346
-
347
- Specify the details of the object.
348
- The object specified for the validation is recursively processed by ejv().
349
-
350
- ```typescript
351
- ejv({
352
- data : {
353
- num : 10,
354
- str : 'ejv'
355
- }
356
- }, [{
357
- key : 'data',
358
- type : 'object',
359
- properties : [{
360
- key : 'num',
361
- type : 'number'
362
- }, {
363
- key : 'str',
364
- type : 'string'
365
- }]
366
- }]);
367
- ```
368
-
369
- #### `'date'` options
370
-
371
- - `min : Date | string`
372
-
373
- Checks the minimum value of the date.
374
- Error occurs when the date is earlier than this value.
375
- The minimum value can be used for `Date` object or text representing a date.
376
-
377
- - `exclusiveMin : boolean`
378
-
379
- If you specify `true`, ejv will not allow the same date as the minimum limit.
380
- If you omit this option or specify it as `false`, ejv will allow the same date as the minimum limit.
381
- This option is used only when the `min` option is used.
382
-
383
- ```typescript
384
- ejv({
385
- date1 : new Date(2019, 11, 30)
386
- }, [{
387
- key : 'date1',
388
- type : 'date',
389
- min : new Date(2019, 11, 30) // success
390
- }, {
391
- key : 'date1',
392
- type : 'date',
393
- min : new Date(2019, 11, 30),
394
- exclusiveMin : true // failed
395
- }, {
396
- key : 'date1',
397
- type : 'date',
398
- min : '2019-12-30T00:00:00Z' // success
399
- }, {
400
- key : 'date1',
401
- type : 'date',
402
- min : '2019-12-30T00:00:00Z',
403
- exclusiveMin : true // failed
404
- }]);
405
- ```
406
-
407
- - `max : Date | string`
408
-
409
- Checks the maximum value of the date.
410
- Error occurs when the date is after than this value.
411
- The maximum value can be used for `Date` object or text representing a date.
412
-
413
- - `exclusiveMax : boolean`
414
-
415
- If you specify `true`, ejv will not allow the same date as the maximum limit.
416
- If you omit this option or specify it as `false`, ejv will allow the same date as the maximum limit.
417
- This option is used only when the `max` option is used.
418
-
419
- ```typescript
420
- ejv({
421
- date1 : new Date(2019, 11, 30)
422
- }, [{
423
- key : 'date1',
424
- type : 'date',
425
- max : new Date(2019, 11, 30) // success
426
- }, {
427
- key : 'date1',
428
- type : 'date',
429
- max : new Date(2019, 11, 30),
430
- exclusiveMax : true // failed
431
- }, {
432
- key : 'date1',
433
- type : 'date',
434
- max : '2019-12-30T00:00:00Z' // success
435
- }, {
436
- key : 'date1',
437
- type : 'date',
438
- max : '2019-12-30T00:00:00Z',
439
- exclusiveMax : true // failed
440
- }]);
441
- ```
442
-
443
- #### `'array'` options
444
-
445
- - `length : number`
446
-
447
- Checks the length of the array.
448
-
449
- ```typescript
450
- ejv({
451
- arr : [1, 2]
452
- }, [{
453
- key : 'arr',
454
- type : 'array',
455
- length : 2
456
- }]);
457
- ````
458
-
459
- - `minLength : number`
460
-
461
- Checks the minimum length of the array.
462
-
463
- ```typescript
464
- ejv({
465
- arr : [1, 2]
466
- }, [{
467
- key : 'arr',
468
- type : 'array',
469
- minLength : 2
470
- }]);
471
- ````
472
-
473
- - `maxLength : string`
474
-
475
- Checks the maximum length of the array.
476
-
477
- ```typescript
478
- ejv({
479
- arr : [1, 2, 3]
480
- }, [{
481
- key : 'arr',
482
- type : 'array',
483
- maxLength : 3
484
- }]);
485
- ````
486
-
487
- - `unique : boolean`
488
-
489
- Checks if all items in the array are different.
490
- If you specify `true`, ejv will not allow the array to duplicate values.
491
- If you omit this option or specify it as `false`, ejv will allow to duplicate the values of the array.
492
-
493
- - `items : Scheme[]`
494
-
495
- Specify the rules to inspect items in the array.
496
- The Scheme specified at this time is the same format as the Scheme used in the `ejv()``, but omits the `key`.
497
- Schemes specified as arrays are recursively processed by `ejv()`, and processed in the order specified in the array.
498
-
499
- ```typescript
500
- ejv({
501
- arr : [1, 2, 3]
502
- }, [{
503
- key : 'arr',
504
- type : 'array',
505
- items : [{
506
- type : 'number',
507
- min : 1,
508
- max : 3
509
- }]
510
- }])
511
- ```
512
-
513
- ## `DataType`
514
-
515
- Specify the type of property to inspect. The values available are as follows.
516
-
517
- type|example
518
- ---|---
519
- `'boolean'`|`true`, `false`
520
- `'number'`|`0`, `1`, `1.5`, ...
521
- `'string'`|`'ejv'`, `'hello'`, ...
522
- `'object'`|`{}`, `{ key : 123 }`, ...
523
- `'date'`|`new Date`
524
- `'regexp'`|`new RegExp(/./)`, `/./`, ...
525
- `'array'`|`[]`, `[1, 2, 3]`, ...
526
-
527
-
528
- ## `EjvError`
529
-
530
- If the JSON object passes the validation rule, it returns the `null` object, but if it does not pass the inspection rule, it returns the instance of the `EjvError` type.
531
- The `EjvError` object is an object that represents the error that occurred at this time.
532
-
533
- > You do not always need to use `EjvError` type.
534
- However, if you use TypeScript, you can use it to refer to the property of an error object.
535
-
536
- - `type : ErrorType`
537
-
538
- Represents the type of the error that occurred.
539
-
540
- - `keyword : string`
541
-
542
- Describes the contents of the error that occurred.
543
-
544
- - `path : string`
545
-
546
- Points to the location of the data where the error occurred.
547
-
548
- - `data : any`
549
-
550
- Means the data that passed to `ejv()`.
551
-
552
- - `errorData : any`
553
-
554
- Means the data that the error occurred.
555
-
556
- usage)
557
-
558
- ```typescript
559
- import { ejv, EjvError } from 'ejv';
560
-
561
- const error : null | EjvError = ejv({
562
- a : 10
563
- }, [{
564
- key : 'a',
565
- type : 'string'
566
- }]);
567
-
568
- console.log(error.type); // 'TYPE_MISMATCH'
569
- console.log(error.message); // 'the value should be a string'
570
- console.log(error.path); // 'a'
571
- console.log(error.data); // { a : 10 }
572
- console.log(error.errorData); // 10
573
- ```
574
-
575
- ## Options
576
-
577
- When using a `ejv()` function, you can specify options as a third parameter.
578
-
579
- - `customErrorMsg: object`
580
-
581
- You can override error message corresponding with `EjvError.type` to another content.
582
-
583
- This option is used in the type of `object`. You can use `ErrorType` as a key when overriding error message.
584
-
585
- ```typescript
586
- import { ejv, EjvError, ErrorType } from 'ejv';
587
-
588
- const error : null | EjvError = ejv({
589
- a : 10
590
- }, [{
591
- key : 'a',
592
- type : 'string'
593
- }, {
594
- customErrorMsg : {
595
- [ErrorType.TYPE_MISMATCH] : 'property "a" should be a "string".'
596
- }
597
- }]);
598
-
599
- console.log(error.message); // 'property "a" should be a "string".'
600
- ```
1
+ ![npm](https://img.shields.io/npm/v/ejv?logo=npm)
2
+
3
+ # ejv - Easy JSON Validator
4
+
5
+ [한국어](https://github.com/han41858/ejv/blob/master/README-KR.md)
6
+
7
+ ejv is JSON validation library. Check your JSON object with simple syntax.
8
+
9
+ > ejv is written by TypeScript, and published by JavaScript. So you can use this library in TypeScript code and JavaScript code also.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install ejv
15
+ ```
16
+
17
+ ## `ejv(data : object, schemes : Scheme[])`
18
+
19
+ ejv provides only one function.
20
+ All validation use this function.
21
+
22
+ `ejv()` is pure sync function.
23
+ So you can use this function with Promise or Observable easily.
24
+ This function does not change original JSON object.
25
+
26
+ ### Load symbol
27
+
28
+ - TypeScript, JavaScript (after ES6)
29
+
30
+ ```typescript
31
+ import { ejv, EjvError } from 'ejv';
32
+ ```
33
+
34
+ - JavaScript (before ES6)
35
+
36
+ ```javascript
37
+ var _ejv = require('ejv');
38
+ var ejv = _ejv.ejv;
39
+ ```
40
+
41
+ ### Usage
42
+
43
+ - TypeScript
44
+
45
+ ```typescript
46
+ const error : null | EjvError = ejv({
47
+ a : 10
48
+ }, [{
49
+ key : 'a',
50
+ type : 'number'
51
+ }]);
52
+
53
+ if (!error) {
54
+ console.log('success');
55
+ } else {
56
+ console.log('failed');
57
+ }
58
+ ```
59
+
60
+ - JavaScript
61
+
62
+ ```javascript
63
+ var error = ejv({
64
+ a : 10
65
+ }, [{
66
+ key : 'a',
67
+ type : 'number'
68
+ }]);
69
+
70
+ if (!error) {
71
+ console.log('success');
72
+ } else {
73
+ console.log('failed');
74
+ }
75
+ ```
76
+
77
+ ## `Scheme`
78
+
79
+ `ejv()` needs *validation rules*.
80
+ So, you should pass schemes to second parameter.
81
+
82
+ Validation rules declared by array of object.
83
+ ejv use this rule in order of array, you can check orderly, and result is always same.
84
+
85
+ ### Mandatory keys
86
+
87
+ #### `key` : `string`
88
+
89
+ Specify the property to check.
90
+ For example, if you want to check 'a' property in JSON object, set `key : a`
91
+
92
+ > This property is omitted to check `array` with `items` option.
93
+
94
+ #### `type` : [`DataType`](#DataType) | `DataType[]`
95
+
96
+ Specify the type of property to check.
97
+ If only one type is specified, Checksfor that type.
98
+ And if specified as an array, checks if it corresponds to one of the items in the array.
99
+
100
+ ### Optional keys
101
+
102
+ #### Common
103
+
104
+ - `optional : boolean`
105
+
106
+ If you set it to `true`, ejv will allow the `undefined` value.
107
+ This option is available for all validation rules.
108
+
109
+ ```typescript
110
+ ejv({
111
+ // empty object
112
+ }, [{
113
+ key : 'a',
114
+ optional : true // Error does not occur without proffering declared.
115
+ }]);
116
+ ```
117
+
118
+ - `nullable : boolean`
119
+
120
+ If you set it to `true`, ejv will allow the `null` value.
121
+ This option is available for all validation rules.
122
+
123
+ ```typescript
124
+ ejv({
125
+ a : null
126
+ }, [{
127
+ key : 'a',
128
+ nullable : true
129
+ }]);
130
+ ```
131
+
132
+ - `enum : number[] | string[]`
133
+
134
+ Allows only the values that are delivered in an array.
135
+ This option is available for the rules of validation: `type: number` and `type: string`.
136
+
137
+ ```typescript
138
+ ejv({
139
+ a : 1,
140
+ b : 'hello'
141
+ }, [{
142
+ key : 'a',
143
+ type : 'number',
144
+ enum : [1, 2, 3] // allow 1, 2, 3
145
+ }, {
146
+ key : 'b',
147
+ type : 'string',
148
+ enum : ['hello', 'ejv'] // allow 'hello', 'ejv'
149
+ }]);
150
+ ```
151
+
152
+ - `enumReverse : number[] | string[]`
153
+
154
+ Not allows the values that are delivered in an array. This result is reverse of the option `enum`.
155
+ This option is available for the rules of validation: `type: number` and `type: string`.
156
+
157
+ ```typescript
158
+ ejv({
159
+ a : 1,
160
+ b : 'hello'
161
+ }, [{
162
+ key : 'a',
163
+ type : 'number',
164
+ enumReverse : [1, 2, 3] // not allow 1, 2, 3
165
+ }, {
166
+ key : 'b',
167
+ type : 'string',
168
+ enumReverse : ['hello', 'ejv'] // not allow 'hello', 'ejv'
169
+ }]);
170
+ ```
171
+
172
+ #### `'number'` options
173
+
174
+ - `min : number`
175
+
176
+ Checks ths minimum value.
177
+ Error occurs if the value is smaller than this value.
178
+
179
+ - `exclusiveMin : boolean`
180
+
181
+ If you specify `true`, ejv will not allow the same value as the minimum limit.
182
+ If you omit this option or specify it as `false`, ejv will allow the same value as the minimum limit.
183
+ This option is used only when the `min` option is used.
184
+
185
+ ```typescript
186
+ ejv({
187
+ num1 : 10,
188
+ num2 : 10
189
+ }, [{
190
+ key : 'num1',
191
+ type : 'number',
192
+ min : 10 // success
193
+ }, {
194
+ key : 'num2',
195
+ type : 'number',
196
+ min : 10,
197
+ exclusiveMin : true // failed
198
+ }]);
199
+ ```
200
+
201
+ - `max : number`
202
+
203
+ Checks the maximum value.
204
+ Error occurs if the number is greater than this value.
205
+
206
+ - `exclusiveMax : boolean`
207
+
208
+ If you specify `true`, ejv will not allow the same value as the maximum limit.
209
+ If you omit this option or specify it as `false`, ejv will allow the same value as the maximum limit.
210
+ This option is used only when the `min` option is used.
211
+
212
+ ```typescript
213
+ ejv({
214
+ num1 : 10,
215
+ num2 : 10
216
+ }, [{
217
+ key : 'num1',
218
+ type : 'number',
219
+ max : 10 // success
220
+ }, {
221
+ key : 'num2',
222
+ type : 'number',
223
+ max : 10,
224
+ exclusiveMax : true // failed
225
+ }]);
226
+ ```
227
+
228
+ - `format : NumberFormat | NumberFormat[]`
229
+
230
+ Checks the format of the number.
231
+ If specified as an array, ejv allow the value if it corresponds to one of the given formats.
232
+ The available formats are as follows.
233
+
234
+ format|example
235
+ ---|---
236
+ `'integer'`|Allows only integer. ex) -1, 0, 1, ...
237
+ `'index'`|Allows only index. This format is same rule with `format : 'integer', min : 0`. ex) 0, 1, 2, ...
238
+
239
+ #### `'string'` options
240
+
241
+ - `format : StringFormat | StringFormat[]`
242
+
243
+ Checks the format of string. If specified as an array, ejv will allow the value if it corresponds to one of the given
244
+ formats. The available formats are as follows.
245
+
246
+ format|example
247
+ ---|---
248
+ `'email'`|Allows only email. This is based on [RFC 5322 3.4.1](https://tools.ietf.org/html/rfc5322#section-3.4.1). ex) `'email@domain.com'`
249
+ `'date'`|Allows only date string format. This is based on [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'2018-12-29'`
250
+ `'time'`|Allows only time string format. This is based on [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'21:07:35'`
251
+ `'date-time'`|Allows only date-time string format. This is based on [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) and [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). ex) `'2018-12-29T21:07:35Z'`
252
+
253
+ - `length : number`
254
+
255
+ Checks the length of string.
256
+
257
+ ```typescript
258
+ ejv({
259
+ str : 'hello'
260
+ }, [{
261
+ key : 'str',
262
+ type : 'string',
263
+ length : 5
264
+ }]);
265
+ ````
266
+
267
+ - `minLength : number`
268
+
269
+ Checks the minimum length of string.
270
+
271
+ ```typescript
272
+ ejv({
273
+ str : 'hello'
274
+ }, [{
275
+ key : 'str',
276
+ type : 'string',
277
+ minLength : 5
278
+ }]);
279
+ ````
280
+
281
+ - `maxLength : string`
282
+
283
+ Checks the maximum length of string.
284
+
285
+ ```typescript
286
+ ejv({
287
+ str : 'hello'
288
+ }, [{
289
+ key : 'str',
290
+ type : 'string',
291
+ maxLength : 5
292
+ }]);
293
+ ````
294
+
295
+ - `pattern : string | string[] | RegExp | RegExp[]`
296
+
297
+ Checks the pattern of string.
298
+ If specified as a string, the string is converted to a regular expression and checked, and if specified as a regular expression, it checks whether it passes the regular expression.
299
+ If the value of this option is specified as an array, pass the check if one of the rule passes.
300
+
301
+ ```typescript
302
+ ejv({
303
+ str : 'abc'
304
+ }, [{
305
+ key : 'str',
306
+ type : 'string',
307
+ pattern : 'abc'
308
+ }, {
309
+ key : 'str',
310
+ type : 'string',
311
+ pattern : ['abc', 'ac']
312
+ }, {
313
+ key : 'str',
314
+ type : 'string',
315
+ pattern : /abc/
316
+ }, {
317
+ key : 'str',
318
+ type : 'string',
319
+ pattern : [/abc/, /ac/]
320
+ }]);
321
+ ```
322
+
323
+ #### `'object'` options
324
+
325
+ - `allowNoProperty : boolean`
326
+
327
+ Checks if object has at least one property.
328
+ If you specify `false`, ejv will not allow the empty object.
329
+ If you omit this option or specify it as `true`, ejv will allow the empty object has no property.
330
+
331
+ ```typescript
332
+ ejv({
333
+ obj : {}
334
+ }, [{
335
+ key : 'obj',
336
+ type : 'object',
337
+ allowNoProperty : false // failed
338
+ }]);
339
+ ```
340
+
341
+ - `properties : Scheme[]`
342
+
343
+ Specify the details of the object.
344
+ The object specified for the validation is recursively processed by ejv().
345
+
346
+ ```typescript
347
+ ejv({
348
+ data : {
349
+ num : 10,
350
+ str : 'ejv'
351
+ }
352
+ }, [{
353
+ key : 'data',
354
+ type : 'object',
355
+ properties : [{
356
+ key : 'num',
357
+ type : 'number'
358
+ }, {
359
+ key : 'str',
360
+ type : 'string'
361
+ }]
362
+ }]);
363
+ ```
364
+
365
+ #### `'date'` options
366
+
367
+ - `min : Date | string`
368
+
369
+ Checks the minimum value of the date.
370
+ Error occurs when the date is earlier than this value.
371
+ The minimum value can be used for `Date` object or text representing a date.
372
+
373
+ - `exclusiveMin : boolean`
374
+
375
+ If you specify `true`, ejv will not allow the same date as the minimum limit.
376
+ If you omit this option or specify it as `false`, ejv will allow the same date as the minimum limit.
377
+ This option is used only when the `min` option is used.
378
+
379
+ ```typescript
380
+ ejv({
381
+ date1 : new Date(2019, 11, 30)
382
+ }, [{
383
+ key : 'date1',
384
+ type : 'date',
385
+ min : new Date(2019, 11, 30) // success
386
+ }, {
387
+ key : 'date1',
388
+ type : 'date',
389
+ min : new Date(2019, 11, 30),
390
+ exclusiveMin : true // failed
391
+ }, {
392
+ key : 'date1',
393
+ type : 'date',
394
+ min : '2019-12-30T00:00:00Z' // success
395
+ }, {
396
+ key : 'date1',
397
+ type : 'date',
398
+ min : '2019-12-30T00:00:00Z',
399
+ exclusiveMin : true // failed
400
+ }]);
401
+ ```
402
+
403
+ - `max : Date | string`
404
+
405
+ Checks the maximum value of the date.
406
+ Error occurs when the date is after than this value.
407
+ The maximum value can be used for `Date` object or text representing a date.
408
+
409
+ - `exclusiveMax : boolean`
410
+
411
+ If you specify `true`, ejv will not allow the same date as the maximum limit.
412
+ If you omit this option or specify it as `false`, ejv will allow the same date as the maximum limit.
413
+ This option is used only when the `max` option is used.
414
+
415
+ ```typescript
416
+ ejv({
417
+ date1 : new Date(2019, 11, 30)
418
+ }, [{
419
+ key : 'date1',
420
+ type : 'date',
421
+ max : new Date(2019, 11, 30) // success
422
+ }, {
423
+ key : 'date1',
424
+ type : 'date',
425
+ max : new Date(2019, 11, 30),
426
+ exclusiveMax : true // failed
427
+ }, {
428
+ key : 'date1',
429
+ type : 'date',
430
+ max : '2019-12-30T00:00:00Z' // success
431
+ }, {
432
+ key : 'date1',
433
+ type : 'date',
434
+ max : '2019-12-30T00:00:00Z',
435
+ exclusiveMax : true // failed
436
+ }]);
437
+ ```
438
+
439
+ #### `'array'` options
440
+
441
+ - `length : number`
442
+
443
+ Checks the length of the array.
444
+
445
+ ```typescript
446
+ ejv({
447
+ arr : [1, 2]
448
+ }, [{
449
+ key : 'arr',
450
+ type : 'array',
451
+ length : 2
452
+ }]);
453
+ ````
454
+
455
+ - `minLength : number`
456
+
457
+ Checks the minimum length of the array.
458
+
459
+ ```typescript
460
+ ejv({
461
+ arr : [1, 2]
462
+ }, [{
463
+ key : 'arr',
464
+ type : 'array',
465
+ minLength : 2
466
+ }]);
467
+ ````
468
+
469
+ - `maxLength : string`
470
+
471
+ Checks the maximum length of the array.
472
+
473
+ ```typescript
474
+ ejv({
475
+ arr : [1, 2, 3]
476
+ }, [{
477
+ key : 'arr',
478
+ type : 'array',
479
+ maxLength : 3
480
+ }]);
481
+ ````
482
+
483
+ - `unique : boolean`
484
+
485
+ Checks if all items in the array are different.
486
+ If you specify `true`, ejv will not allow the array to duplicate values.
487
+ If you omit this option or specify it as `false`, ejv will allow to duplicate the values of the array.
488
+
489
+ - `items : Scheme[]`
490
+
491
+ Specify the rules to inspect items in the array.
492
+ The Scheme specified at this time is the same format as the Scheme used in the `ejv()``, but omits the `key`.
493
+ Schemes specified as arrays are recursively processed by `ejv()`, and processed in the order specified in the array.
494
+
495
+ ```typescript
496
+ ejv({
497
+ arr : [1, 2, 3]
498
+ }, [{
499
+ key : 'arr',
500
+ type : 'array',
501
+ items : [{
502
+ type : 'number',
503
+ min : 1,
504
+ max : 3
505
+ }]
506
+ }])
507
+ ```
508
+
509
+ ## `DataType`
510
+
511
+ Specify the type of property to inspect. The values available are as follows.
512
+
513
+ type|example
514
+ ---|---
515
+ `'boolean'`|`true`, `false`
516
+ `'number'`|`0`, `1`, `1.5`, ...
517
+ `'string'`|`'ejv'`, `'hello'`, ...
518
+ `'object'`|`{}`, `{ key : 123 }`, ...
519
+ `'date'`|`new Date`
520
+ `'regexp'`|`new RegExp(/./)`, `/./`, ...
521
+ `'array'`|`[]`, `[1, 2, 3]`, ...
522
+
523
+ ## `EjvError`
524
+
525
+ If the JSON object passes the validation rule, it returns the `null` object, but if it does not pass the inspection rule, it returns the instance of the `EjvError` type.
526
+ The `EjvError` object is an object that represents the error that occurred at this time.
527
+
528
+ > You do not always need to use `EjvError` type.
529
+ However, if you use TypeScript, you can use it to refer to the property of an error object.
530
+
531
+ - `type : ErrorType`
532
+
533
+ Represents the type of the error that occurred.
534
+
535
+ - `keyword : string`
536
+
537
+ Describes the contents of the error that occurred.
538
+
539
+ - `path : string`
540
+
541
+ Points to the location of the data where the error occurred.
542
+
543
+ - `data : any`
544
+
545
+ Means the data that passed to `ejv()`.
546
+
547
+ - `errorData : any`
548
+
549
+ Means the data that the error occurred.
550
+
551
+ usage)
552
+
553
+ ```typescript
554
+ import { ejv, EjvError } from 'ejv';
555
+
556
+ const error : null | EjvError = ejv({
557
+ a : 10
558
+ }, [{
559
+ key : 'a',
560
+ type : 'string'
561
+ }]);
562
+
563
+ console.log(error.type); // 'TYPE_MISMATCH'
564
+ console.log(error.message); // 'the value should be a string'
565
+ console.log(error.path); // 'a'
566
+ console.log(error.data); // { a : 10 }
567
+ console.log(error.errorData); // 10
568
+ ```
569
+
570
+ ## Options
571
+
572
+ When using a `ejv()` function, you can specify options as a third parameter.
573
+
574
+ - `customErrorMsg: object`
575
+
576
+ You can override error message corresponding with `EjvError.type` to another content.
577
+
578
+ This option is used in the type of `object`. You can use `ErrorType` as a key when overriding error message.
579
+
580
+ ```typescript
581
+ import { ejv, EjvError, ErrorType } from 'ejv';
582
+
583
+ const error : null | EjvError = ejv({
584
+ a : 10
585
+ }, [{
586
+ key : 'a',
587
+ type : 'string'
588
+ }, {
589
+ customErrorMsg : {
590
+ [ErrorType.TYPE_MISMATCH] : 'property "a" should be a "string".'
591
+ }
592
+ }]);
593
+
594
+ console.log(error.message); // 'property "a" should be a "string".'
595
+ ```