@litert/typeguard 1.1.0 → 1.2.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 (60) hide show
  1. package/CHANGES.md +5 -0
  2. package/lib/BuiltInTypeCompiler.d.ts +2 -2
  3. package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
  4. package/lib/BuiltInTypeCompiler.js +1 -1
  5. package/lib/BuiltInTypeCompiler.js.map +1 -1
  6. package/lib/BuiltInTypes.d.ts +1 -1
  7. package/lib/BuiltInTypes.js +1 -1
  8. package/lib/Common.d.ts +5 -1
  9. package/lib/Common.d.ts.map +1 -1
  10. package/lib/Common.js +1 -1
  11. package/lib/Compiler.d.ts +1 -1
  12. package/lib/Compiler.d.ts.map +1 -1
  13. package/lib/Compiler.js +8 -6
  14. package/lib/Compiler.js.map +1 -1
  15. package/lib/Context.d.ts +1 -1
  16. package/lib/Context.js +1 -1
  17. package/lib/FilterCompiler.d.ts +3 -3
  18. package/lib/FilterCompiler.d.ts.map +1 -1
  19. package/lib/FilterCompiler.js +2 -2
  20. package/lib/FilterCompiler.js.map +1 -1
  21. package/lib/InlineCompiler.d.ts +10 -3
  22. package/lib/InlineCompiler.d.ts.map +1 -1
  23. package/lib/InlineCompiler.js +12 -4
  24. package/lib/InlineCompiler.js.map +1 -1
  25. package/lib/Internal.d.ts +2 -1
  26. package/lib/Internal.d.ts.map +1 -1
  27. package/lib/Internal.js +3 -2
  28. package/lib/Internal.js.map +1 -1
  29. package/lib/Modifiers.d.ts +1 -1
  30. package/lib/Modifiers.js +1 -1
  31. package/lib/index.d.ts +1 -1
  32. package/lib/index.js +6 -2
  33. package/lib/index.js.map +1 -1
  34. package/lib/langs/JavaScript.d.ts +1 -1
  35. package/lib/langs/JavaScript.js +2 -2
  36. package/lib/langs/JavaScript.js.map +1 -1
  37. package/package.json +11 -11
  38. package/src/examples/quick-start.ts +1 -1
  39. package/src/lib/BuiltInTypeCompiler.ts +2 -2
  40. package/src/lib/BuiltInTypes.ts +1 -1
  41. package/src/lib/Common.ts +7 -2
  42. package/src/lib/Compiler.ts +12 -9
  43. package/src/lib/Context.ts +1 -1
  44. package/src/lib/FilterCompiler.ts +4 -4
  45. package/src/lib/InlineCompiler.ts +30 -9
  46. package/src/lib/Internal.ts +3 -1
  47. package/src/lib/Modifiers.ts +1 -1
  48. package/src/lib/index.ts +1 -1
  49. package/src/lib/langs/JavaScript.ts +2 -2
  50. package/src/test/00-all.ts +1 -1
  51. package/src/test/01-elemental-types.ts +2 -1
  52. package/src/test/02-array-and-list.ts +18 -18
  53. package/src/test/03-tuple.ts +1 -1
  54. package/src/test/04-from-string.ts +2 -1
  55. package/src/test/05-string-asserts.ts +1 -1
  56. package/src/test/06-structure.ts +26 -26
  57. package/src/test/07-modifiers.ts +71 -10
  58. package/src/test/08-map-and-dict.ts +20 -20
  59. package/src/test/09-exceptions.ts +4 -4
  60. package/src/test/abstracts.ts +12 -3
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 Angus.Fenying <fenying@litert.org>
2
+ * Copyright 2022 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.
@@ -32,7 +32,7 @@ const testItems: ITestSuite = {
32
32
  },
33
33
  {
34
34
  inputName: 'Object with one property',
35
- inputValue: {'a': 123},
35
+ inputValue: { 'a': 123 },
36
36
  expect: true
37
37
  },
