@litert/typeguard 1.4.0 → 1.4.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.
@@ -1,469 +0,0 @@
1
- /**
2
- * Copyright 2023 Angus Fenying <fenying@litert.org>
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import { createTestDefinition, defaultItems, ITestSuite } from './abstracts';
18
-
19
- const testItems: ITestSuite = {
20
-
21
- name: 'Modifiers',
22
- sections: [
23
-
24
- {
25
- 'name': '$.string',
26
- 'rule': ['$.string', 'uint32', { 'a': 'uint32' }],
27
- 'items': [
28
- {
29
- title: JSON.stringify('123'),
30
- value: '123',
31
- expect: true
32
- },
33
- {
34
- title: JSON.stringify({ 'a': 123 }),
35
- value: { 'a': 123 },
36
- expect: true
37
- },
38
- {
39
- title: JSON.stringify({ 'a': 123 }),
40
- value: { 'a': 123 },
41
- expect: true
42
- },
43
- ...defaultItems({
44
- 'number 1': true,
45
- 'number 0': true
46
- })
47
- ]
48
- },
49
- {
50
- 'name': '$.string standalone',
51
- 'rule': ['$.and', ['$.string', 'uint32'], 'uint32'],
52
- 'items': [
53
- {
54
- title: JSON.stringify('123'),
55
- value: '123',
56
- expect: false
57
- }
58
- ]
59
- },
60
- {
61
- 'name': '$.strict standalone',
62
- 'rule': ['$.tuple', ['$.strict', {
63
- 'a': 'string'
64
- }], {
65
- 'a': 'string'
66
- }],
67
- 'items': [
68
- {
69
- title: JSON.stringify([{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }]),
70
- value: [{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }],
71
- expect: true
72
- }
73
- ]
74
- },
75
- {
76
- 'name': '$.equal standalone',
77
- 'rule': ['$.and', ['$.equal', {
78
- 'a': 'any'
79
- }], {
80
- 'a': {
81
- 'b': 'string'
82
- }
83
- }],
84
- 'items': [
85
- {
86
- title: JSON.stringify({ 'a': { 'b': 'c', 'd': 'ccc' } }),
87
- value: { 'a': { 'b': 'c', 'd': 'ccc' } },
88
- expect: true
89
- }
90
- ]
91
- },
92
- {
93
- 'name': '$.not with string and literal 0',
94
- 'rule': ['$.not', 'string', 0],
95
- 'items': [
96
- {
97
- title: 'string \'abc\'',
98
- value: 'abc',
99
- expect: false
100
- },
101
- ...defaultItems({
102
- 'string \'hello\'': false,
103
- 'empty string': false,
104
- 'number 0': false
105
- }, true)
106
- ]
107
- },
108
- {
109
- 'name': '$.type with complex name',
110
- 'rule': {
111
- 'a': ['$.type', 'test:a.b-c', 'int'],
112
- 'b': '@test:a.b-c',
113
- },
114
- 'items': [
115
- {
116
- title: 'object {a: 213, b: 321}',
117
- value: {
118
- 'a': 213,
119
- 'b': 321
120
- },
121
- expect: true
122
- },
123
- {
124
- title: 'object {a: 213, b: "321"}',
125
- value: {
126
- 'a': 213,
127
- 'b': '321'
128
- },
129
- expect: false
130
- },
131
- {
132
- title: 'object {a: 213, b: "321"}',
133
- value: {
134
- 'a': 213,
135
- 'b': 777
136
- },
137
- expect: true
138
- },
139
- ...defaultItems({}, false)
140
- ]
141
- },
142
- {
143
- 'name': '$.type pre-defined IPv4 Address Checker',
144
- 'rule': '@ipv4_address',
145
- 'items': [
146
- {
147
- title: 'String: 1.1.1.1',
148
- value: '1.1.1.1',
149
- expect: true
150
- },
151
- {
152
- title: 'String: 255.255.255.255',
153
- value: '255.255.255.255',
154
- expect: true
155
- },
156
- {
157
- title: 'String: 0.0.0.0',
158
- value: '0.0.0.0',
159
- expect: true
160
- },
161
- {
162
- title: 'String: 2552.1.1.1',
163
- value: '2552.1.1.1',
164
- expect: false
165
- },
166
- {
167
- title: 'String: 256.1.1.1',
168
- value: '256.1.1.1',
169
- expect: false
170
- },
171
- {
172
- title: 'String: .1.1.1',
173
- value: '.1.1.1',
174
- expect: false
175
- },
176
- {
177
- title: 'object {a: 213, b: 321}',
178
- value: {
179
- 'a': 213,
180
- 'b': 321
181
- },
182
- expect: false
183
- },
184
- {
185
- title: 'object {a: 213, b: "321"}',
186
- value: {
187
- 'a': 213,
188
- 'b': '321'
189
- },
190
- expect: false
191
- },
192
- {
193
- title: 'object {a: 213, b: "321"}',
194
- value: {
195
- 'a': 213,
196
- 'b': 777
197
- },
198
- expect: false
199
- },
200
- ...defaultItems({}, false)
201
- ]
202
- },
203
- {
204
- 'name': '$.type pre-defined type "trim_string" without args',
205
- 'rule': '@trim_string',
206
- 'items': [
207
- {
208
- title: 'String " aaa"',
209
- value: ' aaa',
210
- expect: true
211
- },
212
- ...defaultItems({
213
- 'string \'hello\'': true,
214
- 'empty string': true,
215
- }, false)
216
- ]
217
- },
218
- {
219
- 'name': '$.type pre-defined type "trim_string" with full args (2, 5)',
220
- 'rule': '@trim_string(2, 5)',
221
- 'items': [
222
- {
223
- title: 'String " aaa"',
224
- value: ' aaa',
225
- expect: true
226
- },
227
- {
228
- title: 'String " ffff"',
229
- value: ' ffff',
230
- expect: true
231
- },
232
- {
233
- title: 'String " aa "',
234
- value: ' aa ',
235
- expect: true
236
- },
237
- {
238
- title: 'String " a "',
239
- value: ' a ',
240
- expect: false
241
- },
242
- {
243
- title: 'String " aaaaaaa "',
244
- value: ' aaaaaaa ',
245
- expect: false
246
- },
247
- ...defaultItems({
248
- 'string \'hello\'': true,
249
- 'empty string': false,
250
- }, false)
251
- ]
252
- },
253
- {
254
- 'name': '$.type pre-defined type "enum" with args (1, 2, 3, "dddd", true, false, null, -2, 0x1111, "ss\\"", \'ff\', \'ff\\\'\')',
255
- 'rule': '@enum(1, 2, 3, "dddd", true, false, null, -2, 0x1111, "ss\\"", \'ff\', \'ff\\\'\')',
256
- 'items': [
257
- {
258
- title: 'Integer 2',
259
- value: 2,
260
- expect: true
261
- },
262
- {
263
- title: 'Integer 0x1111',
264
- value: 0x1111,
265
- expect: true
266
- },
267
- {
268
- title: 'Integer -2',
269
- value: -2,
270
- expect: true
271
- },
272
- {
273
- title: 'String "dddd"',
274
- value: 'dddd',
275
- expect: true
276
- },
277
- {
278
- title: 'String "ss\\""',
279
- value: 'ss"',
280
- expect: true
281
- },
282
- {
283
- title: 'String "ff"',
284
- value: 'ff',
285
- expect: true
286
- },
287
- {
288
- title: 'String "ff\'"',
289
- value: 'ff\'',
290
- expect: true
291
- },
292
- ...defaultItems({
293
- 'number 1': true,
294
- 'true': true,
295
- 'false': true,
296
- 'null': true,
297
- }, false)
298
- ]
299
- },
300
- {
301
- 'name': '$.type pre-defined type "trim_string" with args (2)',
302
- 'rule': '@trim_string(2)',
303
- 'items': [
304
- {
305
- title: 'String " aaa"',
306
- value: ' aaa',
307
- expect: true
308
- },
309
- {
310
- title: 'String " ffff"',
311
- value: ' ffff',
312
- expect: true
313
- },
314
- {
315
- title: 'String " aa "',
316
- value: ' aa ',
317
- expect: true
318
- },
319
- {
320
- title: 'String " a "',
321
- value: ' a ',
322
- expect: false
323
- },
324
- {
325
- title: 'String " aaaaaaa "',
326
- value: ' aaaaaaa ',
327
- expect: true
328
- },
329
- ...defaultItems({
330
- 'string \'hello\'': true,
331
- 'empty string': false,
332
- }, false)
333
- ]
334
- },
335
- {
336
- 'name': '$.type with unsupported characters in type name',
337
- 'rule': {
338
- 'a': ['$.type', 'test=123', 'int'],
339
- 'b': '@test:a',
340
- },
341
- 'items': [
342
- {
343
- title: 'invalid format',
344
- value: '',
345
- expect: 'throw'
346
- }
347
- ]
348
- },
349
- {
350
- 'name': '$.enum',
351
- 'rule': ['$.enum', '==123', '=123', 'int', 3333333, null, true],
352
- 'items': [
353
- {
354
- title: 'string "==123"',
355
- value: '==123',
356
- expect: true
357
- },
358
- {
359
- title: 'string "123"',
360
- value: '123',
361
- expect: false
362
- },
363
- {
364
- title: 'string "=123"',
365
- value: '=123',
366
- expect: true
367
- },
368
- {
369
- title: 'string "int"',
370
- value: 'int',
371
- expect: true
372
- },
373
- {
374
- title: 'string "uint"',
375
- value: 'uint',
376
- expect: false
377
- },
378
- {
379
- title: 'integer 3333333',
380
- value: 3333333,
381
- expect: true
382
- },
383
- ...defaultItems({
384
- 'null': true,
385
- 'true': true
386
- }, false)
387
- ]
388
- },
389
- {
390
- 'name': '$.enum with empty candidate list',
391
- 'rule': ['$.enum'],
392
- 'items': [
393
- {
394
- title: 'invalid empty candidate list',
395
- value: '',
396
- expect: 'throw'
397
- }
398
- ]
399
- },
400
- {
401
- 'name': '$.enum with object candidate',
402
- 'rule': ['$.enum', {}],
403
- 'items': [
404
- {
405
- title: 'object as candidate',
406
- value: '',
407
- expect: 'throw'
408
- }
409
- ]
410
- },
411
- {
412
- 'name': '$.enum with array candidate',
413
- 'rule': ['$.enum', []],
414
- 'items': [
415
- {
416
- title: 'array as candidate',
417
- value: '',
418
- expect: 'throw'
419
- }
420
- ]
421
- },
422
- {
423
- 'name': '$.enum with undefined candidate',
424
- 'rule': ['$.enum', undefined],
425
- 'items': [
426
- {
427
- title: 'undefined as candidate',
428
- value: '',
429
- expect: 'throw'
430
- }
431
- ]
432
- },
433
- {
434
- 'name': '$.enum with function candidate',
435
- 'rule': ['$.enum', () => true],
436
- 'items': [
437
- {
438
- title: 'function as candidate',
439
- value: '',
440
- expect: 'throw'
441
- }
442
- ]
443
- },
444
- {
445
- 'name': '$.enum with bigint candidate',
446
- 'rule': ['$.enum', BigInt(123)],
447
- 'items': [
448
- {
449
- title: 'bigint as candidate',
450
- value: '',
451
- expect: 'throw'
452
- }
453
- ]
454
- },
455
- {
456
- 'name': '$.enum with symbol candidate',
457
- 'rule': ['$.enum', Symbol('123')],
458
- 'items': [
459
- {
460
- title: 'symbol as candidate',
461
- value: '',
462
- expect: 'throw'
463
- }
464
- ]
465
- },
466
- ]
467
- };
468
-
469
- export default createTestDefinition(testItems);
@@ -1,151 +0,0 @@
1
- /**
2
- * Copyright 2023 Angus Fenying <fenying@litert.org>
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import {
18
- createTestDefinition,
19
- defaultItems,
20
- ITestSuite,
21
- assertItem,
22
- addRule
23
- } from './abstracts';
24
-
25
- const testItems: ITestSuite = {
26
-
27
- name: 'Mappings & Dicts',
28
- sections: [
29
-
30
- {
31
- 'name': JSON.stringify(['$.map', 'string', 'uint32']),
32
- 'rule': ['$.map', 'string', 'uint32'],
33
- 'items': [
34
- {
35
- title: 'all string object',
36
- value: { 'a': '123312312', 'b': 'ccc' },
37
- expect: true
38
- },
39
- {
40
- title: 'all uint32 object',
41
- value: { 'a': 123312312, 'b': 0xCCC },
42
- expect: true
43
- },
44
- {
45
- title: 'all uint32 & string object',
46
- value: { 'a': 123312312, 'b': 0xCCC },
47
- expect: true
48
- },
49
- {
50
- title: 'all uint32 & boolean object',
51
- value: { 'a': 123312312, 'b': false },
52
- expect: false
53
- },
54
- ...defaultItems({
55
- 'object': true
56
- })
57
- ]
58
- },
59
- {
60
- 'name': JSON.stringify({
61
- 'a': 'string',
62
- '$.map': 'number'
63
- }),
64
- 'rule': {
65
- 'a': 'string',
66
- '$.map': 'number'
67
- },
68
- 'items': [
69
- assertItem(
70
- { 'a': '123312312', 'b': 123.3, c: 1232 },
71
- true
72
- ),
73
- assertItem(
74
- { 'a': '123312312', 'b': 'ccc' },
75
- false
76
- ),
77
- assertItem(
78
- { 'a': 123312312, 'b': 123.3 },
79
- false
80
- ),
81
- ]
82
- },
83
- {
84
- 'name': JSON.stringify({
85
- 'a': 'string',
86
- 'b->{}': 'number'
87
- }),
88
- 'rule': {
89
- 'a': 'string',
90
- 'b->{}': 'number'
91
- },
92
- 'items': [
93
- assertItem(
94
- { 'a': '123312312', 'b': 'ccc' },
95
- false
96
- ),
97
- assertItem(
98
- { 'a': 123312312, 'b': 123.3 },
99
- false
100
- ),
101
- assertItem(
102
- { 'a': '123312312', 'b': { d: 123.3, c: 1232 } },
103
- true
104
- ),
105
- assertItem(
106
- { 'a': '123312312', 'b': { d: 123.3, c: '1232' } },
107
- false
108
- ),
109
- ]
110
- },
111
- addRule(['$.dict', ['a', 'b'], 'string'], [
112
- assertItem(
113
- { 'a': '123312312', 'b': 'ccc' },
114
- true
115
- ),
116
- assertItem(
117
- { 'a': '123312312', 'b': 'ccc', 'c': 123 },
118
- true
119
- ),
120
- assertItem(
121
- { 'a': '123312312' },
122
- false
123
- ),
124
- assertItem(
125
- { 'a': '123312312', 'c': 'ddd' },
126
- false
127
- ),
128
- assertItem(
129
- { 'a': '123312312', 'b': 123.3 },
130
- false
131
- )
132
- ]),
133
- addRule(['$.strict', '$.dict', ['a', 'b'], 'string'], [
134
- assertItem(
135
- { 'a': '123312312', 'b': 'ccc' },
136
- true
137
- ),
138
- assertItem(
139
- { 'a': '123312312', 'b': 'ccc', 'c': 123 },
140
- false
141
- ),
142
- assertItem(
143
- { 'a': '123312312' },
144
- false
145
- ),
146
- ])
147
-
148
- ]
149
- };
150
-
151
- export default createTestDefinition(testItems);
@@ -1,67 +0,0 @@
1
- /**
2
- * Copyright 2023 Angus Fenying <fenying@litert.org>
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import {
18
- createTestDefinition,
19
- ITestSuite
20
- } from './abstracts';
21
-
22
- const testItems: ITestSuite = {
23
-
24
- name: 'When optional with any',
25
- sections: [
26
-
27
- {
28
- 'name': JSON.stringify({ 'test?': 'any' }),
29
- 'rule': { 'test?': 'any' },
30
- 'items': [
31
- {
32
- title: 'When test === \'ffff\'',
33
- value: { 'test': 'ffff' },
34
- expect: true
35
- },
36
- {
37
- title: 'When test is omitted',
38
- value: {},
39
- expect: true
40
- }
41
- ]
42
- },
43
- {
44
- 'name': JSON.stringify({ 'test': 'int8' }),
45
- 'rule': { 'test': 'int8' },
46
- 'items': [
47
- {
48
- title: 'When test === 123',
49
- value: { 'test': 123 },
50
- expect: true
51
- },
52
- {
53
- title: 'When test === 1234',
54
- value: { 'test': 1234 },
55
- expect: false
56
- },
57
- {
58
- title: 'When test is omitted',
59
- value: {},
60
- expect: false
61
- }
62
- ]
63
- },
64
- ]
65
- };
66
-
67
- export default createTestDefinition(testItems);