ejv 2.1.4 → 2.1.5

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.
@@ -6,13 +6,13 @@ import { ejv } from '../src/ejv';
6
6
  import { EjvError, Scheme } from '../src/interfaces';
7
7
  import { ERROR_MESSAGE, ERROR_TYPE } from '../src/constants';
8
8
  import { createErrorMsg } from '../src/util';
9
- import { checkSchemeError, TypeTester, typeTesterArr } from './common-test-util';
9
+ import { checkSchemeError, TypeTester, TYPE_TESTER_ARR } from './common-test-util';
10
10
 
11
11
 
12
12
  describe('StringScheme', () => {
13
13
  describe('type', () => {
14
14
  describe('mismatch', () => {
15
- typeTesterArr
15
+ TYPE_TESTER_ARR
16
16
  .filter((obj: TypeTester): boolean => obj.type !== 'string')
17
17
  .forEach((obj: TypeTester): void => {
18
18
  const data = {
@@ -129,7 +129,8 @@ describe('StringScheme', () => {
129
129
  const errorScheme: Scheme = {
130
130
  key: 'a',
131
131
  type: 'string',
132
- enum: null as unknown as string[]
132
+ // @ts-expect-error: null
133
+ enum: null
133
134
  };
134
135
 
135
136
  checkSchemeError({
@@ -143,7 +144,8 @@ describe('StringScheme', () => {
143
144
  const errorScheme: Scheme = {
144
145
  key: 'a',
145
146
  type: 'string',
146
- enum: 'a' as unknown as string[]
147
+ // @ts-expect-error: type mismatch
148
+ enum: 'a'
147
149
  };
148
150
 
149
151
  checkSchemeError({
@@ -225,7 +227,8 @@ describe('StringScheme', () => {
225
227
  const errorScheme: Scheme = {
226
228
  key: 'a',
227
229
  type: 'string',
228
- notEnum: null as unknown as string[]
230
+ // @ts-expect-error: null
231
+ notEnum: null
229
232
  };
230
233
 
231
234
  checkSchemeError({
@@ -239,7 +242,8 @@ describe('StringScheme', () => {
239
242
  const errorScheme: Scheme = {
240
243
  key: 'a',
241
244
  type: 'string',
242
- notEnum: 'a' as unknown as string[]
245
+ // @ts-expect-error: type mismatch
246
+ notEnum: 'a'
243
247
  };
244
248
 
245
249
  checkSchemeError({
@@ -322,7 +326,8 @@ describe('StringScheme', () => {
322
326
  const errorScheme: Scheme = {
323
327
  key: 'a',
324
328
  type: 'string',
325
- length: null as unknown as number
329
+ // @ts-expect-error: null
330
+ length: null
326
331
  };
327
332
 
328
333
  checkSchemeError({
@@ -336,7 +341,8 @@ describe('StringScheme', () => {
336
341
  const errorScheme: Scheme = {
337
342
  key: 'a',
338
343
  type: 'string',
339
- length: '3' as unknown as number
344
+ // @ts-expect-error: type mismatch
345
+ length: '3'
340
346
  };
341
347
 
342
348
  checkSchemeError({
@@ -402,7 +408,8 @@ describe('StringScheme', () => {
402
408
  const errorScheme: Scheme = {
403
409
  key: 'a',
404
410
  type: 'string',
405
- minLength: null as unknown as number
411
+ // @ts-expect-error: type mismatch
412
+ minLength: null
406
413
  };
407
414
 
408
415
  checkSchemeError({
@@ -430,7 +437,8 @@ describe('StringScheme', () => {
430
437
  const errorScheme: Scheme = {
431
438
  key: 'a',
432
439
  type: 'string',
433
- minLength: '1' as unknown as number
440
+ // @ts-expect-error: type mismatch
441
+ minLength: '1'
434
442
  };
435
443
 
436
444
  checkSchemeError({
@@ -504,7 +512,8 @@ describe('StringScheme', () => {
504
512
  const errorScheme: Scheme = {
505
513
  key: 'a',
506
514
  type: 'string',
507
- maxLength: null as unknown as number
515
+ // @ts-expect-error: null
516
+ maxLength: null
508
517
  };
509
518
 
510
519
  checkSchemeError({
@@ -532,7 +541,8 @@ describe('StringScheme', () => {
532
541
  const errorScheme: Scheme = {
533
542
  key: 'a',
534
543
  type: 'string',
535
- maxLength: '1' as unknown as number
544
+ // @ts-expect-error: type mismatch
545
+ maxLength: '1'
536
546
  };
537
547
 
538
548
  checkSchemeError({
@@ -606,7 +616,8 @@ describe('StringScheme', () => {
606
616
  const errorScheme: Scheme = {
607
617
  key: 'a',
608
618
  type: 'string',
609
- format: null as unknown as string
619
+ // @ts-expect-error: null
620
+ format: null
610
621
  };
611
622
 
612
623
  checkSchemeError({
@@ -1064,7 +1075,8 @@ describe('StringScheme', () => {
1064
1075
  const errorScheme: Scheme = {
1065
1076
  key: 'a',
1066
1077
  type: 'string',
1067
- pattern: null as unknown as string
1078
+ // @ts-expect-error: null
1079
+ pattern: null
1068
1080
  };
1069
1081
 
1070
1082
  checkSchemeError({
@@ -1080,7 +1092,8 @@ describe('StringScheme', () => {
1080
1092
  const errorScheme: Scheme = {
1081
1093
  key: 'a',
1082
1094
  type: 'string',
1083
- pattern: 1 as unknown as string
1095
+ // @ts-expect-error: type mismatch
1096
+ pattern: 1
1084
1097
  };
1085
1098
 
1086
1099
  checkSchemeError({
@@ -1128,7 +1141,8 @@ describe('StringScheme', () => {
1128
1141
  const errorScheme: Scheme = {
1129
1142
  key: 'a',
1130
1143
  type: 'string',
1131
- pattern: [null as unknown as RegExp, /ab/]
1144
+ // @ts-expect-error: type mismatch
1145
+ pattern: [null, /ab/]
1132
1146
  };
1133
1147
 
1134
1148
  checkSchemeError({
@@ -1144,7 +1158,8 @@ describe('StringScheme', () => {
1144
1158
  const errorScheme: Scheme = {
1145
1159
  key: 'a',
1146
1160
  type: 'string',
1147
- pattern: [1, 3] as unknown as string[]
1161
+ // @ts-expect-error: type mismatch
1162
+ pattern: [1, 3]
1148
1163
  };
1149
1164
 
1150
1165
  checkSchemeError({
@@ -1192,7 +1207,8 @@ describe('StringScheme', () => {
1192
1207
  const errorScheme: Scheme = {
1193
1208
  key: 'a',
1194
1209
  type: 'string',
1195
- pattern: new RegExp(null as unknown as string)
1210
+ // @ts-expect-error: type mismatch
1211
+ pattern: new RegExp(null)
1196
1212
  };
1197
1213
 
1198
1214
  checkSchemeError({
@@ -6,7 +6,7 @@ import { EjvError, Scheme } from '../src/interfaces';
6
6
  import { ejv } from '../src/ejv';
7
7
  import { ERROR_TYPE } from '../src/constants';
8
8
 
9
- export const commonTestRunner = (
9
+ export function commonTestRunner (
10
10
  testFnc: (args: unknown) => unknown,
11
11
  nullResult: boolean,
12
12
  undefinedResult: boolean,
@@ -14,38 +14,41 @@ export const commonTestRunner = (
14
14
  numberResult: boolean,
15
15
  stringResult: boolean,
16
16
  arrayResult: boolean,
17
- objectResult: boolean
18
- ): boolean => {
17
+ objectResult: boolean,
18
+ bufferResult: boolean
19
+ ): boolean {
19
20
  return testFnc(null) === nullResult
20
21
  && testFnc(undefined) === undefinedResult
21
22
  && testFnc(true) === booleanResult
22
23
  && testFnc(8) === numberResult
23
24
  && testFnc('hello') === stringResult
24
25
  && testFnc([1, 2, 3]) === arrayResult
25
- && testFnc({ a: 1 }) === objectResult;
26
- };
26
+ && testFnc({ a: 1 }) === objectResult
27
+ && testFnc(new Uint8Array(3)) === bufferResult;
28
+ }
27
29
 
28
30
  export interface TypeTester {
29
31
  type: string;
30
32
  value: unknown;
31
33
  }
32
34
 
33
- export const typeTesterArr: TypeTester[] = [
35
+ export const TYPE_TESTER_ARR: TypeTester[] = [
34
36
  { type: 'boolean', value: true },
35
37
  { type: 'number', value: 123 },
36
38
  { type: 'string', value: 'ejv' },
37
39
  { type: 'object', value: {} },
38
40
  { type: 'date', value: new Date },
39
41
  { type: 'regexp', value: new RegExp('ejv') },
40
- { type: 'array', value: [1, 2, 3] }
42
+ { type: 'array', value: [1, 2, 3] },
43
+ { type: 'buffer', value: new Uint8Array(5) }
41
44
  ];
42
45
 
43
46
 
44
- export const checkSchemeError = (param: {
47
+ export function checkSchemeError (param: {
45
48
  data: object,
46
49
  errorScheme: Scheme,
47
50
  message: string,
48
- }): void => {
51
+ }): void {
49
52
  const ejvError: EjvError | null = ejv(param.data, [param.errorScheme]);
50
53
 
51
54
  expect(ejvError).to.be.instanceOf(EjvError);
@@ -60,4 +63,4 @@ export const checkSchemeError = (param: {
60
63
 
61
64
  expect(ejvError).to.have.property('isSchemeError', true);
62
65
  expect(ejvError).to.have.property('isDataError', false);
63
- };
66
+ }
@@ -7,6 +7,7 @@ import {
7
7
  arrayTester,
8
8
  arrayTypeOfTester,
9
9
  booleanTester,
10
+ bufferTester,
10
11
  dateFormatTester,
11
12
  dateTester,
12
13
  dateTimeFormatTester,
@@ -49,6 +50,7 @@ describe('testers', function () {
49
50
  true,
50
51
  true,
51
52
  true,
53
+ true,
52
54
  true
53
55
  )).to.be.true;
54
56
  });
@@ -112,6 +114,7 @@ describe('testers', function () {
112
114
  false,
113
115
  false,
114
116
  false,
117
+ false,
115
118
  false
116
119
  )).to.be.true;
117
120
  });
@@ -140,6 +143,7 @@ describe('testers', function () {
140
143
  true,
141
144
  false,
142
145
  false,
146
+ false,
143
147
  false
144
148
  )).to.be.true;
145
149
  });
@@ -257,6 +261,7 @@ describe('testers', function () {
257
261
  false,
258
262
  true,
259
263
  false,
264
+ false,
260
265
  false
261
266
  )).to.be.true;
262
267
  });
@@ -603,7 +608,8 @@ describe('testers', function () {
603
608
  false,
604
609
  false,
605
610
  true, // array is object
606
- true
611
+ true,
612
+ true // buffer is object
607
613
  )).to.be.true;
608
614
  });
609
615
 
@@ -644,6 +650,7 @@ describe('testers', function () {
644
650
  false,
645
651
  false,
646
652
  false,
653
+ false,
647
654
  false
648
655
  )).to.be.true;
649
656
  });
@@ -778,6 +785,7 @@ describe('testers', function () {
778
785
  false,
779
786
  false,
780
787
  true,
788
+ false,
781
789
  false
782
790
  )).to.be.true;
783
791
  });
@@ -818,6 +826,7 @@ describe('testers', function () {
818
826
  false,
819
827
  false,
820
828
  false,
829
+ false,
821
830
  false
822
831
  )).to.be.true;
823
832
  });
@@ -830,4 +839,29 @@ describe('testers', function () {
830
839
  });
831
840
  });
832
841
  });
842
+
843
+ describe('buffer', () => {
844
+ describe('bufferTester()', () => {
845
+ it('common test', () => {
846
+ expect(commonTestRunner(
847
+ bufferTester,
848
+ false,
849
+ false,
850
+ false,
851
+ false,
852
+ false,
853
+ false,
854
+ false,
855
+ true
856
+ )).to.be.true;
857
+ });
858
+
859
+ it('logic test', () => {
860
+ expect(bufferTester(new Uint8Array)).to.be.true;
861
+ expect(bufferTester(new Uint16Array)).to.be.true;
862
+ expect(bufferTester(new Uint32Array)).to.be.true;
863
+ expect(bufferTester(Buffer.from('hello'))).to.be.true;
864
+ });
865
+ });
866
+ });
833
867
  });
package/src/constants.ts CHANGED
@@ -5,7 +5,8 @@ export enum DATA_TYPE {
5
5
  OBJECT = 'object',
6
6
  DATE = 'date',
7
7
  REGEXP = 'regexp',
8
- ARRAY = 'array'
8
+ ARRAY = 'array',
9
+ BUFFER = 'buffer'
9
10
  }
10
11
 
11
12
  export enum NUMBER_FORMAT {
@@ -58,6 +59,10 @@ export enum ERROR_TYPE {
58
59
  MIN_LENGTH = 'MIN_LENGTH',
59
60
  MAX_LENGTH = 'MAX_LENGTH',
60
61
 
62
+ BYTE_LENGTH = 'BYTE_LENGTH',
63
+ MIN_BYTE_LENGTH = 'MIN_BYTE_LENGTH',
64
+ MAX_BYTE_LENGTH = 'MAX_BYTE_LENGTH',
65
+
61
66
  PATTERN = 'PATTERN',
62
67
  PATTERN_ONE_OF = 'PATTERN_ONE_OF',
63
68
 
@@ -110,6 +115,10 @@ export enum ERROR_MESSAGE {
110
115
  MIN_LENGTH_SHOULD_BE_INTEGER = 'minLength should be a integer',
111
116
  MAX_LENGTH_SHOULD_BE_INTEGER = 'maxLength should be a integer',
112
117
 
118
+ BYTE_LENGTH_SHOULD_BE_INTEGER = 'byteLength should be a integer',
119
+ MIN_BYTE_LENGTH_SHOULD_BE_INTEGER = 'minByteLength should be a integer',
120
+ MAX_BYTE_LENGTH_SHOULD_BE_INTEGER = 'maxByteLength should be a integer',
121
+
113
122
  PROPERTIES_SHOULD_BE_ARRAY = 'properties should be array',
114
123
  PROPERTIES_SHOULD_HAVE_ITEMS = 'properties should have items',
115
124
  PROPERTIES_SHOULD_BE_ARRAY_OF_OBJECT = 'properties should be array of object',
@@ -145,6 +154,10 @@ export enum ERROR_MESSAGE {
145
154
  MIN_LENGTH = 'the value should be longer than <<1>>', // minLength
146
155
  MAX_LENGTH = 'the value should be shorter than <<1>>', // maxLength
147
156
 
157
+ BYTE_LENGTH = 'the byte length of value should be <<1>>', // length
158
+ MIN_BYTE_LENGTH = 'the byte length of value should be longer than <<1>>', // minLength
159
+ MAX_BYTE_LENGTH = 'the byte length of value should be shorter than <<1>>', // maxLength
160
+
148
161
  PATTERN = 'the value should be pattern of <<1>>', // pattern
149
162
  PATTERN_ONE_OF = 'the value should be one of pattern of <<1>>', // pattern
150
163
 
package/src/ejv.ts CHANGED
@@ -2,6 +2,8 @@ import {
2
2
  AllDataType,
3
3
  AnyObject,
4
4
  ArrayScheme,
5
+ BufferLike,
6
+ BufferScheme,
5
7
  DateScheme,
6
8
  EjvError,
7
9
  InternalOptions,
@@ -17,6 +19,7 @@ import {
17
19
  arrayTester,
18
20
  arrayTypeOfTester,
19
21
  booleanTester,
22
+ bufferLengthTester,
20
23
  dateFormatTester,
21
24
  dateTester,
22
25
  dateTimeFormatTester,
@@ -32,9 +35,11 @@ import {
32
35
  integerTester,
33
36
  jsonStrTester,
34
37
  lengthTester,
38
+ maxByteLengthTester,
35
39
  maxDateTester,
36
40
  maxLengthTester,
37
41
  maxNumberTester,
42
+ minByteLengthTester,
38
43
  minDateTester,
39
44
  minLengthTester,
40
45
  minNumberTester,
@@ -1695,6 +1700,108 @@ const _ejv = <T> (data: T, schemes: Scheme[], options: InternalOptions): null |
1695
1700
  }
1696
1701
  break;
1697
1702
  }
1703
+
1704
+ case DATA_TYPE.BUFFER: {
1705
+ const valueAsBuffer: BufferLike = value as unknown as BufferLike;
1706
+ const bufferScheme: BufferScheme = scheme as BufferScheme;
1707
+
1708
+ if (definedTester(bufferScheme.byteLength)) {
1709
+ const length: number = bufferScheme.byteLength;
1710
+
1711
+ if (!(numberTester(length) && integerTester(length))) {
1712
+ return new EjvError({
1713
+ type: ERROR_TYPE.INVALID_SCHEMES,
1714
+ message: createErrorMsg(ERROR_MESSAGE.BYTE_LENGTH_SHOULD_BE_INTEGER),
1715
+
1716
+ data: data,
1717
+
1718
+ errorScheme: bufferScheme,
1719
+ isSchemeError: true
1720
+ });
1721
+ }
1722
+
1723
+ if (!bufferLengthTester(valueAsBuffer, length)) {
1724
+ result = new EjvError({
1725
+ type: ERROR_TYPE.BYTE_LENGTH,
1726
+ message: createErrorMsg(ERROR_MESSAGE.BYTE_LENGTH, {
1727
+ placeholders: ['' + length]
1728
+ }),
1729
+
1730
+ data,
1731
+ path: _options.path,
1732
+
1733
+ errorScheme: bufferScheme,
1734
+ errorData: value
1735
+ });
1736
+ break;
1737
+ }
1738
+ }
1739
+
1740
+ if (definedTester(bufferScheme.minByteLength)) {
1741
+ const minLength: number = bufferScheme.minByteLength;
1742
+
1743
+ if (!(numberTester(bufferScheme.minByteLength) && integerTester(minLength))) {
1744
+ return new EjvError({
1745
+ type: ERROR_TYPE.INVALID_SCHEMES,
1746
+ message: createErrorMsg(ERROR_MESSAGE.MIN_BYTE_LENGTH_SHOULD_BE_INTEGER),
1747
+
1748
+ data: data,
1749
+
1750
+ errorScheme: bufferScheme,
1751
+ isSchemeError: true
1752
+ });
1753
+ }
1754
+
1755
+ if (!minByteLengthTester(valueAsBuffer, minLength)) {
1756
+ result = new EjvError({
1757
+ type: ERROR_TYPE.MIN_BYTE_LENGTH,
1758
+ message: createErrorMsg(ERROR_MESSAGE.MIN_BYTE_LENGTH, {
1759
+ placeholders: ['' + minLength]
1760
+ }),
1761
+
1762
+ data,
1763
+ path: _options.path,
1764
+
1765
+ errorScheme: bufferScheme,
1766
+ errorData: value
1767
+ });
1768
+ break;
1769
+ }
1770
+ }
1771
+
1772
+ if (definedTester(bufferScheme.maxByteLength)) {
1773
+ const maxLength: number = bufferScheme.maxByteLength;
1774
+
1775
+ if (!(numberTester(bufferScheme.maxByteLength) && integerTester(maxLength))) {
1776
+ return new EjvError({
1777
+ type: ERROR_TYPE.INVALID_SCHEMES,
1778
+ message: createErrorMsg(ERROR_MESSAGE.MAX_BYTE_LENGTH_SHOULD_BE_INTEGER),
1779
+
1780
+ data: data,
1781
+
1782
+ errorScheme: bufferScheme,
1783
+ isSchemeError: true
1784
+ });
1785
+ }
1786
+
1787
+ if (!maxByteLengthTester(valueAsBuffer, maxLength)) {
1788
+ result = new EjvError({
1789
+ type: ERROR_TYPE.MAX_BYTE_LENGTH,
1790
+ message: createErrorMsg(ERROR_MESSAGE.MAX_BYTE_LENGTH, {
1791
+ placeholders: ['' + maxLength]
1792
+ }),
1793
+
1794
+ data,
1795
+ path: _options.path,
1796
+
1797
+ errorScheme: bufferScheme,
1798
+ errorData: value
1799
+ });
1800
+ break;
1801
+ }
1802
+ }
1803
+ break;
1804
+ }
1698
1805
  }
1699
1806
 
1700
1807
  if (result) {
@@ -1720,7 +1827,7 @@ const _ejv = <T> (data: T, schemes: Scheme[], options: InternalOptions): null |
1720
1827
  return result;
1721
1828
  };
1722
1829
 
1723
- export const ejv = (data: object, schemes: Scheme[], options?: Options): null | EjvError => {
1830
+ export function ejv (data: object, schemes: Scheme[], options?: Options): null | EjvError {
1724
1831
  // check data itself
1725
1832
  if (!definedTester(data) || !objectTester(data) || data === null) {
1726
1833
  return new EjvError({
@@ -1748,4 +1855,4 @@ export const ejv = (data: object, schemes: Scheme[], options?: Options): null |
1748
1855
  }
1749
1856
 
1750
1857
  return _ejv(data, schemes, internalOption);
1751
- };
1858
+ }
package/src/interfaces.ts CHANGED
@@ -66,6 +66,17 @@ export interface ArrayScheme extends CommonScheme {
66
66
  maxLength?: number;
67
67
  }
68
68
 
69
+
70
+ export type BufferLike = ArrayBuffer
71
+ | SharedArrayBuffer
72
+ | Exclude<ArrayBufferView, DataView>;
73
+
74
+ export interface BufferScheme extends CommonScheme {
75
+ byteLength?: number;
76
+ minByteLength?: number;
77
+ maxByteLength?: number;
78
+ }
79
+
69
80
  export type Scheme =
70
81
  BooleanScheme
71
82
  | NumberScheme
@@ -73,7 +84,8 @@ export type Scheme =
73
84
  | ObjectScheme
74
85
  | DateScheme
75
86
  | RegExpScheme
76
- | ArrayScheme;
87
+ | ArrayScheme
88
+ | BufferScheme;
77
89
 
78
90
 
79
91
  export interface Options {