ejv 2.1.1 → 2.1.3

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