@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.
- package/CHANGES.md +16 -0
- package/lib/BuiltInTypeCompiler.d.ts +4 -4
- package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
- package/lib/BuiltInTypeCompiler.js +167 -166
- package/lib/BuiltInTypeCompiler.js.map +1 -1
- package/lib/BuiltInTypes.d.ts +1 -1
- package/lib/BuiltInTypes.js +37 -36
- package/lib/BuiltInTypes.js.map +1 -1
- package/lib/Common.d.ts +27 -10
- package/lib/Common.d.ts.map +1 -1
- package/lib/Common.js +1 -1
- package/lib/Compiler.d.ts +2 -2
- package/lib/Compiler.d.ts.map +1 -1
- package/lib/Compiler.js +159 -102
- package/lib/Compiler.js.map +1 -1
- package/lib/Context.d.ts +6 -5
- package/lib/Context.d.ts.map +1 -1
- package/lib/Context.js +11 -7
- package/lib/Context.js.map +1 -1
- package/lib/FilterCompiler.d.ts +5 -5
- package/lib/FilterCompiler.d.ts.map +1 -1
- package/lib/FilterCompiler.js +25 -24
- package/lib/FilterCompiler.js.map +1 -1
- package/lib/InlineCompiler.d.ts +12 -5
- package/lib/InlineCompiler.d.ts.map +1 -1
- package/lib/InlineCompiler.js +18 -6
- package/lib/InlineCompiler.js.map +1 -1
- package/lib/Internal.d.ts +6 -4
- package/lib/Internal.d.ts.map +1 -1
- package/lib/Internal.js +12 -10
- package/lib/Internal.js.map +1 -1
- package/lib/Modifiers.d.ts +1 -1
- package/lib/Modifiers.js +2 -1
- package/lib/Modifiers.js.map +1 -1
- package/lib/index.d.ts +5 -5
- package/lib/index.js +19 -7
- package/lib/index.js.map +1 -1
- package/lib/langs/JavaScript.d.ts +2 -2
- package/lib/langs/JavaScript.d.ts.map +1 -1
- package/lib/langs/JavaScript.js +55 -29
- package/lib/langs/JavaScript.js.map +1 -1
- package/package.json +17 -13
- package/src/examples/quick-start.ts +172 -0
- package/src/lib/BuiltInTypeCompiler.ts +171 -171
- package/src/lib/BuiltInTypes.ts +36 -36
- package/src/lib/Common.ts +49 -10
- package/src/lib/Compiler.ts +354 -247
- package/src/lib/Context.ts +11 -13
- package/src/lib/FilterCompiler.ts +84 -84
- package/src/lib/InlineCompiler.ts +41 -15
- package/src/lib/Internal.ts +17 -13
- package/src/lib/Modifiers.ts +2 -2
- package/src/lib/index.ts +5 -5
- package/src/lib/langs/JavaScript.ts +84 -31
- package/src/test/00-all.ts +10 -10
- package/src/test/01-elemental-types.ts +1111 -1110
- package/src/test/02-array-and-list.ts +75 -75
- package/src/test/03-tuple.ts +87 -87
- package/src/test/04-from-string.ts +849 -848
- package/src/test/05-string-asserts.ts +422 -422
- package/src/test/06-structure.ts +107 -107
- package/src/test/07-modifiers.ts +151 -42
- package/src/test/08-map-and-dict.ts +46 -46
- package/src/test/09-exceptions.ts +30 -9
- package/src/test/abstracts.ts +83 -45
- package/dist/typeguard.amd.d.ts +0 -588
- package/dist/typeguard.amd.d.ts.map +0 -1
- package/dist/typeguard.amd.js +0 -2069
- package/dist/typeguard.amd.js.map +0 -1
- package/dist/typeguard.system.d.ts +0 -588
- package/dist/typeguard.system.d.ts.map +0 -1
- package/dist/typeguard.system.js +0 -2185
- package/dist/typeguard.system.js.map +0 -1
- package/src/samples/quick-start.ts +0 -52
- package/tsconfig-amd.json +0 -72
- package/tsconfig-systemjs.json +0 -72
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
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
|
|
23
|
+
} from './abstracts';
|
|
24
24
|
|
|
25
25
|
const testItems: ITestSuite = {
|
|
26
26
|
|
|
27
|
-
name:
|
|
27
|
+
name: 'Mappings & Dicts',
|
|
28
28
|
sections: [
|
|
29
29
|
|
|
30
30
|
{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
'name': JSON.stringify(['$.map', 'string', 'uint32']),
|
|
32
|
+
'rule': ['$.map', 'string', 'uint32'],
|
|
33
|
+
'items': [
|
|
34
34
|
{
|
|
35
|
-
inputName:
|
|
36
|
-
inputValue: {
|
|
35
|
+
inputName: 'all string object',
|
|
36
|
+
inputValue: { 'a': '123312312', 'b': 'ccc' },
|
|
37
37
|
expect: true
|
|
38
38
|
},
|
|
39
39
|
{
|
|
40
|
-
inputName:
|
|
41
|
-
inputValue: {
|
|
40
|
+
inputName: 'all uint32 object',
|
|
41
|
+
inputValue: { 'a': 123312312, 'b': 0xCCC },
|
|
42
42
|
expect: true
|
|
43
43
|
},
|
|
44
44
|
{
|
|
45
|
-
inputName:
|
|
46
|
-
inputValue: {
|
|
45
|
+
inputName: 'all uint32 & string object',
|
|
46
|
+
inputValue: { 'a': 123312312, 'b': 0xCCC },
|
|
47
47
|
expect: true
|
|
48
48
|
},
|
|
49
49
|
{
|
|
50
|
-
inputName:
|
|
51
|
-
inputValue: {
|
|
50
|
+
inputName: 'all uint32 & boolean object',
|
|
51
|
+
inputValue: { 'a': 123312312, 'b': false },
|
|
52
52
|
expect: false
|
|
53
53
|
},
|
|
54
54
|
...defaultItemss({
|
|
55
|
-
|
|
55
|
+
'object': true
|
|
56
56
|
})
|
|
57
57
|
]
|
|
58
58
|
},
|
|
59
59
|
{
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
'name': JSON.stringify({
|
|
61
|
+
'a': 'string',
|
|
62
|
+
'$.map': 'number'
|
|
63
63
|
}),
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
'rule': {
|
|
65
|
+
'a': 'string',
|
|
66
|
+
'$.map': 'number'
|
|
67
67
|
},
|
|
68
|
-
|
|
68
|
+
'items': [
|
|
69
69
|
assertItem(
|
|
70
|
-
{
|
|
70
|
+
{ 'a': '123312312', 'b': 123.3, c: 1232 },
|
|
71
71
|
true
|
|
72
72
|
),
|
|
73
73
|
assertItem(
|
|
74
|
-
{
|
|
74
|
+
{ 'a': '123312312', 'b': 'ccc' },
|
|
75
75
|
false
|
|
76
76
|
),
|
|
77
77
|
assertItem(
|
|
78
|
-
{
|
|
78
|
+
{ 'a': 123312312, 'b': 123.3 },
|
|
79
79
|
false
|
|
80
80
|
),
|
|
81
81
|
]
|
|
82
82
|
},
|
|
83
83
|
{
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
'name': JSON.stringify({
|
|
85
|
+
'a': 'string',
|
|
86
|
+
'b->{}': 'number'
|
|
87
87
|
}),
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
'rule': {
|
|
89
|
+
'a': 'string',
|
|
90
|
+
'b->{}': 'number'
|
|
91
91
|
},
|
|
92
|
-
|
|
92
|
+
'items': [
|
|
93
93
|
assertItem(
|
|
94
|
-
{
|
|
94
|
+
{ 'a': '123312312', 'b': 'ccc' },
|
|
95
95
|
false
|
|
96
96
|
),
|
|
97
97
|
assertItem(
|
|
98
|
-
{
|
|
98
|
+
{ 'a': 123312312, 'b': 123.3 },
|
|
99
99
|
false
|
|
100
100
|
),
|
|
101
101
|
assertItem(
|
|
102
|
-
{
|
|
102
|
+
{ 'a': '123312312', 'b': { d: 123.3, c: 1232 } },
|
|
103
103
|
true
|
|
104
104
|
),
|
|
105
105
|
assertItem(
|
|
106
|
-
{
|
|
106
|
+
{ 'a': '123312312', 'b': { d: 123.3, c: '1232' } },
|
|
107
107
|
false
|
|
108
108
|
),
|
|
109
109
|
]
|
|
110
110
|
},
|
|
111
|
-
addRule([
|
|
111
|
+
addRule(['$.dict', ['a', 'b'], 'string'], [
|
|
112
112
|
assertItem(
|
|
113
|
-
{
|
|
113
|
+
{ 'a': '123312312', 'b': 'ccc' },
|
|
114
114
|
true
|
|
115
115
|
),
|
|
116
116
|
assertItem(
|
|
117
|
-
{
|
|
117
|
+
{ 'a': '123312312', 'b': 'ccc', 'c': 123 },
|
|
118
118
|
true
|
|
119
119
|
),
|
|
120
120
|
assertItem(
|
|
121
|
-
{
|
|
121
|
+
{ 'a': '123312312' },
|
|
122
122
|
false
|
|
123
123
|
),
|
|
124
124
|
assertItem(
|
|
125
|
-
{
|
|
125
|
+
{ 'a': '123312312', 'c': 'ddd' },
|
|
126
126
|
false
|
|
127
127
|
),
|
|
128
128
|
assertItem(
|
|
129
|
-
{
|
|
129
|
+
{ 'a': '123312312', 'b': 123.3 },
|
|
130
130
|
false
|
|
131
131
|
)
|
|
132
132
|
]),
|
|
133
|
-
addRule([
|
|
133
|
+
addRule(['$.strict', '$.dict', ['a', 'b'], 'string'], [
|
|
134
134
|
assertItem(
|
|
135
|
-
{
|
|
135
|
+
{ 'a': '123312312', 'b': 'ccc' },
|
|
136
136
|
true
|
|
137
137
|
),
|
|
138
138
|
assertItem(
|
|
139
|
-
{
|
|
139
|
+
{ 'a': '123312312', 'b': 'ccc', 'c': 123 },
|
|
140
140
|
false
|
|
141
141
|
),
|
|
142
142
|
assertItem(
|
|
143
|
-
{
|
|
143
|
+
{ 'a': '123312312' },
|
|
144
144
|
false
|
|
145
145
|
),
|
|
146
146
|
])
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
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
|
|
20
|
+
} from './abstracts';
|
|
21
21
|
|
|
22
22
|
const testItems: ITestSuite = {
|
|
23
23
|
|
|
24
|
-
name:
|
|
24
|
+
name: 'When optional with any',
|
|
25
25
|
sections: [
|
|
26
26
|
|
|
27
27
|
{
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
'name': JSON.stringify({ 'test?': 'any' }),
|
|
29
|
+
'rule': { 'test?': 'any' },
|
|
30
|
+
'items': [
|
|
31
31
|
{
|
|
32
|
-
inputName:
|
|
33
|
-
inputValue: {
|
|
32
|
+
inputName: 'When test === \'ffff\'',
|
|
33
|
+
inputValue: { 'test': 'ffff' },
|
|
34
34
|
expect: true
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
|
-
inputName:
|
|
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
|
|
package/src/test/abstracts.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
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
|
|
18
|
-
import * as TypeGuard from
|
|
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 |
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
'inputName': 'true',
|
|
53
|
+
'inputValue': true,
|
|
54
|
+
'expect': items['true'] === undefined ? defaultValue : items['true']
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
'inputName': 'false',
|
|
58
|
+
'inputValue': false,
|
|
59
|
+
'expect': items['false'] === undefined ? defaultValue : items['false']
|
|
60
60
|
},
|
|
61
61
|
{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
'inputName': 'undefined',
|
|
63
|
+
'inputValue': undefined,
|
|
64
|
+
'expect': items['undefined'] === undefined ? defaultValue : items['undefined']
|
|
65
65
|
},
|
|
66
66
|
{
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
'inputName': 'null',
|
|
68
|
+
'inputValue': null,
|
|
69
|
+
'expect': items['null'] === undefined ? defaultValue : items['null']
|
|
70
70
|
},
|
|
71
71
|
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
'inputName': 'empty array',
|
|
73
|
+
'inputValue': [],
|
|
74
|
+
'expect': items['empty array'] === undefined ? defaultValue : items['empty array']
|
|
75
75
|
},
|
|
76
76
|
{
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
'inputName': 'string \'hello\'',
|
|
78
|
+
'inputValue': 'hello',
|
|
79
|
+
'expect': items['string \'hello\''] === undefined ? defaultValue : items['string \'hello\'']
|
|
80
80
|
},
|
|
81
81
|
{
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
'inputName': 'empty string',
|
|
83
|
+
'inputValue': '',
|
|
84
|
+
'expect': items['empty string'] === undefined ? defaultValue : items['empty string']
|
|
85
85
|
},
|
|
86
86
|
{
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
'inputName': 'object',
|
|
88
|
+
'inputValue': {},
|
|
89
|
+
'expect': items['object'] === undefined ? defaultValue : items['object']
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
'inputName': 'number 0',
|
|
93
|
+
'inputValue': 0,
|
|
94
|
+
'expect': items['number 0'] === undefined ? defaultValue : items['number 0']
|
|
95
95
|
},
|
|
96
96
|
{
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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:
|
|
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 (
|
|
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 ===
|
|
156
|
+
section.items[0].expect === 'throw'
|
|
137
157
|
) {
|
|
138
158
|
|
|
139
|
-
it(
|
|
159
|
+
it('Throws exception.', function() {
|
|
140
160
|
|
|
141
161
|
assert.throws(() => {
|
|
142
162
|
|
|
143
163
|
compiler.compile<any>({
|
|
144
|
-
|
|
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
|
|
152
|
-
|
|
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 (
|
|
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 ?
|
|
191
|
+
item.expect ? 'PASSED' : 'REJECTED'
|
|
159
192
|
} when input ${
|
|
160
193
|
item.inputName
|
|
161
194
|
}.`, function() {
|
|
162
195
|
|
|
163
196
|
assert.equal(
|
|
164
|
-
|
|
197
|
+
checkWithTrace(item.inputValue),
|
|
198
|
+
item.expect
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
assert.equal(
|
|
202
|
+
checkWithoutTrace(item.inputValue),
|
|
165
203
|
item.expect
|
|
166
204
|
);
|
|
167
205
|
});
|