@litert/typeguard 1.3.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.
Files changed (61) hide show
  1. package/CHANGES.md +11 -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 +58 -36
  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 +67 -40
  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 +0 -35
  52. package/src/test/01-elemental-types.ts +0 -1963
  53. package/src/test/02-array-and-list.ts +0 -197
  54. package/src/test/03-tuple.ts +0 -220
  55. package/src/test/04-from-string.ts +0 -1488
  56. package/src/test/05-string-asserts.ts +0 -794
  57. package/src/test/06-structure.ts +0 -277
  58. package/src/test/07-modifiers.ts +0 -220
  59. package/src/test/08-map-and-dict.ts +0 -151
  60. package/src/test/09-exceptions.ts +0 -67
  61. package/src/test/abstracts.ts +0 -211
@@ -1,277 +0,0 @@
1
- /**
2
- * Copyright 2022 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, defaultItemss, ITestSuite } from './abstracts';
18
-
19
- const testItems: ITestSuite = {
20
-
21
- name: 'Structure',
22
- sections: [
23
-
24
- {
25
- 'name': 'Empty object',
26
- 'rule': {},
27
- 'items': [
28
- {
29
- inputName: 'Empty object',
30
- inputValue: {},
31
- expect: true
32
- },
33
- {
34
- inputName: 'Object with one property',
35
- inputValue: { 'a': 123 },
36
- expect: true
37
- },
38
- {
39
- inputName: '[int, string]',
40
- inputValue: [123, 'fff'],
41
- expect: false
42
- },
43
- ...defaultItemss({
44
- 'object': true
45
- })
46
- ]
47
- },
48
- {
49
- 'name': '1 property object',
50
- 'rule': { 'a': 'uint32' },
51
- 'items': [
52
- {
53
- inputName: 'Empty object',
54
- inputValue: {},
55
- expect: false
56
- },
57
- {
58
- inputName: 'Object with one property',
59
- inputValue: { 'a': 123 },
60
- expect: true
61
- },
62
- {
63
- inputName: '[int, string]',
64
- inputValue: [123, 'fff'],
65
- expect: false
66
- },
67
- ...defaultItemss({
68
- 'object': false
69
- })
70
- ]
71
- },
72
- {
73
- 'name': '1 optional property object',
74
- 'rule': { 'a?': 'uint32' },
75
- 'items': [
76
- {
77
- inputName: 'Empty object',
78
- inputValue: {},
79
- expect: true
80
- },
81
- {
82
- inputName: 'Object with one property',
83
- inputValue: { 'a': 123 },
84
- expect: true
85
- },
86
- {
87
- inputName: 'Object with one mismatch-type property',
88
- inputValue: { 'a': '123' },
89
- expect: false
90
- },
91
- {
92
- inputName: '[int, string]',
93
- inputValue: [123, 'fff'],
94
- expect: false
95
- },
96
- ...defaultItemss({
97
- 'object': true
98
- })
99
- ]
100
- },
101
- {
102
- 'name': 'Nested object',
103
- 'rule': { 'a?': 'uint32', 'b': { 'c': 'string' } },
104
- 'items': [
105
- {
106
- inputName: 'Empty object',
107
- inputValue: {},
108
- expect: false
109
- },
110
- {
111
- inputName: 'Object with one property',
112
- inputValue: { 'a': 123 },
113
- expect: false
114
- },
115
- {
116
- inputName: 'Object with mismatch-typed nest property',
117
- inputValue: { 'a': 123, 'b': { 'c': 1232131 } },
118
- expect: false
119
- },
120
- {
121
- inputName: 'Object with one mismatch-typed property',
122
- inputValue: { 'a': '123', 'b': { 'c': 'ffff' } },
123
- expect: false
124
- },
125
- {
126
- inputName: 'Object with all corrected',
127
- inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
128
- expect: true
129
- }
130
- ]
131
- },
132
- {
133
- 'name': 'Strict object',
134
- 'rule': ['$.strict', {
135
- 'a': 'uint'
136
- }],
137
- 'items': [
138
- {
139
- inputName: 'Empty object',
140
- inputValue: {},
141
- expect: false
142
- },
143
- {
144
- inputName: 'Object with one property',
145
- inputValue: { 'a': 123 },
146
- expect: true
147
- },
148
- {
149
- inputName: 'Object with one mismatch-typed property',
150
- inputValue: { 'a': '123' },
151
- expect: false
152
- },
153
- {
154
- inputName: 'Object with 2 properties',
155
- inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
156
- expect: false
157
- }
158
- ]
159
- },
160
- {
161
- 'name': 'Strict object with nested properties',
162
- 'rule': ['$.strict', {
163
- 'a': 'uint',
164
- 'b': {
165
- 'c': 'uint'
166
- }
167
- }],
168
- 'items': [
169
- {
170
- inputName: 'Object with one property',
171
- inputValue: { 'a': 123 },
172
- expect: false
173
- },
174
- {
175
- inputName: 'Object with wrong type in nest',
176
- inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
177
- expect: false
178
- },
179
- {
180
- inputName: 'Object with all correct',
181
- inputValue: { 'a': 123, 'b': { 'c': 123 } },
182
- expect: true
183
- },
184
- {
185
- inputName: 'Object with one more in nested',
186
- inputValue: { 'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
187
- expect: true
188
- }
189
- ]
190
- },
191
- {
192
- 'name': 'Recursively-Strict object with nested properties',
193
- 'rule': ['$.equal', {
194
- 'a': 'uint',
195
- 'b': {
196
- 'c': 'uint'
197
- }
198
- }],
199
- 'items': [
200
- {
201
- inputName: 'Object with one property',
202
- inputValue: { 'a': 123 },
203
- expect: false
204
- },
205
- {
206
- inputName: 'Object with wrong type in nest',
207
- inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
208
- expect: false
209
- },
210
- {
211
- inputName: 'Object with all correct',
212
- inputValue: { 'a': 123, 'b': { 'c': 123 } },
213
- expect: true
214
- },
215
- {
216
- inputName: 'Object with one more in nested',
217
- inputValue: { 'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
218
- expect: false
219
- }
220
- ]
221
- },
222
- {
223
- 'name': 'Object with from-string modifier',
224
- 'rule': ['$.string', {
225
- 'a': 'uint',
226
- 'b': {
227
- 'c': 'uint'
228
- }
229
- }],
230
- 'items': [
231
- {
232
- inputName: 'Object with all correct',
233
- inputValue: { 'a': 123, 'b': { 'c': 123 } },
234
- expect: true
235
- },
236
- {
237
- inputName: 'Object with string incorrect-value',
238
- inputValue: { 'a': 123, 'b': { 'c': '123d' } },
239
- expect: false
240
- },
241
- {
242
- inputName: 'Object with string value',
243
- inputValue: { 'a': '123312312', 'b': { 'c': '123' } },
244
- expect: true
245
- }
246
- ]
247
- },
248
- {
249
- 'name': 'Object with from-string modifier',
250
- 'rule': ['$.string', {
251
- 'a': 'uint',
252
- 'b': {
253
- 'c': 'uint'
254
- }
255
- }],
256
- 'items': [
257
- {
258
- inputName: 'Object with all correct',
259
- inputValue: { 'a': 123, 'b': { 'c': 123 } },
260
- expect: true
261
- },
262
- {
263
- inputName: 'Object with string incorrect-value',
264
- inputValue: { 'a': 123, 'b': { 'c': '123d' } },
265
- expect: false
266
- },
267
- {
268
- inputName: 'Object with string value',
269
- inputValue: { 'a': '123312312', 'b': { 'c': '123' } },
270
- expect: true
271
- }
272
- ]
273
- },
274
- ]
275
- };
276
-
277
- export default createTestDefinition(testItems);
@@ -1,220 +0,0 @@
1
- /**
2
- * Copyright 2022 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, defaultItemss, 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
- inputName: JSON.stringify('123'),
30
- inputValue: '123',
31
- expect: true
32
- },
33
- {
34
- inputName: JSON.stringify({ 'a': 123 }),
35
- inputValue: { 'a': 123 },
36
- expect: true
37
- },
38
- {
39
- inputName: JSON.stringify({ 'a': 123 }),
40
- inputValue: { 'a': 123 },
41
- expect: true
42
- },
43
- ...defaultItemss({
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
- inputName: JSON.stringify('123'),
55
- inputValue: '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
- inputName: JSON.stringify([{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }]),
70
- inputValue: [{ '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
- inputName: JSON.stringify({ 'a': { 'b': 'c', 'd': 'ccc' } }),
87
- inputValue: { '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
- inputName: 'string \'abc\'',
98
- inputValue: 'abc',
99
- expect: false
100
- },
101
- ...defaultItemss({
102
- 'string \'hello\'': false,
103
- 'empty string': false,
104
- 'number 0': false
105
- }, true)
106
- ]
107
- },
108
- {
109
- 'name': '$.type ',
110
- 'rule': {
111
- 'a': ['$.type', 'test:a.b-c', 'int'],
112
- 'b': '@test:a.b-c',
113
- },
114
- 'items': [
115
- {
116
- inputName: 'object {a: 213, b: 321}',
117
- inputValue: {
118
- 'a': 213,
119
- 'b': 321
120
- },
121
- expect: true
122
- },
123
- {
124
- inputName: 'object {a: 213, b: "321"}',
125
- inputValue: {
126
- 'a': 213,
127
- 'b': '321'
128
- },
129
- expect: false
130
- },
131
- {
132
- inputName: 'object {a: 213, b: "321"}',
133
- inputValue: {
134
- 'a': 213,
135
- 'b': 777
136
- },
137
- expect: true
138
- },
139
- ...defaultItemss({}, false)
140
- ]
141
- },
142
- {
143
- 'name': 'Pre-defined IPv4 Address Checker',
144
- 'rule': '@ipv4_address',
145
- 'items': [
146
- {
147
- inputName: 'String: 1.1.1.1',
148
- inputValue: '1.1.1.1',
149
- expect: true
150
- },
151
- {
152
- inputName: 'String: 255.255.255.255',
153
- inputValue: '255.255.255.255',
154
- expect: true
155
- },
156
- {
157
- inputName: 'String: 0.0.0.0',
158
- inputValue: '0.0.0.0',
159
- expect: true
160
- },
161
- {
162
- inputName: 'String: 2552.1.1.1',
163
- inputValue: '2552.1.1.1',
164
- expect: false
165
- },
166
- {
167
- inputName: 'String: 256.1.1.1',
168
- inputValue: '256.1.1.1',
169
- expect: false
170
- },
171
- {
172
- inputName: 'String: .1.1.1',
173
- inputValue: '.1.1.1',
174
- expect: false
175
- },
176
- {
177
- inputName: 'object {a: 213, b: 321}',
178
- inputValue: {
179
- 'a': 213,
180
- 'b': 321
181
- },
182
- expect: false
183
- },
184
- {
185
- inputName: 'object {a: 213, b: "321"}',
186
- inputValue: {
187
- 'a': 213,
188
- 'b': '321'
189
- },
190
- expect: false
191
- },
192
- {
193
- inputName: 'object {a: 213, b: "321"}',
194
- inputValue: {
195
- 'a': 213,
196
- 'b': 777
197
- },
198
- expect: false
199
- },
200
- ...defaultItemss({}, false)
201
- ]
202
- },
203
- {
204
- 'name': '$.type with unsupported characters in type name',
205
- 'rule': {
206
- 'a': ['$.type', 'test=123', 'int'],
207
- 'b': '@test:a',
208
- },
209
- 'items': [
210
- {
211
- inputName: 'invalid format',
212
- inputValue: '',
213
- expect: 'throw'
214
- }
215
- ]
216
- },
217
- ]
218
- };
219
-
220
- export default createTestDefinition(testItems);
@@ -1,151 +0,0 @@
1
- /**
2
- * Copyright 2022 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
- defaultItemss,
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
- inputName: 'all string object',
36
- inputValue: { 'a': '123312312', 'b': 'ccc' },
37
- expect: true
38
- },
39
- {
40
- inputName: 'all uint32 object',
41
- inputValue: { 'a': 123312312, 'b': 0xCCC },
42
- expect: true
43
- },
44
- {
45
- inputName: 'all uint32 & string object',
46
- inputValue: { 'a': 123312312, 'b': 0xCCC },
47
- expect: true
48
- },
49
- {
50
- inputName: 'all uint32 & boolean object',
51
- inputValue: { 'a': 123312312, 'b': false },
52
- expect: false
53
- },
54
- ...defaultItemss({
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);