@litert/typeguard 1.0.1 → 1.3.0-dev.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 (76) hide show
  1. package/CHANGES.md +16 -0
  2. package/lib/BuiltInTypeCompiler.d.ts +4 -4
  3. package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
  4. package/lib/BuiltInTypeCompiler.js +167 -166
  5. package/lib/BuiltInTypeCompiler.js.map +1 -1
  6. package/lib/BuiltInTypes.d.ts +1 -1
  7. package/lib/BuiltInTypes.js +37 -36
  8. package/lib/BuiltInTypes.js.map +1 -1
  9. package/lib/Common.d.ts +27 -10
  10. package/lib/Common.d.ts.map +1 -1
  11. package/lib/Common.js +1 -1
  12. package/lib/Compiler.d.ts +2 -2
  13. package/lib/Compiler.d.ts.map +1 -1
  14. package/lib/Compiler.js +159 -102
  15. package/lib/Compiler.js.map +1 -1
  16. package/lib/Context.d.ts +6 -5
  17. package/lib/Context.d.ts.map +1 -1
  18. package/lib/Context.js +11 -7
  19. package/lib/Context.js.map +1 -1
  20. package/lib/FilterCompiler.d.ts +5 -5
  21. package/lib/FilterCompiler.d.ts.map +1 -1
  22. package/lib/FilterCompiler.js +25 -24
  23. package/lib/FilterCompiler.js.map +1 -1
  24. package/lib/InlineCompiler.d.ts +12 -5
  25. package/lib/InlineCompiler.d.ts.map +1 -1
  26. package/lib/InlineCompiler.js +18 -6
  27. package/lib/InlineCompiler.js.map +1 -1
  28. package/lib/Internal.d.ts +6 -4
  29. package/lib/Internal.d.ts.map +1 -1
  30. package/lib/Internal.js +12 -10
  31. package/lib/Internal.js.map +1 -1
  32. package/lib/Modifiers.d.ts +1 -1
  33. package/lib/Modifiers.js +2 -1
  34. package/lib/Modifiers.js.map +1 -1
  35. package/lib/index.d.ts +5 -5
  36. package/lib/index.js +19 -7
  37. package/lib/index.js.map +1 -1
  38. package/lib/langs/JavaScript.d.ts +2 -2
  39. package/lib/langs/JavaScript.d.ts.map +1 -1
  40. package/lib/langs/JavaScript.js +55 -29
  41. package/lib/langs/JavaScript.js.map +1 -1
  42. package/package.json +17 -13
  43. package/src/examples/quick-start.ts +172 -0
  44. package/src/lib/BuiltInTypeCompiler.ts +171 -171
  45. package/src/lib/BuiltInTypes.ts +36 -36
  46. package/src/lib/Common.ts +49 -10
  47. package/src/lib/Compiler.ts +354 -247
  48. package/src/lib/Context.ts +11 -13
  49. package/src/lib/FilterCompiler.ts +84 -84
  50. package/src/lib/InlineCompiler.ts +41 -15
  51. package/src/lib/Internal.ts +17 -13
  52. package/src/lib/Modifiers.ts +2 -2
  53. package/src/lib/index.ts +5 -5
  54. package/src/lib/langs/JavaScript.ts +84 -31
  55. package/src/test/00-all.ts +10 -10
  56. package/src/test/01-elemental-types.ts +1111 -1110
  57. package/src/test/02-array-and-list.ts +75 -75
  58. package/src/test/03-tuple.ts +87 -87
  59. package/src/test/04-from-string.ts +849 -848
  60. package/src/test/05-string-asserts.ts +422 -422
  61. package/src/test/06-structure.ts +107 -107
  62. package/src/test/07-modifiers.ts +151 -42
  63. package/src/test/08-map-and-dict.ts +46 -46
  64. package/src/test/09-exceptions.ts +30 -9
  65. package/src/test/abstracts.ts +83 -45
  66. package/dist/typeguard.amd.d.ts +0 -588
  67. package/dist/typeguard.amd.d.ts.map +0 -1
  68. package/dist/typeguard.amd.js +0 -2069
  69. package/dist/typeguard.amd.js.map +0 -1
  70. package/dist/typeguard.system.d.ts +0 -588
  71. package/dist/typeguard.system.d.ts.map +0 -1
  72. package/dist/typeguard.system.js +0 -2185
  73. package/dist/typeguard.system.js.map +0 -1
  74. package/src/samples/quick-start.ts +0 -52
  75. package/tsconfig-amd.json +0 -72
  76. package/tsconfig-systemjs.json +0 -72
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2019 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.
@@ -20,127 +20,127 @@ import {
20
20
  ITestSuite,
21
21
  assertItem,
22
22
  addRule
23
- } from "./abstracts";
23
+ } from './abstracts';
24
24
 
