@litert/typeguard 1.3.0 → 1.4.0

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 (61) hide show
  1. package/CHANGES.md +7 -1
  2. package/lib/BuiltInTypeCompiler.d.ts +1 -1
  3. package/lib/BuiltInTypeCompiler.js +4 -4
  4. package/lib/BuiltInTypeCompiler.js.map +1 -1
  5. package/lib/BuiltInTypes.d.ts +1 -1
  6. package/lib/BuiltInTypes.js +1 -1
  7. package/lib/Common.d.ts +6 -5
  8. package/lib/Common.d.ts.map +1 -1
  9. package/lib/Common.js +1 -1
  10. package/lib/Compiler.d.ts +1 -1
  11. package/lib/Compiler.d.ts.map +1 -1
  12. package/lib/Compiler.js +57 -35
  13. package/lib/Compiler.js.map +1 -1
  14. package/lib/Context.d.ts +2 -2
  15. package/lib/Context.d.ts.map +1 -1
  16. package/lib/Context.js +2 -2
  17. package/lib/Context.js.map +1 -1
  18. package/lib/FilterCompiler.d.ts +3 -3
  19. package/lib/FilterCompiler.d.ts.map +1 -1
  20. package/lib/FilterCompiler.js +4 -4
  21. package/lib/FilterCompiler.js.map +1 -1
  22. package/lib/InlineCompiler.d.ts +2 -2
  23. package/lib/InlineCompiler.d.ts.map +1 -1
  24. package/lib/InlineCompiler.js +5 -7
  25. package/lib/InlineCompiler.js.map +1 -1
  26. package/lib/Internal.d.ts +9 -4
  27. package/lib/Internal.d.ts.map +1 -1
  28. package/lib/Internal.js +45 -4
  29. package/lib/Internal.js.map +1 -1
  30. package/lib/Modifiers.d.ts +2 -1
  31. package/lib/Modifiers.d.ts.map +1 -1
  32. package/lib/Modifiers.js +3 -2
  33. package/lib/Modifiers.js.map +1 -1
  34. package/lib/index.d.ts +1 -1
  35. package/lib/index.js +1 -1
  36. package/lib/langs/JavaScript.d.ts +1 -1
  37. package/lib/langs/JavaScript.js +2 -2
  38. package/package.json +10 -12
  39. package/src/examples/quick-start.ts +5 -1
  40. package/src/lib/BuiltInTypeCompiler.ts +4 -4
  41. package/src/lib/BuiltInTypes.ts +1 -1
  42. package/src/lib/Common.ts +5 -4
  43. package/src/lib/Compiler.ts +66 -39
  44. package/src/lib/Context.ts +2 -2
  45. package/src/lib/FilterCompiler.ts +3 -3
  46. package/src/lib/InlineCompiler.ts +6 -9
  47. package/src/lib/Internal.ts +73 -4
  48. package/src/lib/Modifiers.ts +3 -1
  49. package/src/lib/index.ts +1 -1
  50. package/src/lib/langs/JavaScript.ts +2 -2
  51. package/src/test/00-all.ts +1 -1
  52. package/src/test/01-elemental-types.ts +580 -580
  53. package/src/test/02-array-and-list.ts +43 -43
  54. package/src/test/03-tuple.ts +61 -61
  55. package/src/test/04-from-string.ts +501 -501
  56. package/src/test/05-string-asserts.ts +203 -203
  57. package/src/test/06-structure.ts +71 -71
  58. package/src/test/07-modifiers.ts +297 -48
  59. package/src/test/08-map-and-dict.ts +11 -11
  60. package/src/test/09-exceptions.ts +11 -11
  61. package/src/test/abstracts.ts +57 -29
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
2
+ * Copyright 2023 Angus Fenying <fenying@litert.org>
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import { createTestDefinition, defaultItemss, ITestSuite } from './abstracts';
17
+ import { createTestDefinition, defaultItems, ITestSuite } from './abstracts';
18
18
 
