ejv 1.1.11 → 2.0.1

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 (75) hide show
  1. package/.eslintrc.json +88 -0
  2. package/.mocharc.json +8 -0
  3. package/CHANGELOG.md +109 -68
  4. package/LICENSE +21 -21
  5. package/README-KR.md +596 -592
  6. package/README.md +599 -595
  7. package/build/{constants.js → cjs/constants.js} +45 -46
  8. package/build/cjs/constants.js.map +1 -0
  9. package/build/cjs/ejv.js +1263 -0
  10. package/build/cjs/ejv.js.map +1 -0
  11. package/build/{public_api.js → cjs/index.js} +4 -4
  12. package/build/cjs/index.js.map +1 -0
  13. package/build/cjs/interfaces.js +29 -0
  14. package/build/cjs/interfaces.js.map +1 -0
  15. package/build/cjs/package.json +1 -0
  16. package/build/{tester.js → cjs/tester.js} +88 -83
  17. package/build/cjs/tester.js.map +1 -0
  18. package/build/cjs/util.js +103 -0
  19. package/build/cjs/util.js.map +1 -0
  20. package/build/constants.d.ts +37 -40
  21. package/build/esm/constants.js +115 -0
  22. package/build/esm/constants.js.map +1 -0
  23. package/build/esm/ejv.js +1261 -0
  24. package/build/esm/ejv.js.map +1 -0
  25. package/build/esm/index.js +4 -0
  26. package/build/esm/index.js.map +1 -0
  27. package/build/esm/interfaces.js +33 -0
  28. package/build/esm/interfaces.js.map +1 -0
  29. package/build/esm/package.json +1 -0
  30. package/build/esm/tester.js +240 -0
  31. package/build/esm/tester.js.map +1 -0
  32. package/build/esm/util.js +96 -0
  33. package/build/esm/util.js.map +1 -0
  34. package/build/index.d.ts +3 -0
  35. package/build/interfaces.d.ts +51 -13
  36. package/build/scripts/add-js-extensions.js +46 -0
  37. package/build/scripts/add-js-extensions.js.map +1 -0
  38. package/build/tester.d.ts +16 -17
  39. package/build/util.d.ts +7 -1
  40. package/package.json +50 -39
  41. package/scripts/add-js-extensions.ts +59 -0
  42. package/spec/ArrayScheme.ts +1021 -0
  43. package/spec/CommonScheme.ts +251 -0
  44. package/spec/DateScheme.ts +472 -0
  45. package/spec/NumberScheme.ts +1032 -0
  46. package/spec/ObjectScheme.ts +499 -0
  47. package/spec/RegExpScheme.ts +112 -0
  48. package/spec/StringScheme.ts +1239 -0
  49. package/spec/common-test-util.ts +63 -0
  50. package/spec/ejv.spec.ts +209 -4634
  51. package/spec/testers.spec.ts +833 -832
  52. package/src/constants.ts +155 -156
  53. package/src/ejv.ts +1648 -1071
  54. package/src/index.ts +14 -0
  55. package/src/interfaces.ts +145 -63
  56. package/src/tester.ts +308 -302
  57. package/src/util.ts +124 -59
  58. package/tsconfig.cjs.json +8 -0
  59. package/tsconfig.esm.json +7 -0
  60. package/tsconfig.json +22 -19
  61. package/tsconfig.scripts.json +14 -0
  62. package/tsconfig.types.json +9 -0
  63. package/build/constants.js.map +0 -1
  64. package/build/ejv.js +0 -687
  65. package/build/ejv.js.map +0 -1
  66. package/build/interfaces.js +0 -15
  67. package/build/interfaces.js.map +0 -1
  68. package/build/public_api.d.ts +0 -3
  69. package/build/public_api.js.map +0 -1
  70. package/build/tester.js.map +0 -1
  71. package/build/util.js +0 -68
  72. package/build/util.js.map +0 -1
  73. package/spec/common-test-runner.ts +0 -17
  74. package/src/public_api.ts +0 -3
  75. package/tsconfig.spec.json +0 -19
