@litert/typeguard 1.0.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.
- package/CHANGES.md +15 -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 +13 -9
- 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 +77 -72
- package/lib/Compiler.js.map +1 -1
- package/lib/Context.d.ts +2 -2
- package/lib/Context.js +3 -2
- 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 +14 -5
- package/lib/InlineCompiler.js.map +1 -1
- package/lib/Internal.d.ts +2 -1
- 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 +30 -23
- package/lib/langs/JavaScript.js.map +1 -1
- package/package.json +18 -13
- package/src/{samples → examples}/quick-start.ts +22 -12
- package/src/lib/BuiltInTypeCompiler.ts +171 -171
- package/src/lib/BuiltInTypes.ts +36 -36
- package/src/lib/Common.ts +15 -10
- package/src/lib/Compiler.ts +213 -208
- package/src/lib/Context.ts +3 -3
- package/src/lib/FilterCompiler.ts +84 -84
- package/src/lib/InlineCompiler.ts +35 -14
- package/src/lib/Internal.ts +12 -10
- package/src/lib/Modifiers.ts +2 -2
- package/src/lib/index.ts +5 -5
- package/src/lib/langs/JavaScript.ts +34 -24
- package/src/test/00-all.ts +11 -9
- 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 +67 -0
- package/src/test/abstracts.ts +52 -43
- package/dist/typeguard.amd.d.ts +0 -588
- package/dist/typeguard.amd.d.ts.map +0 -1
- package/dist/typeguard.amd.js +0 -2063
- 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 -2179
- package/dist/typeguard.system.js.map +0 -1
- 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
|
])
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
ITestSuite
|
|
20
|
+
} from './abstracts';
|
|
21
|
+
|
|
22
|
+
const testItems: ITestSuite = {
|
|
23
|
+
|
|
24
|
+
name: 'When optional with any',
|
|
25
|
+
sections: [
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
'name': JSON.stringify({ 'test?': 'any' }),
|
|
29
|
+
'rule': { 'test?': 'any' },
|
|
30
|
+
'items': [
|
|
31
|
+
{
|
|
32
|
+
inputName: 'When test === \'ffff\'',
|
|
33
|
+
inputValue: { 'test': 'ffff' },
|
|
34
|
+
expect: true
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
inputName: 'When test is omitted',
|
|
38
|
+
inputValue: {},
|
|
39
|
+
expect: true
|
|
40
|
+
}
|
|
41
|
+
]
|
|
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
|
+
},
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export default createTestDefinition(testItems);
|
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,70 @@ 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
|
+
export function assertItem(input: unknown, expect: boolean | 'throw'): ITestItem {
|
|
107
116
|
|
|
108
117
|
return {
|
|
109
118
|
inputName: JSON.stringify(input),
|
|
@@ -112,7 +121,7 @@ export function assertItem(input: any, expect: boolean | "throw"): ITestItem {
|
|
|
112
121
|
};
|
|
113
122
|
}
|
|
114
123
|
|
|
115
|
-
export function addRule(rule:
|
|
124
|
+
export function addRule(rule: unknown, items: ITestItem[]): ITestRule {
|
|
116
125
|
|
|
117
126
|
return {
|
|
118
127
|
name: JSON.stringify(rule),
|
|
@@ -127,21 +136,21 @@ export function createTestDefinition(suite: ITestSuite) {
|
|
|
127
136
|
|
|
128
137
|
describe(suite.name, function() {
|
|
129
138
|
|
|
130
|
-
for (
|
|
139
|
+
for (const section of suite.sections) {
|
|
131
140
|
|
|
132
141
|
describe(section.name, function() {
|
|
133
142
|
|
|
134
143
|
if (
|
|
135
144
|
section.items.length === 1 &&
|
|
136
|
-
section.items[0].expect ===
|
|
145
|
+
section.items[0].expect === 'throw'
|
|
137
146
|
) {
|
|
138
147
|
|
|
139
|
-
it(
|
|
148
|
+
it('Throws exception.', function() {
|
|
140
149
|
|
|
141
150
|
assert.throws(() => {
|
|
142
151
|
|
|
143
152
|
compiler.compile<any>({
|
|
144
|
-
|
|
153
|
+
'rule': section.rule
|
|
145
154
|
});
|
|
146
155
|
});
|
|
147
156
|
});
|
|
@@ -149,13 +158,13 @@ export function createTestDefinition(suite: ITestSuite) {
|
|
|
149
158
|
}
|
|
150
159
|
|
|
151
160
|
const check = compiler.compile<any>({
|
|
152
|
-
|
|
161
|
+
'rule': section.rule
|
|
153
162
|
});
|
|
154
163
|
|
|
155
|
-
for (
|
|
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
|
-
item.expect ?
|
|
167
|
+
item.expect ? 'PASSED' : 'REJECTED'
|
|
159
168
|
} when input ${
|
|
160
169
|
item.inputName
|
|
161
170
|
}.`, function() {
|