19
19
  const testItems: ITestSuite = {
20
20
 
@@ -26,21 +26,21 @@ const testItems: ITestSuite = {
26
26
  'rule': ['$.string', 'uint32', { 'a': 'uint32' }],
27
27
  'items': [
28
28
  {
29
- inputName: JSON.stringify('123'),
30
- inputValue: '123',
29
+ title: JSON.stringify('123'),
30
+ value: '123',
31
31
  expect: true
32
32
  },
33
33
  {
34
- inputName: JSON.stringify({ 'a': 123 }),
35
- inputValue: { 'a': 123 },
34
+ title: JSON.stringify({ 'a': 123 }),
35
+ value: { 'a': 123 },
36
36
  expect: true
37
37
  },
38
38
  {
39
- inputName: JSON.stringify({ 'a': 123 }),
40
- inputValue: { 'a': 123 },
39
+ title: JSON.stringify({ 'a': 123 }),
40
+ value: { 'a': 123 },
41
41
  expect: true
42
42
  },
43
- ...defaultItemss({
43
+ ...defaultItems({
44
44
  'number 1': true,
45
45
  'number 0': true
46
46
  })
@@ -51,8 +51,8 @@ const testItems: ITestSuite = {
51
51
  'rule': ['$.and', ['$.string', 'uint32'], 'uint32'],
52
52
  'items': [
53
53
  {
54
- inputName: JSON.stringify('123'),
55
- inputValue: '123',
54
+ title: JSON.stringify('123'),
55
+ value: '123',
56
56
  expect: false
57
57
  }
58
58
  ]
@@ -66,8 +66,8 @@ const testItems: ITestSuite = {
66
66
  }],
67
67
  'items': [
68
68
  {
69
- inputName: JSON.stringify([{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }]),
70
- inputValue: [{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }],
69
+ title: JSON.stringify([{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }]),
70
+ value: [{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }],
71
71
  expect: true
72
72
  }
73
73
  ]
@@ -83,8 +83,8 @@ const testItems: ITestSuite = {
83
83
  }],
84
84
  'items': [
85
85
  {
86
- inputName: JSON.stringify({ 'a': { 'b': 'c', 'd': 'ccc' } }),
87
- inputValue: { 'a': { 'b': 'c', 'd': 'ccc' } },
86
+ title: JSON.stringify({ 'a': { 'b': 'c', 'd': 'ccc' } }),
87
+ value: { 'a': { 'b': 'c', 'd': 'ccc' } },
88
88
  expect: true
89
89
  }
90
90
  ]
@@ -94,11 +94,11 @@ const testItems: ITestSuite = {
94
94
  'rule': ['$.not', 'string', 0],
95
95
  'items': [
96
96
  {
97
- inputName: 'string \'abc\'',
98
- inputValue: 'abc',
97
+ title: 'string \'abc\'',
98
+ value: 'abc',
99
99
  expect: false
100
100
  },
101
- ...defaultItemss({
101
+ ...defaultItems({
102
102
  'string \'hello\'': false,
103
103
  'empty string': false,
104
104
  'number 0': false
@@ -106,98 +106,230 @@ const testItems: ITestSuite = {
106
106
  ]
107
107
  },
108
108
  {
109
- 'name': '$.type ',
109
+ 'name': '$.type with complex name',
110
110
  'rule': {
111
111
  'a': ['$.type', 'test:a.b-c', 'int'],
112
112
  'b': '@test:a.b-c',
113
113
  },
114
114
  'items': [
115
115
  {
116
- inputName: 'object {a: 213, b: 321}',
117
- inputValue: {
116
+ title: 'object {a: 213, b: 321}',
117
+ value: {
118
118
  'a': 213,
119
119
  'b': 321
120
120
  },
121
121
  expect: true
122
122
  },
123
123
  {
124
- inputName: 'object {a: 213, b: "321"}',
125
- inputValue: {
124
+ title: 'object {a: 213, b: "321"}',
125
+ value: {
126
126
  'a': 213,
127
127
  'b': '321'
128
128
  },
129
129
  expect: false
130
130
  },
131
131
  {
132
- inputName: 'object {a: 213, b: "321"}',
133
- inputValue: {
132
+ title: 'object {a: 213, b: "321"}',
133
+ value: {
134
134
  'a': 213,
135
135
  'b': 777
136
136
  },
137
137
  expect: true
138
138
  },
139
- ...defaultItemss({}, false)
139
+ ...defaultItems({}, false)
140
140
  ]
141
141
  },
142
142
  {
143
- 'name': 'Pre-defined IPv4 Address Checker',
143
+ 'name': '$.type pre-defined IPv4 Address Checker',
144
144
  'rule': '@ipv4_address',
145
145
  'items': [
146
146
  {
147
- inputName: 'String: 1.1.1.1',
148
- inputValue: '1.1.1.1',
147
+ title: 'String: 1.1.1.1',
148
+ value: '1.1.1.1',
149
149
  expect: true
150
150
  },
151
151
  {
152
- inputName: 'String: 255.255.255.255',
153
- inputValue: '255.255.255.255',
152
+ title: 'String: 255.255.255.255',
153
+ value: '255.255.255.255',
154
154
  expect: true
155
155
  },
156
156
  {
157
- inputName: 'String: 0.0.0.0',
158
- inputValue: '0.0.0.0',
157
+ title: 'String: 0.0.0.0',
158
+ value: '0.0.0.0',
159
159
  expect: true
160
160
  },
161
161
  {
162
- inputName: 'String: 2552.1.1.1',
163
- inputValue: '2552.1.1.1',
162
+ title: 'String: 2552.1.1.1',
163
+ value: '2552.1.1.1',
164
164
  expect: false
165
165
  },
166
166
  {
167
- inputName: 'String: 256.1.1.1',
168
- inputValue: '256.1.1.1',
167
+ title: 'String: 256.1.1.1',
168
+ value: '256.1.1.1',
169
169
  expect: false
170
170
  },
171
171
  {
172
- inputName: 'String: .1.1.1',
173
- inputValue: '.1.1.1',
172
+ title: 'String: .1.1.1',
173
+ value: '.1.1.1',
174
174
  expect: false
175
175
  },
176
176
  {
177
- inputName: 'object {a: 213, b: 321}',
178
- inputValue: {
177
+ title: 'object {a: 213, b: 321}',
178
+ value: {
179
179
  'a': 213,
180
180
  'b': 321
181
181
  },
182
182
  expect: false
183
183
  },
184
184
  {
185
- inputName: 'object {a: 213, b: "321"}',
186
- inputValue: {
185
+ title: 'object {a: 213, b: "321"}',
186
+ value: {
187
187
  'a': 213,
188
188
  'b': '321'
189
189
  },
190
190
  expect: false
191
191
  },
192
192
  {
193
- inputName: 'object {a: 213, b: "321"}',
194
- inputValue: {
193
+ title: 'object {a: 213, b: "321"}',
194
+ value: {
195
195
  'a': 213,
196
196
  'b': 777
197
197
  },
198
198
  expect: false
199
199
  },
200
- ...defaultItemss({}, false)
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)
201
333
  ]
202
334
  },
203
335
  {
@@ -208,8 +340,125 @@ const testItems: ITestSuite = {
208
340
  },
209
341
  'items': [
210
342
  {
211
- inputName: 'invalid format',
212
- inputValue: '',
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: '',
213
462
  expect: 'throw'
214
463
  }
215
464
  ]
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
2
+ * Copyright 2023 Angus Fenying <fenying@litert.org>
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
16
16
 
17
17
  import {
18
18
  createTestDefinition,
19
- defaultItemss,
19
+ defaultItems,
20
20
  ITestSuite,
21
21
  assertItem,
22
22
  addRule
@@ -32,26 +32,26 @@ const testItems: ITestSuite = {
32
32
  'rule': ['$.map', 'string', 'uint32'],
33
33
  'items': [
34
34
  {
35
- inputName: 'all string object',
36
- inputValue: { 'a': '123312312', 'b': 'ccc' },
35
+ title: 'all string object',
36
+ value: { 'a': '123312312', 'b': 'ccc' },
37
37
  expect: true
38
38
  },
39
39
  {
40
- inputName: 'all uint32 object',
41
- inputValue: { 'a': 123312312, 'b': 0xCCC },
40
+ title: 'all uint32 object',
41
+ value: { 'a': 123312312, 'b': 0xCCC },
42
42
  expect: true
43
43
  },
44
44
  {
45
- inputName: 'all uint32 & string object',
46
- inputValue: { 'a': 123312312, 'b': 0xCCC },
45
+ title: 'all uint32 & string object',
46
+ value: { 'a': 123312312, 'b': 0xCCC },
47
47
  expect: true
48
48
  },
49
49
  {
50
- inputName: 'all uint32 & boolean object',
51
- inputValue: { 'a': 123312312, 'b': false },
50
+ title: 'all uint32 & boolean object',
51
+ value: { 'a': 123312312, 'b': false },
52
52
  expect: false
53
53
  },
54
- ...defaultItemss({
54
+ ...defaultItems({
55
55
  'object': true
56
56
  })
57
57
  ]
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2022 Angus Fenying <fenying@litert.org>
2
+ * Copyright 2023 Angus Fenying <fenying@litert.org>
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -29,13 +29,13 @@ const testItems: ITestSuite = {
29
29
  'rule': { 'test?': 'any' },
30
30
  'items': [
31
31
  {
32
- inputName: 'When test === \'ffff\'',
33
- inputValue: { 'test': 'ffff' },
32
+ title: 'When test === \'ffff\'',
33
+ value: { 'test': 'ffff' },
34
34
  expect: true
35
35
  },
36
36
  {
37
- inputName: 'When test is omitted',
38
- inputValue: {},
37
+ title: 'When test is omitted',
38
+ value: {},
39
39
  expect: true
40
40
  }
41
41
  ]
@@ -45,18 +45,18 @@ const testItems: ITestSuite = {
45
45
  'rule': { 'test': 'int8' },
46
46
  'items': [
47
47
  {
48
- inputName: 'When test === 123',
49
- inputValue: { 'test': 123 },
48
+ title: 'When test === 123',
49
+ value: { 'test': 123 },
50
50
  expect: true
51
51
  },
52
52
  {
53
- inputName: 'When test === 1234',
54
- inputValue: { 'test': 1234 },
53
+ title: 'When test === 1234',
54
+ value: { 'test': 1234 },
55
55
  expect: false
56
56
  },
57
57
  {
58
- inputName: 'When test is omitted',
59
- inputValue: {},
58
+ title: 'When test is omitted',
59
+ value: {},
60
60
  expect: false
61
61
  }
62
62
  ]