@@ -0,0 +1,499 @@
1
+ import { describe, it } from 'mocha';
2
+ import { expect } from 'chai';
3
+
4
+ import { ejv } from '../src/ejv';
5
+
6
+ import { EjvError, Scheme } from '../src/interfaces';
7
+ import { ErrorMsg, ErrorType } from '../src/constants';
8
+ import { createErrorMsg } from '../src/util';
9
+ import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
10
+
11
+
12
+ describe('ObjectScheme', () => {
13
+ describe('type', () => {
14
+ describe('mismatch', () => {
15
+ typeTesterArr
16
+ .filter((obj: TypeTester): boolean => {
17
+ return !['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
18
+ })
19
+ .forEach((obj: TypeTester): void => {
20
+ const data = {
21
+ a: obj.value
22
+ };
23
+
24
+ it(obj.type, () => {
25
+ const error: EjvError | null = ejv(data, [{
26
+ key: 'a',
27
+ type: 'object'
28
+ }]);
29
+
30
+ expect(error).to.be.instanceof(EjvError);
31
+
32
+ if (!error) {
33
+ throw new Error('spec failed');
34
+ }
35
+
36
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
37
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH, {
38
+ placeholders: ['object']
39
+ }));
40
+ expect(error.path).to.be.eql('a');
41
+ expect(error.data).to.be.deep.equal(data);
42
+ expect(error.errorData).to.be.eql(obj.value);
43
+ });
44
+ });
45
+
46
+ it('multiple types', () => {
47
+ const value = {};
48
+ const typeArr: string[] = ['boolean', 'number'];
49
+
50
+ const data = {
51
+ a: value
52
+ };
53
+
54
+ const error: EjvError | null = ejv(data, [{
55
+ key: 'a',
56
+ type: typeArr
57
+ }]);
58
+
59
+ expect(error).to.be.instanceof(EjvError);
60
+
61
+ if (!error) {
62
+ throw new Error('spec failed');
63
+ }
64
+
65
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
66
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH_ONE_OF, {
67
+ placeholders: [JSON.stringify(typeArr)]
68
+ }));
69
+ expect(error.path).to.be.eql('a');
70
+ expect(error.data).to.be.deep.equal(data);
71
+ expect(error.errorData).to.be.eql(value);
72
+ });
73
+ });
74
+
75
+ describe('match', () => {
76
+ it('optional', () => {
77
+ expect(ejv({
78
+ a: undefined
79
+ }, [{
80
+ key: 'a',
81
+ type: 'object',
82
+ optional: true
83
+ }])).to.be.null;
84
+ });
85
+
86
+ typeTesterArr
87
+ .filter((obj: TypeTester): boolean => {
88
+ return ['null', 'date', 'regexp', 'array', 'object'].includes(obj.type);
89
+ })
90
+ .forEach((obj: TypeTester): void => {
91
+ it(obj.type, () => {
92
+ expect(ejv({
93
+ a: obj.value
94
+ }, [{
95
+ key: 'a',
96
+ type: 'object'
97
+ }])).to.be.null;
98
+ });
99
+ });
100
+
101
+ it('single type', () => {
102
+ expect(ejv({
103
+ a: {
104
+ b: 1
105
+ }
106
+ }, [{
107
+ key: 'a',
108
+ type: 'object'
109
+ }])).to.be.null;
110
+ });
111
+
112
+ it('multiple types', () => {
113
+ expect(ejv({
114
+ a: {
115
+ b: 1
116
+ }
117
+ }, [{
118
+ key: 'a',
119
+ type: ['object', 'number']
120
+ }])).to.be.null;
121
+ });
122
+
123
+ it('multiple types', () => {
124
+ expect(ejv({
125
+ a: {
126
+ b: 1
127
+ }
128
+ }, [{
129
+ key: 'a',
130
+ type: ['number', 'object']
131
+ }])).to.be.null;
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('properties', () => {
137
+ describe('check parameter', () => {
138
+ const data = {
139
+ a: {
140
+ b: 1
141
+ }
142
+ };
143
+
144
+ it('undefined is ok', () => {
145
+ expect(ejv(data, [{
146
+ key: 'a',
147
+ type: 'object',
148
+ properties: undefined
149
+ }])).to.be.null;
150
+ });
151
+
152
+ it('null', () => {
153
+ const errorScheme: Scheme = {
154
+ key: 'a',
155
+ type: 'object',
156
+ properties: null as unknown as Scheme[]
157
+ };
158
+
159
+ checkSchemeError({
160
+ data,
161
+ errorScheme,
162
+ message: createErrorMsg(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY)
163
+ });
164
+ });
165
+
166
+ it('not array', () => {
167
+ const errorScheme: Scheme = {
168
+ key: 'a',
169
+ type: 'object',
170
+ properties: 'b' as unknown as Scheme[]
171
+ };
172
+
173
+ checkSchemeError({
174
+ data,
175
+ errorScheme,
176
+ message: createErrorMsg(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY)
177
+ });
178
+ });
179
+
180
+ it('empty array', () => {
181
+ const errorScheme: Scheme = {
182
+ key: 'a',
183
+ type: 'object',
184
+ properties: []
185
+ };
186
+
187
+ checkSchemeError({
188
+ data,
189
+ errorScheme,
190
+ message: createErrorMsg(ErrorMsg.PROPERTIES_SHOULD_HAVE_ITEMS)
191
+ });
192
+ });
193
+
194
+ it('not object array', () => {
195
+ const errorScheme: Scheme = {
196
+ key: 'a',
197
+ type: 'object',
198
+ properties: ['b'] as unknown as Scheme[]
199
+ };
200
+
201
+ checkSchemeError({
202
+ data,
203
+ errorScheme,
204
+ message: createErrorMsg(ErrorMsg.PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT)
205
+ });
206
+ });
207
+ });
208
+
209
+ it('with single type', () => {
210
+ const undefinedError: EjvError | null = ejv({
211
+ a: {
212
+ b: undefined
213
+ }
214
+ }, [{
215
+ key: 'a',
216
+ type: 'object',
217
+ properties: [{
218
+ key: 'b',
219
+ type: 'string'
220
+ }]
221
+ }]);
222
+
223
+ expect(undefinedError).to.be.instanceof(EjvError);
224
+
225
+ if (!undefinedError) {
226
+ throw new Error('spec failed');
227
+ }
228
+
229
+ expect(undefinedError.type).to.be.eql(ErrorType.REQUIRED);
230
+ expect(undefinedError.message).to.be.eql(createErrorMsg(ErrorMsg.REQUIRED));
231
+ expect(undefinedError.path).to.be.eql('a/b');
232
+
233
+ const nullError: EjvError | null = ejv({
234
+ a: null
235
+ }, [{
236
+ key: 'a',
237
+ type: 'object',
238
+ properties: [{
239
+ key: 'b',
240
+ type: 'string'
241
+ }]
242
+ }]);
243
+
244
+ expect(nullError).to.be.instanceof(EjvError);
245
+
246
+ if (!nullError) {
247
+ throw new Error('spec failed');
248
+ }
249
+
250
+ expect(nullError.type).to.be.eql(ErrorType.REQUIRED);
251
+ expect(nullError.message).to.be.eql(createErrorMsg(ErrorMsg.REQUIRED));
252
+ expect(nullError.path).to.be.eql('a');
253
+
254
+ const data = {
255
+ a: {
256
+ b: 1
257
+ }
258
+ };
259
+
260
+ const error: EjvError | null = ejv(data, [{
261
+ key: 'a',
262
+ type: 'object',
263
+ properties: [{
264
+ key: 'b',
265
+ type: 'string'
266
+ }]
267
+ }]);
268
+
269
+ expect(error).to.be.instanceof(EjvError);
270
+
271
+ if (!error) {
272
+ throw new Error('spec failed');
273
+ }
274
+
275
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
276
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH, {
277
+ placeholders: ['string']
278
+ }));
279
+ expect(error.path).to.be.eql('a/b');
280
+ expect(error.data).to.be.deep.equal(data);
281
+ expect(error.errorData).to.be.eql(1);
282
+
283
+ expect(ejv({
284
+ a: {
285
+ b: 1
286
+ }
287
+ }, [{
288
+ key: 'a',
289
+ type: 'object',
290
+ properties: [{
291
+ key: 'b',
292
+ type: 'number'
293
+ }]
294
+ }])).to.be.null;
295
+ });
296
+
297
+ it('with multiple types', () => {
298
+ const typeArr: string[] = ['string', 'boolean'];
299
+
300
+ const undefinedError: EjvError | null = ejv({
301
+ a: {
302
+ b: undefined
303
+ }
304
+ }, [{
305
+ key: 'a',
306
+ type: 'object',
307
+ properties: [{
308
+ key: 'b',
309
+ type: typeArr
310
+ }]
311
+ }]);
312
+
313
+ expect(undefinedError).to.be.instanceof(EjvError);
314
+
315
+ if (!undefinedError) {
316
+ throw new Error('spec failed');
317
+ }
318
+
319
+ expect(undefinedError.type).to.be.eql(ErrorType.REQUIRED);
320
+ expect(undefinedError.message).to.be.eql(createErrorMsg(ErrorMsg.REQUIRED));
321
+ expect(undefinedError.path).to.be.eql('a/b');
322
+
323
+ const data = {
324
+ a: {
325
+ b: 1
326
+ }
327
+ };
328
+
329
+ const error: EjvError | null = ejv(data, [{
330
+ key: 'a',
331
+ type: 'object',
332
+ properties: [{
333
+ key: 'b',
334
+ type: typeArr
335
+ }]
336
+ }]);
337
+
338
+ expect(error).to.be.instanceof(EjvError);
339
+
340
+ if (!error) {
341
+ throw new Error('spec failed');
342
+ }
343
+
344
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
345
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH_ONE_OF, {
346
+ placeholders: [JSON.stringify(typeArr)]
347
+ }));
348
+ expect(error.path).to.be.eql('a/b');
349
+ expect(error.data).to.be.deep.equal(data);
350
+ expect(error.errorData).to.be.eql(1);
351
+
352
+ expect(ejv({
353
+ a: {
354
+ b: 1
355
+ }
356
+ }, [{
357
+ key: 'a',
358
+ type: 'object',
359
+ properties: [{
360
+ key: 'b',
361
+ type: ['number', 'string']
362
+ }]
363
+ }])).to.be.null;
364
+
365
+ expect(ejv({
366
+ a: {
367
+ b: 1
368
+ }
369
+ }, [{
370
+ key: 'a',
371
+ type: 'object',
372
+ properties: [{
373
+ key: 'b',
374
+ type: ['string', 'number']
375
+ }]
376
+ }])).to.be.null;
377
+ });
378
+ });
379
+
380
+ describe('allowNoProperty', () => {
381
+ describe('check parameter', () => {
382
+ const data = {
383
+ a: {
384
+ b: 1
385
+ }
386
+ };
387
+ it('undefined is ok', () => {
388
+ expect(ejv(data, [{
389
+ key: 'a',
390
+ type: 'object',
391
+ allowNoProperty: undefined
392
+ }])).to.be.null;
393
+ });
394
+
395
+ it('null', () => {
396
+ const errorScheme: Scheme = {
397
+ key: 'a',
398
+ type: 'object',
399
+ allowNoProperty: null as unknown as boolean
400
+ };
401
+
402
+ checkSchemeError({
403
+ data,
404
+ errorScheme,
405
+ message: createErrorMsg(ErrorMsg.ALLOW_NO_PROPERTY_SHOULD_BE_BOOLEAN)
406
+ });
407
+ });
408
+ });
409
+
410
+ describe('default', () => {
411
+ it('empty object', () => {
412
+ const error: EjvError | null = ejv({
413
+ a: {}
414
+ }, [{
415
+ key: 'a',
416
+ type: 'object'
417
+ }]);
418
+
419
+ expect(error).to.be.null;
420
+ });
421
+
422
+ it('object has property', () => {
423
+ const error: EjvError | null = ejv({
424
+ a: {
425
+ b: 1
426
+ }
427
+ }, [{
428
+ key: 'a',
429
+ type: 'object'
430
+ }]);
431
+
432
+ expect(error).to.be.null;
433
+ });
434
+ });
435
+
436
+ describe('allowNoProperty === false', () => {
437
+ it('empty object', () => {
438
+ const error: EjvError | null = ejv({
439
+ a: {}
440
+ }, [{
441
+ key: 'a',
442
+ type: 'object',
443
+ allowNoProperty: false
444
+ }]);
445
+
446
+ expect(error).to.be.instanceof(EjvError);
447
+
448
+ if (!error) {
449
+ throw new Error('spec failed');
450
+ }
451
+
452
+ expect(error.type).to.be.eql(ErrorType.PROPERTY);
453
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.PROPERTY));
454
+ expect(error.path).to.be.eql('a');
455
+ expect(error.data).to.be.a('object');
456
+ });
457
+
458
+ it('object has property', () => {
459
+ expect(ejv({
460
+ a: {
461
+ b: 1
462
+ }
463
+ }, [{
464
+ key: 'a',
465
+ type: 'object',
466
+ allowNoProperty: false
467
+ }])).to.be.null;
468
+ });
469
+ });
470
+
471
+ describe('allowNoProperty === true', () => {
472
+ it('empty object', () => {
473
+ const error: EjvError | null = ejv({
474
+ a: {}
475
+ }, [{
476
+ key: 'a',
477
+ type: 'object',
478
+ allowNoProperty: true
479
+ }]);
480
+
481
+ expect(error).to.be.null;
482
+ });
483
+
484
+ it('object has property', () => {
485
+ const error: EjvError | null = ejv({
486
+ a: {
487
+ b: 1
488
+ }
489
+ }, [{
490
+ key: 'a',
491
+ type: 'object',
492
+ allowNoProperty: true
493
+ }]);
494
+
495
+ expect(error).to.be.null;
496
+ });
497
+ });
498
+ });
499
+ });
@@ -0,0 +1,112 @@
1
+ import { describe, it } from 'mocha';
2
+ import { expect } from 'chai';
3
+
4
+ import { ejv } from '../src/ejv';
5
+
6
+ import { EjvError } from '../src/interfaces';
7
+ import { ErrorMsg, ErrorType } from '../src/constants';
8
+ import { createErrorMsg } from '../src/util';
9
+ import { TypeTester, typeTesterArr } from './common-test-util';
10
+
11
+
12
+ describe('RegExpScheme', () => {
13
+ describe('type', () => {
14
+ describe('mismatch', () => {
15
+ typeTesterArr
16
+ .filter((obj: TypeTester): boolean => obj.type !== 'regexp')
17
+ .forEach((obj: TypeTester): void => {
18
+ it(obj.type, () => {
19
+ const testData = {
20
+ a: obj.value
21
+ };
22
+
23
+ const error: EjvError | null = ejv(testData, [{
24
+ key: 'a',
25
+ type: 'regexp'
26
+ }]);
27
+
28
+ expect(error).to.be.instanceof(EjvError);
29
+
30
+ if (!error) {
31
+ throw new Error('spec failed');
32
+ }
33
+
34
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH);
35
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH, {
36
+ placeholders: ['regexp']
37
+ }));
38
+ expect(error.path).to.be.eql('a');
39
+ expect(error.data).to.be.deep.equal(testData);
40
+ expect(error.errorData).to.be.eql(obj.value);
41
+ });
42
+ });
43
+
44
+ it('multiple types', () => {
45
+ const value = 'ejv';
46
+ const typeArr: string[] = ['boolean', 'regexp'];
47
+
48
+ const testData = {
49
+ a: value
50
+ };
51
+
52
+ const error: EjvError | null = ejv(testData, [{
53
+ key: 'a',
54
+ type: typeArr
55
+ }]);
56
+
57
+ expect(error).to.be.instanceof(EjvError);
58
+
59
+ if (!error) {
60
+ throw new Error('spec failed');
61
+ }
62
+
63
+ expect(error.type).to.be.eql(ErrorType.TYPE_MISMATCH_ONE_OF);
64
+ expect(error.message).to.be.eql(createErrorMsg(ErrorMsg.TYPE_MISMATCH_ONE_OF, {
65
+ placeholders: [JSON.stringify(typeArr)]
66
+ }));
67
+ expect(error.path).to.be.eql('a');
68
+ expect(error.data).to.be.deep.equal(testData);
69
+ expect(error.errorData).to.be.eql(value);
70
+ });
71
+ });
72
+
73
+ describe('match', () => {
74
+ it('optional', () => {
75
+ expect(ejv({
76
+ a: undefined
77
+ }, [{
78
+ key: 'a',
79
+ type: 'regexp',
80
+ optional: true
81
+ }])).to.be.null;
82
+ });
83
+
84
+ it('single type', () => {
85
+ expect(ejv({
86
+ a: /./
87
+ }, [{
88
+ key: 'a',
89
+ type: 'regexp'
90
+ }])).to.be.null;
91
+ });
92
+
93
+ it('multiple types', () => {
94
+ expect(ejv({
95
+ a: /./
96
+ }, [{
97
+ key: 'a',
98
+ type: ['regexp', 'number']
99
+ }])).to.be.null;
100
+ });
101
+
102
+ it('multiple types', () => {
103
+ expect(ejv({
104
+ a: /./
105
+ }, [{
106
+ key: 'a',
107
+ type: ['number', 'regexp']
108
+ }])).to.be.null;
109
+ });
110
+ });
111
+ });
112
+ });