38
38
  {
@@ -56,7 +56,7 @@ const testItems: ITestSuite = {
56
56
  },
57
57
  {
58
58
  inputName: 'Object with one property',
59
- inputValue: {'a': 123},
59
+ inputValue: { 'a': 123 },
60
60
  expect: true
61
61
  },
62
62
  {
@@ -80,12 +80,12 @@ const testItems: ITestSuite = {
80
80
  },
81
81
  {
82
82
  inputName: 'Object with one property',
83
- inputValue: {'a': 123},
83
+ inputValue: { 'a': 123 },
84
84
  expect: true
85
85
  },
86
86
  {
87
87
  inputName: 'Object with one mismatch-type property',
88
- inputValue: {'a': '123'},
88
+ inputValue: { 'a': '123' },
89
89
  expect: false
90
90
  },
91
91
  {
@@ -109,22 +109,22 @@ const testItems: ITestSuite = {
109
109
  },
110
110
  {
111
111
  inputName: 'Object with one property',
112
- inputValue: {'a': 123},
112
+ inputValue: { 'a': 123 },
113
113
  expect: false
114
114
  },
115
115
  {
116
116
  inputName: 'Object with mismatch-typed nest property',
117
- inputValue: {'a': 123, 'b': { 'c': 1232131 }},
117
+ inputValue: { 'a': 123, 'b': { 'c': 1232131 } },
118
118
  expect: false
119
119
  },
120
120
  {
121
121
  inputName: 'Object with one mismatch-typed property',
122
- inputValue: {'a': '123', 'b': { 'c': 'ffff'} },
122
+ inputValue: { 'a': '123', 'b': { 'c': 'ffff' } },
123
123
  expect: false
124
124
  },
125
125
  {
126
126
  inputName: 'Object with all corrected',
127
- inputValue: {'a': 123, 'b': { 'c': 'ffff'} },
127
+ inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
128
128
  expect: true
129
129
  }
130
130
  ]
@@ -142,17 +142,17 @@ const testItems: ITestSuite = {
142
142
  },
143
143
  {
144
144
  inputName: 'Object with one property',
145
- inputValue: {'a': 123},
145
+ inputValue: { 'a': 123 },
146
146
  expect: true
147
147
  },
148
148
  {
149
149
  inputName: 'Object with one mismatch-typed property',
150
- inputValue: {'a': '123' },
150
+ inputValue: { 'a': '123' },
151
151
  expect: false
152
152
  },
153
153
  {
154
154
  inputName: 'Object with 2 properties',
155
- inputValue: {'a': 123, 'b': { 'c': 'ffff'} },
155
+ inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
156
156
  expect: false
157
157
  }
158
158
  ]
@@ -168,22 +168,22 @@ const testItems: ITestSuite = {
168
168
  'items': [
169
169
  {
170
170
  inputName: 'Object with one property',
171
- inputValue: {'a': 123},
171
+ inputValue: { 'a': 123 },
172
172
  expect: false
173
173
  },
174
174
  {
175
175
  inputName: 'Object with wrong type in nest',
176
- inputValue: {'a': 123, 'b': { 'c': 'ffff'} },
176
+ inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
177
177
  expect: false
178
178
  },
179
179
  {
180
180
  inputName: 'Object with all correct',
181
- inputValue: {'a': 123, 'b': { 'c': 123 } },
181
+ inputValue: { 'a': 123, 'b': { 'c': 123 } },
182
182
  expect: true
183
183
  },
184
184
  {
185
185
  inputName: 'Object with one more in nested',
186
- inputValue: {'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
186
+ inputValue: { 'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
187
187
  expect: true
188
188
  }
189
189
  ]
@@ -199,22 +199,22 @@ const testItems: ITestSuite = {
199
199
  'items': [
200
200
  {
201
201
  inputName: 'Object with one property',
202
- inputValue: {'a': 123},
202
+ inputValue: { 'a': 123 },
203
203
  expect: false
204
204
  },
205
205
  {
206
206
  inputName: 'Object with wrong type in nest',
207
- inputValue: {'a': 123, 'b': { 'c': 'ffff'} },
207
+ inputValue: { 'a': 123, 'b': { 'c': 'ffff' } },
208
208
  expect: false
209
209
  },
210
210
  {
211
211
  inputName: 'Object with all correct',
212
- inputValue: {'a': 123, 'b': { 'c': 123 } },
212
+ inputValue: { 'a': 123, 'b': { 'c': 123 } },
213
213
  expect: true
214
214
  },
215
215
  {
216
216
  inputName: 'Object with one more in nested',
217
- inputValue: {'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
217
+ inputValue: { 'a': 123, 'b': { 'c': 123, 'd': '1213131' } },
218
218
  expect: false
219
219
  }
220
220
  ]
@@ -230,17 +230,17 @@ const testItems: ITestSuite = {
230
230
  'items': [
231
231
  {
232
232
  inputName: 'Object with all correct',
233
- inputValue: {'a': 123, 'b': { 'c': 123 } },
233
+ inputValue: { 'a': 123, 'b': { 'c': 123 } },
234
234
  expect: true
235
235
  },
236
236
  {
237
237
  inputName: 'Object with string incorrect-value',
238
- inputValue: {'a': 123, 'b': { 'c': '123d' } },
238
+ inputValue: { 'a': 123, 'b': { 'c': '123d' } },
239
239
  expect: false
240
240
  },
241
241
  {
242
242
  inputName: 'Object with string value',
243
- inputValue: {'a': '123312312', 'b': { 'c': '123' } },
243
+ inputValue: { 'a': '123312312', 'b': { 'c': '123' } },
244
244
  expect: true
245
245
  }
246
246
  ]
@@ -256,17 +256,17 @@ const testItems: ITestSuite = {
256
256
  'items': [
257
257
  {
258
258
  inputName: 'Object with all correct',
259
- inputValue: {'a': 123, 'b': { 'c': 123 } },
259
+ inputValue: { 'a': 123, 'b': { 'c': 123 } },
260
260
  expect: true
261
261
  },
262
262
  {
263
263
  inputName: 'Object with string incorrect-value',
264
- inputValue: {'a': 123, 'b': { 'c': '123d' } },
264
+ inputValue: { 'a': 123, 'b': { 'c': '123d' } },
265
265
  expect: false
266
266
  },
267
267
  {
268
268
  inputName: 'Object with string value',
269
- inputValue: {'a': '123312312', 'b': { 'c': '123' } },
269
+ inputValue: { 'a': '123312312', 'b': { 'c': '123' } },
270
270
  expect: true
271
271
  }
272
272
  ]
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 Angus.Fenying <fenying@litert.org>
2
+ * Copyright 2022 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.
@@ -23,7 +23,7 @@ const testItems: ITestSuite = {
23
23
 
24
24
  {
25
25
  'name': '$.string',
26
- 'rule': ['$.string', 'uint32', {'a': 'uint32'}],
26
+ 'rule': ['$.string', 'uint32', { 'a': 'uint32' }],
27
27
  'items': [
28
28
  {
29
29
  inputName: JSON.stringify('123'),
@@ -31,13 +31,13 @@ const testItems: ITestSuite = {
31
31
  expect: true
32
32
  },
33
33
  {
34
- inputName: JSON.stringify({'a': 123}),
35
- inputValue: {'a': 123},
34
+ inputName: JSON.stringify({ 'a': 123 }),
35
+ inputValue: { 'a': 123 },
36
36
  expect: true
37
37
  },
38
38
  {
39
- inputName: JSON.stringify({'a': 123}),
40
- inputValue: {'a': 123},
39
+ inputName: JSON.stringify({ 'a': 123 }),
40
+ inputValue: { 'a': 123 },
41
41
  expect: true
42
42
  },
43
43
  ...defaultItemss({
@@ -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
+ inputName: JSON.stringify([{ 'a': 'x' }, { 'a': 'c', 'b': 'x' }]),
70
+ inputValue: [{ '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
+ inputName: JSON.stringify({ 'a': { 'b': 'c', 'd': 'ccc' } }),
87
+ inputValue: { 'a': { 'b': 'c', 'd': 'ccc' } },
88
88
  expect: true
89
89
  }
90
90
  ]
@@ -139,6 +139,67 @@ const testItems: ITestSuite = {
139
139
  ...defaultItemss({}, false)
140
140
  ]
141
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
+ },
142
203
  {
143
204
  'name': '$.type with unsupported characters in type name',
144
205
  'rule': {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 Angus.Fenying <fenying@litert.org>
2
+ * Copyright 2022 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.
@@ -33,22 +33,22 @@ const testItems: ITestSuite = {
33
33
  'items': [
34
34
  {
35
35
  inputName: 'all string object',
36
- inputValue: {'a': '123312312', 'b': 'ccc' },
36
+ inputValue: { 'a': '123312312', 'b': 'ccc' },
37
37
  expect: true
38
38
  },
39
39
  {
40
40
  inputName: 'all uint32 object',
41
- inputValue: {'a': 123312312, 'b': 0xCCC },
41
+ inputValue: { 'a': 123312312, 'b': 0xCCC },
42
42
  expect: true
43
43
  },
44
44
  {
45
45
  inputName: 'all uint32 & string object',
46
- inputValue: {'a': 123312312, 'b': 0xCCC },
46
+ inputValue: { 'a': 123312312, 'b': 0xCCC },
47
47
  expect: true
48
48
  },
49
49
  {
50
50
  inputName: 'all uint32 & boolean object',
51
- inputValue: {'a': 123312312, 'b': false },
51
+ inputValue: { 'a': 123312312, 'b': false },
52
52
  expect: false
53
53
  },
54
54
  ...defaultItemss({
@@ -67,15 +67,15 @@ const testItems: ITestSuite = {
67
67
  },
68
68
  'items': [
69
69
  assertItem(
70
- {'a': '123312312', 'b': 123.3, c: 1232 },
70
+ { 'a': '123312312', 'b': 123.3, c: 1232 },
71
71
  true
72
72
  ),
73
73
  assertItem(
74
- {'a': '123312312', 'b': 'ccc' },
74
+ { 'a': '123312312', 'b': 'ccc' },
75
75
  false
76
76
  ),
77
77
  assertItem(
78
- {'a': 123312312, 'b': 123.3 },
78
+ { 'a': 123312312, 'b': 123.3 },
79
79
  false
80
80
  ),
81
81
  ]
@@ -91,56 +91,56 @@ const testItems: ITestSuite = {
91
91
  },
92
92
  'items': [
93
93
  assertItem(
94
- {'a': '123312312', 'b': 'ccc' },
94
+ { 'a': '123312312', 'b': 'ccc' },
95
95
  false
96
96
  ),
97
97
  assertItem(
98
- {'a': 123312312, 'b': 123.3 },
98
+ { 'a': 123312312, 'b': 123.3 },
99
99
  false
100
100
  ),
101
101
  assertItem(
102
- {'a': '123312312', 'b': {d: 123.3, c: 1232} },
102
+ { 'a': '123312312', 'b': { d: 123.3, c: 1232 } },
103
103
  true
104
104
  ),
105
105
  assertItem(
106
- {'a': '123312312', 'b': { d: 123.3, c: '1232' } },
106
+ { 'a': '123312312', 'b': { d: 123.3, c: '1232' } },
107
107
  false
108
108
  ),
109
109
  ]
110
110
  },
111
111
  addRule(['$.dict', ['a', 'b'], 'string'], [
112
112
  assertItem(
113
- {'a': '123312312', 'b': 'ccc' },
113
+ { 'a': '123312312', 'b': 'ccc' },
114
114
  true
115
115
  ),
116
116
  assertItem(
117
- {'a': '123312312', 'b': 'ccc', 'c': 123 },
117
+ { 'a': '123312312', 'b': 'ccc', 'c': 123 },
118
118
  true
119
119
  ),
120
120
  assertItem(
121
- {'a': '123312312' },
121
+ { 'a': '123312312' },
122
122
  false
123
123
  ),
124
124
  assertItem(
125
- {'a': '123312312', 'c': 'ddd' },
125
+ { 'a': '123312312', 'c': 'ddd' },
126
126
  false
127
127
  ),
128
128
  assertItem(
129
- {'a': '123312312', 'b': 123.3 },
129
+ { 'a': '123312312', 'b': 123.3 },
130
130
  false
131
131
  )
132
132
  ]),
133
133
  addRule(['$.strict', '$.dict', ['a', 'b'], 'string'], [
134
134
  assertItem(
135
- {'a': '123312312', 'b': 'ccc' },
135
+ { 'a': '123312312', 'b': 'ccc' },
136
136
  true
137
137
  ),
138
138
  assertItem(
139
- {'a': '123312312', 'b': 'ccc', 'c': 123 },
139
+ { 'a': '123312312', 'b': 'ccc', 'c': 123 },
140
140
  false
141
141
  ),
142
142
  assertItem(
143
- {'a': '123312312' },
143
+ { 'a': '123312312' },
144
144
  false
145
145
  ),
146
146
  ])
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 Angus.Fenying <fenying@litert.org>
2
+ * Copyright 2022 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.
@@ -30,7 +30,7 @@ const testItems: ITestSuite = {
30
30
  'items': [
31
31
  {
32
32
  inputName: 'When test === \'ffff\'',
33
- inputValue: {'test': 'ffff'},
33
+ inputValue: { 'test': 'ffff' },
34
34
  expect: true
35
35
  },
36
36
  {
@@ -46,12 +46,12 @@ const testItems: ITestSuite = {
46
46
  'items': [
47
47
  {
48
48
  inputName: 'When test === 123',
49
- inputValue: {'test': 123},
49
+ inputValue: { 'test': 123 },
50
50
  expect: true
51
51
  },
52
52
  {
53
53
  inputName: 'When test === 1234',
54
- inputValue: {'test': 1234},
54
+ inputValue: { 'test': 1234 },
55
55
  expect: false
56
56
  },
57
57
  {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2021 Angus.Fenying <fenying@litert.org>
2
+ * Copyright 2022 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.
@@ -103,6 +103,15 @@ export function defaultItemss(
103
103
 
104
104
  const compiler = TypeGuard.createInlineCompiler();
105
105
 
106
+ compiler.addPredefinedType<string>(
107
+ 'ipv4_address',
108
+ function(i): i is string {
109
+ return typeof i === 'string'
110
+ && /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(i)
111
+ && i.split('.').map(x => parseInt(x, 10)).every(x => x >= 0 && x <= 255);
112
+ }
113
+ );
114
+
106
115
  export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
107
116
 
108
117
  return {
@@ -127,7 +136,7 @@ export function createTestDefinition(suite: ITestSuite) {
127
136
 
128
137
  describe(suite.name, function() {
129
138
 
130
- for (let section of suite.sections) {
139
+ for (const section of suite.sections) {
131
140
 
132
141
  describe(section.name, function() {
133
142
 
@@ -152,7 +161,7 @@ export function createTestDefinition(suite: ITestSuite) {
152
161
  'rule': section.rule
153
162
  });
154
163
 
155
- for (let item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
164
+ for (const item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
156
165
 
157
166
  it(`${
158
167
  item.expect ? 'PASSED' : 'REJECTED'