ejv 2.1.5 → 2.1.6
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/LICENSE +21 -21
- package/README-KR.md +604 -650
- package/README.md +608 -655
- package/build/cjs/{constants.js → src/constants.js} +0 -10
- package/build/cjs/src/constants.js.map +1 -0
- package/build/cjs/{ejv.js → src/ejv.js} +8 -87
- package/build/cjs/src/ejv.js.map +1 -0
- package/build/cjs/{index.js → src/index.js} +3 -3
- package/build/cjs/src/index.js.map +1 -0
- package/build/{esm → cjs/src}/interfaces.js.map +1 -1
- package/build/cjs/{tester.js → src/tester.js} +126 -146
- package/build/cjs/src/tester.js.map +1 -0
- package/build/cjs/{util.js → src/util.js} +16 -15
- package/build/cjs/src/util.js.map +1 -0
- package/build/esm/{constants.js → src/constants.js} +0 -10
- package/build/esm/src/constants.js.map +1 -0
- package/build/esm/{ejv.js → src/ejv.js} +6 -86
- package/build/esm/src/ejv.js.map +1 -0
- package/build/esm/src/index.js +4 -0
- package/build/esm/src/index.js.map +1 -0
- package/build/esm/src/interfaces.js.map +1 -0
- package/build/esm/{tester.js → src/tester.js} +77 -94
- package/build/esm/src/tester.js.map +1 -0
- package/build/esm/{util.js → src/util.js} +8 -8
- package/build/esm/src/util.js.map +1 -0
- package/build/{constants.d.ts → src/constants.d.ts} +1 -11
- package/build/src/ejv.d.ts +2 -0
- package/build/{interfaces.d.ts → src/interfaces.d.ts} +1 -7
- package/build/src/tester.d.ts +35 -0
- package/build/src/util.d.ts +7 -0
- package/eslint.config.mjs +0 -1
- package/package.json +55 -55
- package/spec/ArrayScheme.ts +12 -113
- package/spec/DateScheme.ts +8 -12
- package/spec/NumberScheme.ts +14 -23
- package/spec/ObjectScheme.ts +9 -12
- package/spec/RegExpScheme.ts +2 -2
- package/spec/StringScheme.ts +18 -34
- package/spec/common-test-util.ts +10 -13
- package/spec/testers.spec.ts +1 -35
- package/src/constants.ts +1 -14
- package/src/ejv.ts +2 -109
- package/src/interfaces.ts +1 -13
- package/src/tester.ts +77 -99
- package/src/util.ts +9 -9
- package/tsconfig.json +8 -2
- package/tsconfig.scripts.json +1 -1
- package/build/cjs/constants.js.map +0 -1
- package/build/cjs/ejv.js.map +0 -1
- package/build/cjs/index.js.map +0 -1
- package/build/cjs/interfaces.js.map +0 -1
- package/build/cjs/tester.js.map +0 -1
- package/build/cjs/util.js.map +0 -1
- package/build/ejv.d.ts +0 -2
- package/build/esm/constants.js.map +0 -1
- package/build/esm/ejv.js.map +0 -1
- package/build/esm/index.js +0 -4
- package/build/esm/index.js.map +0 -1
- package/build/esm/tester.js.map +0 -1
- package/build/esm/util.js.map +0 -1
- package/build/tester.d.ts +0 -39
- package/build/util.d.ts +0 -7
- package/spec/BufferScheme.ts +0 -406
- /package/build/cjs/{interfaces.js → src/interfaces.js} +0 -0
- /package/build/esm/{interfaces.js → src/interfaces.js} +0 -0
- /package/build/{index.d.ts → src/index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,655 +1,608 @@
|
|
|
1
|
-

|
|
2
|
-

|
|
3
|
-
|
|
4
|
-
[🇰🇷 한국어](https://github.com/han41858/ejv/blob/master/README-KR.md)
|
|
5
|
-
|
|
6
|
-
# ejv - Easy JSON Validator
|
|
7
|
-
|
|
8
|
-

|
|
9
|
-

|
|
10
|
-

|
|
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
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
Means the data that the error occurred.
|
|
610
|
-
|
|
611
|
-
usage)
|
|
612
|
-
|
|
613
|
-
```typescript
|
|
614
|
-
import { ejv, EjvError } from 'ejv';
|
|
615
|
-
|
|
616
|
-
const error: null | EjvError = ejv({
|
|
617
|
-
a: 10
|
|
618
|
-
}, [{
|
|
619
|
-
key: 'a',
|
|
620
|
-
type: 'string'
|
|
621
|
-
}]);
|
|
622
|
-
|
|
623
|
-
console.log(error.type); // 'TYPE_MISMATCH'
|
|
624
|
-
console.log(error.message); // 'the value should be a string'
|
|
625
|
-
console.log(error.path); // 'a'
|
|
626
|
-
console.log(error.data); // { a : 10 }
|
|
627
|
-
console.log(error.errorData); // 10
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
## Options
|
|
631
|
-
|
|
632
|
-
When using a `ejv()` function, you can specify options as a third parameter.
|
|
633
|
-
|
|
634
|
-
- `customErrorMsg: object`
|
|
635
|
-
|
|
636
|
-
You can override error message corresponding with `EjvError.type` to another content.
|
|
637
|
-
|
|
638
|
-
This option is used in the type of `object`. You can use `ErrorType` as a key when overriding error message.
|
|
639
|
-
|
|
640
|
-
```typescript
|
|
641
|
-
import { ejv, EjvError, ErrorType } from 'ejv';
|
|
642
|
-
|
|
643
|
-
const error: null | EjvError = ejv({
|
|
644
|
-
a: 10
|
|
645
|
-
}, [{
|
|
646
|
-
key: 'a',
|
|
647
|
-
type: 'string'
|
|
648
|
-
}, {
|
|
649
|
-
customErrorMsg: {
|
|
650
|
-
[ErrorType.TYPE_MISMATCH]: 'property "a" should be a "string".'
|
|
651
|
-
}
|
|
652
|
-
}]);
|
|
653
|
-
|
|
654
|
-
console.log(error.message); // 'property "a" should be a "string".'
|
|
655
|
-
```
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
|
|
4
|
+
[🇰🇷 한국어](https://github.com/han41858/ejv/blob/master/README-KR.md)
|
|
5
|
+
|
|
6
|
+
# ejv - Easy JSON Validator
|
|
7
|
+
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
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
|
+
```
|