25
25
  const testItems: ITestSuite = {
26
26
 
27
- name: "Mappings & Dicts",
27
+ name: 'Mappings & Dicts',
28
28
  sections: [
29
29
 
30
30
  {
31
- "name": JSON.stringify(["$.map", "string", "uint32"]),
32
- "rule": ["$.map", "string", "uint32"],
33
- "items": [
31
+ 'name': JSON.stringify(['$.map', 'string', 'uint32']),
32
+ 'rule': ['$.map', 'string', 'uint32'],
33
+ 'items': [
34
34
  {
35
- inputName: "all string object",
36
- inputValue: {"a": "123312312", "b": "ccc" },
35
+ inputName: 'all string object',
36
+ inputValue: { 'a': '123312312', 'b': 'ccc' },
37
37
  expect: true
38
38
  },
39
39
  {
40
- inputName: "all uint32 object",
41
- inputValue: {"a": 123312312, "b": 0xCCC },
40
+ inputName: 'all uint32 object',
41
+ inputValue: { '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
+ inputName: 'all uint32 & string object',
46
+ inputValue: { '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
+ inputName: 'all uint32 & boolean object',
51
+ inputValue: { 'a': 123312312, 'b': false },
52
52
  expect: false
53
53
  },
54
54
  ...defaultItemss({
55
- "object": true
55
+ 'object': true
56
56
  })
57
57
  ]
58
58
  },
59
59
  {
60
- "name": JSON.stringify({
61
- "a": "string",
62
- "$.map": "number"
60
+ 'name': JSON.stringify({
61
+ 'a': 'string',
62
+ '$.map': 'number'
63
63
  }),
64
- "rule": {
65
- "a": "string",
66
- "$.map": "number"
64
+ 'rule': {
65
+ 'a': 'string',
66
+ '$.map': 'number'
67
67
  },
68
- "items": [
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
  ]
82
82
  },
83
83
  {
84
- "name": JSON.stringify({
85
- "a": "string",
86
- "b->{}": "number"
84
+ 'name': JSON.stringify({
85
+ 'a': 'string',
86
+ 'b->{}': 'number'
87
87
  }),
88
- "rule": {
89
- "a": "string",
90
- "b->{}": "number"
88
+ 'rule': {
89
+ 'a': 'string',
90
+ 'b->{}': 'number'
91
91
  },
92
- "items": [
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
- addRule(["$.dict", ["a", "b"], "string"], [
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
- addRule(["$.strict", "$.dict", ["a", "b"], "string"], [
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 2019 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.
@@ -17,29 +17,50 @@
17
17
  import {
18
18
  createTestDefinition,
19
19
  ITestSuite
20
- } from "./abstracts";
20
+ } from './abstracts';
21
21
 
22
22
  const testItems: ITestSuite = {
23
23
 
24
- name: "When optional with any",
24
+ name: 'When optional with any',
25
25
  sections: [
26
26
 
27
27
  {
28
- "name": JSON.stringify({ "test?": "any" }),
29
- "rule": { "test?": "any" },
30
- "items": [
28
+ 'name': JSON.stringify({ 'test?': 'any' }),
29
+ 'rule': { 'test?': 'any' },
30
+ 'items': [
31
31
  {
32
- inputName: "When test === 'ffff'",
33
- inputValue: {"test": "ffff"},
32
+ inputName: 'When test === \'ffff\'',
33
+ inputValue: { 'test': 'ffff' },
34
34
  expect: true
35
35
  },
36
36
  {
37
- inputName: "When test is omitted",
37
+ inputName: 'When test is omitted',
38
38
  inputValue: {},
39
39
  expect: true
40
40
  }
41
41
  ]
42
42
  },
43
+ {
44
+ 'name': JSON.stringify({ 'test': 'int8' }),
45
+ 'rule': { 'test': 'int8' },
46
+ 'items': [
47
+ {
48
+ inputName: 'When test === 123',
49
+ inputValue: { 'test': 123 },
50
+ expect: true
51
+ },
52
+ {
53
+ inputName: 'When test === 1234',
54
+ inputValue: { 'test': 1234 },
55
+ expect: false
56
+ },
57
+ {
58
+ inputName: 'When test is omitted',
59
+ inputValue: {},
60
+ expect: false
61
+ }
62
+ ]
63
+ },
43
64
  ]
44
65
  };
45
66
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright 2019 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.
@@ -14,8 +14,8 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import * as assert from "assert";
18
- import * as TypeGuard from "../lib";
17
+ import * as assert from 'assert';
18
+ import * as TypeGuard from '../lib';
19
19
 
20
20
  export interface ITestItem {
21
21
 
@@ -23,7 +23,7 @@ export interface ITestItem {
23
23
 
24
24
  inputValue: any;
25
25
 
26
- expect: boolean | "throw";
26
+ expect: boolean | 'throw';
27
27
  }
28
28
 
29
29
  export interface ITestRule {
@@ -49,61 +49,81 @@ export function defaultItemss(
49
49
 
50
50
  return [
51
51
  {
52
- "inputName": "true",
53
- "inputValue": true,
54
- "expect": items["true"] === undefined ? defaultValue : items["true"]
52
+ 'inputName': 'true',
53
+ 'inputValue': true,
54
+ 'expect': items['true'] === undefined ? defaultValue : items['true']
55
55
  },
56
56
  {
57
- "inputName": "false",
58
- "inputValue": false,
59
- "expect": items["false"] === undefined ? defaultValue : items["false"]
57
+ 'inputName': 'false',
58
+ 'inputValue': false,
59
+ 'expect': items['false'] === undefined ? defaultValue : items['false']
60
60
  },
61
61
  {
62
- "inputName": "undefined",
63
- "inputValue": undefined,
64
- "expect": items["undefined"] === undefined ? defaultValue : items["undefined"]
62
+ 'inputName': 'undefined',
63
+ 'inputValue': undefined,
64
+ 'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
65
65
  },
66
66
  {
67
- "inputName": "null",
68
- "inputValue": null,
69
- "expect": items["null"] === undefined ? defaultValue : items["null"]
67
+ 'inputName': 'null',
68
+ 'inputValue': null,
69
+ 'expect': items['null'] === undefined ? defaultValue : items['null']
70
70
  },
71
71
  {
72
- "inputName": "empty array",
73
- "inputValue": [],
74
- "expect": items["empty array"] === undefined ? defaultValue : items["empty array"]
72
+ 'inputName': 'empty array',
73
+ 'inputValue': [],
74
+ 'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
75
75
  },
76
76
  {
77
- "inputName": "string 'hello'",
78
- "inputValue": "hello",
79
- "expect": items["string 'hello'"] === undefined ? defaultValue : items["string 'hello'"]
77
+ 'inputName': 'string \'hello\'',
78
+ 'inputValue': 'hello',
79
+ 'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
80
80
  },
81
81
  {
82
- "inputName": "empty string",
83
- "inputValue": "",
84
- "expect": items["empty string"] === undefined ? defaultValue : items["empty string"]
82
+ 'inputName': 'empty string',
83
+ 'inputValue': '',
84
+ 'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
85
85
  },
86
86
  {
87
- "inputName": "object",
88
- "inputValue": {},
89
- "expect": items["object"] === undefined ? defaultValue : items["object"]
87
+ 'inputName': 'object',
88
+ 'inputValue': {},
89
+ 'expect': items['object'] === undefined ? defaultValue : items['object']
90
90
  },
91
91
  {
92
- "inputName": "number 0",
93
- "inputValue": 0,
94
- "expect": items["number 0"] === undefined ? defaultValue : items["number 0"]
92
+ 'inputName': 'number 0',
93
+ 'inputValue': 0,
94
+ 'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
95
95
  },
96
96
  {
97
- "inputName": "number 1",
98
- "inputValue": 1,
99
- "expect": items["number 1"] === undefined ? defaultValue : items["number 1"]
97
+ 'inputName': 'number 1',
98
+ 'inputValue': 1,
99
+ 'expect': items['number 1'] === undefined ? defaultValue : items['number 1']
100
100
  }
101
101
  ];
102
102
  }
103
103
 
104
104
  const compiler = TypeGuard.createInlineCompiler();
105
105
 
106
- export function assertItem(input: any, expect: boolean | "throw"): ITestItem {
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
+
115
+ const compiler2 = TypeGuard.createInlineCompiler();
116
+
117
+ compiler2.addPredefinedType<string>(
118
+ 'ipv4_address',
119
+ function(i): i is string {
120
+ return typeof i === 'string'
121
+ && /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(i)
122
+ && i.split('.').map(x => parseInt(x, 10)).every(x => x >= 0 && x <= 255);
123
+ }
124
+ );
125
+
126
+ export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
107
127
 
108
128
  return {
109
129
  inputName: JSON.stringify(input),
@@ -112,7 +132,7 @@ export function assertItem(input: any, expect: boolean | "throw"): ITestItem {
112
132
  };
113
133
  }
114
134
 
115
- export function addRule(rule: any, items: ITestItem[]): ITestRule {
135
+ export function addRule(rule: unknown, items: ITestItem[]): ITestRule {
116
136
 
117
137
  return {
118
138
  name: JSON.stringify(rule),
@@ -127,41 +147,59 @@ export function createTestDefinition(suite: ITestSuite) {
127
147
 
128
148
  describe(suite.name, function() {
129
149
 
130
- for (let section of suite.sections) {
150
+ for (const section of suite.sections) {
131
151
 
132
152
  describe(section.name, function() {
133
153
 
134
154
  if (
135
155
  section.items.length === 1 &&
136
- section.items[0].expect === "throw"
156
+ section.items[0].expect === 'throw'
137
157
  ) {
138
158
 
139
- it(`Throws exception.`, function() {
159
+ it('Throws exception.', function() {
140
160
 
141
161
  assert.throws(() => {
142
162
 
143
163
  compiler.compile<any>({
144
- "rule": section.rule
164
+ 'rule': section.rule,
165
+ traceErrors: true,
166
+ });
167
+ });
168
+
169
+ assert.throws(() => {
170
+
171
+ compiler2.compile<any>({
172
+ 'rule': section.rule,
145
173
  });
146
174
  });
147
175
  });
148
176
  return;
149
177
  }
150
178
 
151
- const check = compiler.compile<any>({
152
- "rule": section.rule
179
+ const checkWithTrace = compiler.compile<any>({
180
+ 'rule': section.rule,
181
+ traceErrors: true,
182
+ });
183
+
184
+ const checkWithoutTrace = compiler2.compile<any>({
185
+ 'rule': section.rule,
153
186
  });
154
187
 
155
- for (let item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
188
+ for (const item of section.items.sort((a, b) => a.expect === b.expect ? 0 : (a.expect ? -1 : 1))) {
156
189
 
157
190
  it(`${
158
- item.expect ? "PASSED" : "REJECTED"
191
+ item.expect ? 'PASSED' : 'REJECTED'
159
192
  } when input ${
160
193
  item.inputName
161
194
  }.`, function() {
162
195
 
163
196
  assert.equal(
164
- check(item.inputValue),
197
+ checkWithTrace(item.inputValue),
198
+ item.expect
199
+ );
200
+
201
+ assert.equal(
202
+ checkWithoutTrace(item.inputValue),
165
203
  item.expect
166
204
  );
167
205
  });