@jolibox/implement 1.1.4-beta.10
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/.eslintrc.js +13 -0
- package/.rush/temp/package-deps_build.json +105 -0
- package/.rush/temp/shrinkwrap-deps.json +79 -0
- package/README.md +1 -0
- package/dist/common/api-factory/index.d.ts +21 -0
- package/dist/common/api-factory/validator/__tests__/validate/any.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/array.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/arraybuffer.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/boolean.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/enum.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/function.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/literal.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/nullish.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/number.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/object.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/or.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/record.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/string.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/symbol.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/tuple.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/type-asserts.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/utils.test.d.ts +1 -0
- package/dist/common/api-factory/validator/index.d.ts +29 -0
- package/dist/common/api-factory/validator/validate.d.ts +119 -0
- package/dist/common/can-i-use.d.ts +2 -0
- package/dist/common/context/index.d.ts +16 -0
- package/dist/common/context/types.d.ts +5 -0
- package/dist/common/context/url-parse.d.ts +22 -0
- package/dist/common/http/index.d.ts +13 -0
- package/dist/common/http/uuid.d.ts +2 -0
- package/dist/common/http/xua.d.ts +17 -0
- package/dist/common/report/base-tracker.d.ts +13 -0
- package/dist/common/report/create-trace.d.ts +7 -0
- package/dist/common/report/errors/error-types.d.ts +122 -0
- package/dist/common/report/errors/index.d.ts +13 -0
- package/dist/common/report/errors/report/index.d.ts +51 -0
- package/dist/common/report/errors/report/listeners.d.ts +1 -0
- package/dist/common/report/index.d.ts +3 -0
- package/dist/common/report/task-track/index.d.ts +25 -0
- package/dist/common/report/track.d.ts +3 -0
- package/dist/common/report/types.d.ts +75 -0
- package/dist/h5/ads/ads-action-detection.d.ts +6 -0
- package/dist/h5/ads/anti-cheating.d.ts +61 -0
- package/dist/h5/ads/index.d.ts +275 -0
- package/dist/h5/api/base.d.ts +13 -0
- package/dist/h5/api/get-system-info.d.ts +1 -0
- package/dist/h5/api/index.d.ts +4 -0
- package/dist/h5/api/lifecycle.d.ts +1 -0
- package/dist/h5/api/storage.d.ts +27 -0
- package/dist/h5/api/task.d.ts +1 -0
- package/dist/h5/bootstrap/index.d.ts +1 -0
- package/dist/h5/http/index.d.ts +33 -0
- package/dist/h5/http/utils/__tests__/uuid.test.d.ts +1 -0
- package/dist/h5/http/utils/__tests__/xua.test.d.ts +1 -0
- package/dist/h5/http/utils/index.d.ts +14 -0
- package/dist/h5/http/utils/session.d.ts +1 -0
- package/dist/h5/report/errors/index.d.ts +4 -0
- package/dist/h5/report/event-tracker.d.ts +8 -0
- package/dist/h5/report/index.d.ts +10 -0
- package/dist/h5/report/task-tracker.d.ts +18 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +13 -0
- package/dist/index.native.d.ts +2 -0
- package/dist/index.native.js +3 -0
- package/dist/native/api/ads.d.ts +1 -0
- package/dist/native/api/base.d.ts +13 -0
- package/dist/native/api/get-system-info.d.ts +1 -0
- package/dist/native/api/index.d.ts +8 -0
- package/dist/native/api/keyboard.d.ts +9 -0
- package/dist/native/api/lifecycle.d.ts +1 -0
- package/dist/native/api/login.d.ts +1 -0
- package/dist/native/api/request.d.ts +1 -0
- package/dist/native/api/storage.d.ts +25 -0
- package/dist/native/api/task.d.ts +1 -0
- package/dist/native/bootstrap/bridge.d.ts +4 -0
- package/dist/native/bootstrap/index.d.ts +1 -0
- package/dist/native/js-bridge/const.d.ts +5 -0
- package/dist/native/js-bridge/index.d.ts +2 -0
- package/dist/native/js-bridge/invoke.d.ts +21 -0
- package/dist/native/js-bridge/js-bridge.d.ts +6 -0
- package/dist/native/js-bridge/report.d.ts +63 -0
- package/dist/native/js-bridge/subscribe.d.ts +8 -0
- package/dist/native/js-bridge/types.d.ts +14 -0
- package/dist/native/js-bridge/utils.d.ts +17 -0
- package/dist/native/js-core/index.d.ts +3 -0
- package/dist/native/js-core/jolibox-js-core.d.ts +45 -0
- package/dist/native/js-core/message-port.d.ts +12 -0
- package/dist/native/js-core/utils.d.ts +7 -0
- package/dist/native/network/create-fetch.d.ts +27 -0
- package/dist/native/network/index.d.ts +11 -0
- package/dist/native/network/report.d.ts +15 -0
- package/dist/native/network/types.d.ts +61 -0
- package/dist/native/network/utils.d.ts +9 -0
- package/dist/native/report/errors/index.d.ts +4 -0
- package/dist/native/report/index.d.ts +10 -0
- package/dist/native/report/task-tracker.d.ts +24 -0
- package/dist/utils/index.d.ts +0 -0
- package/esbuild.config.js +66 -0
- package/implement.build.log +9 -0
- package/package.json +30 -0
- package/src/common/api-factory/index.ts +188 -0
- package/src/common/api-factory/validator/__tests__/validate/any.test.ts +68 -0
- package/src/common/api-factory/validator/__tests__/validate/array.test.ts +402 -0
- package/src/common/api-factory/validator/__tests__/validate/arraybuffer.test.ts +48 -0
- package/src/common/api-factory/validator/__tests__/validate/boolean.test.ts +27 -0
- package/src/common/api-factory/validator/__tests__/validate/enum.test.ts +106 -0
- package/src/common/api-factory/validator/__tests__/validate/function.test.ts +54 -0
- package/src/common/api-factory/validator/__tests__/validate/literal.test.ts +130 -0
- package/src/common/api-factory/validator/__tests__/validate/nullish.test.ts +41 -0
- package/src/common/api-factory/validator/__tests__/validate/number.test.ts +147 -0
- package/src/common/api-factory/validator/__tests__/validate/object.test.ts +131 -0
- package/src/common/api-factory/validator/__tests__/validate/or.test.ts +96 -0
- package/src/common/api-factory/validator/__tests__/validate/record.test.ts +274 -0
- package/src/common/api-factory/validator/__tests__/validate/string.test.ts +187 -0
- package/src/common/api-factory/validator/__tests__/validate/symbol.test.ts +23 -0
- package/src/common/api-factory/validator/__tests__/validate/tuple.test.ts +86 -0
- package/src/common/api-factory/validator/__tests__/validate/type-asserts.test.ts +13 -0
- package/src/common/api-factory/validator/__tests__/validate/utils.test.ts +44 -0
- package/src/common/api-factory/validator/index.ts +107 -0
- package/src/common/api-factory/validator/validate.ts +641 -0
- package/src/common/can-i-use.ts +19 -0
- package/src/common/context/index.ts +85 -0
- package/src/common/context/types.ts +5 -0
- package/src/common/context/url-parse.ts +63 -0
- package/src/common/http/index.ts +29 -0
- package/src/common/http/uuid.ts +11 -0
- package/src/common/http/xua.ts +79 -0
- package/src/common/report/base-tracker.ts +134 -0
- package/src/common/report/create-trace.ts +17 -0
- package/src/common/report/errors/error-types.ts +206 -0
- package/src/common/report/errors/index.ts +20 -0
- package/src/common/report/errors/report/index.ts +63 -0
- package/src/common/report/errors/report/listeners.ts +80 -0
- package/src/common/report/index.ts +3 -0
- package/src/common/report/task-track/index.ts +102 -0
- package/src/common/report/track.ts +49 -0
- package/src/common/report/types.ts +90 -0
- package/src/h5/ads/ads-action-detection.ts +31 -0
- package/src/h5/ads/anti-cheating.ts +244 -0
- package/src/h5/ads/index.ts +658 -0
- package/src/h5/api/base.ts +9 -0
- package/src/h5/api/get-system-info.ts +59 -0
- package/src/h5/api/index.ts +4 -0
- package/src/h5/api/lifecycle.ts +95 -0
- package/src/h5/api/storage.ts +173 -0
- package/src/h5/api/task.ts +190 -0
- package/src/h5/bootstrap/index.ts +16 -0
- package/src/h5/http/index.ts +189 -0
- package/src/h5/http/utils/__tests__/uuid.test.ts +16 -0
- package/src/h5/http/utils/__tests__/xua.test.ts +27 -0
- package/src/h5/http/utils/index.ts +19 -0
- package/src/h5/http/utils/session.ts +10 -0
- package/src/h5/report/errors/index.ts +40 -0
- package/src/h5/report/event-tracker.ts +40 -0
- package/src/h5/report/index.ts +56 -0
- package/src/h5/report/task-tracker.ts +42 -0
- package/src/index.native.ts +7 -0
- package/src/index.ts +9 -0
- package/src/native/api/ads.ts +52 -0
- package/src/native/api/base.ts +8 -0
- package/src/native/api/get-system-info.ts +44 -0
- package/src/native/api/index.ts +8 -0
- package/src/native/api/keyboard.ts +75 -0
- package/src/native/api/lifecycle.ts +76 -0
- package/src/native/api/login.ts +73 -0
- package/src/native/api/request.ts +154 -0
- package/src/native/api/storage.ts +287 -0
- package/src/native/api/task.ts +227 -0
- package/src/native/bootstrap/bridge.ts +59 -0
- package/src/native/bootstrap/index.ts +59 -0
- package/src/native/js-bridge/const.ts +11 -0
- package/src/native/js-bridge/index.ts +2 -0
- package/src/native/js-bridge/invoke.ts +210 -0
- package/src/native/js-bridge/js-bridge.ts +23 -0
- package/src/native/js-bridge/report.ts +311 -0
- package/src/native/js-bridge/subscribe.ts +50 -0
- package/src/native/js-bridge/types.ts +26 -0
- package/src/native/js-bridge/utils.ts +116 -0
- package/src/native/js-core/index.ts +4 -0
- package/src/native/js-core/jolibox-js-core.ts +188 -0
- package/src/native/js-core/message-port.ts +52 -0
- package/src/native/js-core/utils.ts +9 -0
- package/src/native/network/create-fetch.ts +237 -0
- package/src/native/network/index.ts +15 -0
- package/src/native/network/report.ts +58 -0
- package/src/native/network/types.ts +77 -0
- package/src/native/network/utils.ts +90 -0
- package/src/native/report/errors/index.ts +27 -0
- package/src/native/report/index.ts +51 -0
- package/src/native/report/task-tracker.ts +72 -0
- package/src/native/types/global.d.ts +26 -0
- package/src/native/types/native-method-map.d.ts +282 -0
- package/src/native/types/native-method.d.ts +30 -0
- package/src/utils/index.ts +0 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/* eslint-disable no-empty-function */
|
|
2
|
+
import { t, typeAsserts } from '../../index';
|
|
3
|
+
|
|
4
|
+
describe('record', () => {
|
|
5
|
+
it('string record', () => {
|
|
6
|
+
expect.hasAssertions();
|
|
7
|
+
const schema = t.record(t.string());
|
|
8
|
+
expect(schema.validate({ value: 'str' })).toBe(true);
|
|
9
|
+
expect(() => typeAsserts(schema, { value: 'str' })).not.toThrow();
|
|
10
|
+
|
|
11
|
+
expect(schema.validate(123)).toBe(false);
|
|
12
|
+
expect(() => typeAsserts(schema, 123)).toThrow('param should be object, but got 123');
|
|
13
|
+
|
|
14
|
+
expect(schema.validate({ value: 123 })).toBe(false);
|
|
15
|
+
expect(() => typeAsserts(schema, { value: 123 })).toThrow('param.value should be string, but got 123');
|
|
16
|
+
|
|
17
|
+
expect(schema.validate({ value: true })).toBe(false);
|
|
18
|
+
expect(() => typeAsserts(schema, { value: true })).toThrow(`param.value should be string, but got true`);
|
|
19
|
+
|
|
20
|
+
expect(schema.validate({ value: false })).toBe(false);
|
|
21
|
+
expect(() => typeAsserts(schema, { value: false })).toThrow(
|
|
22
|
+
`param.value should be string, but got false`
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(schema.validate({ value: Symbol.for('mocked') })).toBe(false);
|
|
26
|
+
expect(() => typeAsserts(schema, { value: Symbol.for('mocked') })).toThrow(
|
|
27
|
+
`param.value should be string, but got a(n) symbol`
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
expect(schema.validate({ value: BigInt('123') })).toBe(false);
|
|
31
|
+
expect(() => typeAsserts(schema, { value: BigInt('123') })).toThrow(
|
|
32
|
+
`param.value should be string, but got a(n) bigint`
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(schema.validate({ value: null })).toBe(false);
|
|
36
|
+
expect(() => typeAsserts(schema, { value: null })).toThrow(`param.value should be string, but got null`);
|
|
37
|
+
|
|
38
|
+
expect(schema.validate({ value: undefined })).toBe(false);
|
|
39
|
+
expect(() => typeAsserts(schema, { value: undefined })).toThrow(
|
|
40
|
+
`param.value should be string, but got undefined`
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect(schema.validate({ value: () => {} })).toBe(false);
|
|
44
|
+
expect(() => typeAsserts(schema, { value: () => {} })).toThrow(
|
|
45
|
+
`param.value should be string, but got a(n) function`
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(schema.validate({ value: [] })).toBe(false);
|
|
49
|
+
expect(() => typeAsserts(schema, { value: [] })).toThrow(
|
|
50
|
+
`param.value should be string, but got a(n) array`
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(schema.validate({ value: {} })).toBe(false);
|
|
54
|
+
expect(() => typeAsserts(schema, { value: {} })).toThrow(
|
|
55
|
+
`param.value should be string, but got a(n) object`
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('number record', () => {
|
|
60
|
+
expect.hasAssertions();
|
|
61
|
+
const schema = t.record(t.number());
|
|
62
|
+
|
|
63
|
+
// valid
|
|
64
|
+
expect(schema.validate({ value: 123 })).toBe(true);
|
|
65
|
+
expect(() => typeAsserts(schema, { value: 123 })).not.toThrow();
|
|
66
|
+
|
|
67
|
+
// invalid
|
|
68
|
+
|
|
69
|
+
expect(schema.validate({ value: 'str' })).toBe(false);
|
|
70
|
+
expect(() => typeAsserts(schema, { value: 'str' })).toThrow(
|
|
71
|
+
'param.value should be number, but got "str"'
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
expect(schema.validate({ value: true })).toBe(false);
|
|
75
|
+
expect(() => typeAsserts(schema, { value: true })).toThrow('param.value should be number, but got true');
|
|
76
|
+
|
|
77
|
+
expect(schema.validate({ value: false })).toBe(false);
|
|
78
|
+
expect(() => typeAsserts(schema, { value: false })).toThrow(
|
|
79
|
+
`param.value should be number, but got false`
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(schema.validate({ value: Symbol.for('mocked') })).toBe(false);
|
|
83
|
+
expect(() => typeAsserts(schema, { value: Symbol.for('mocked') })).toThrow(
|
|
84
|
+
`param.value should be number, but got a(n) symbol`
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(schema.validate({ value: BigInt('123') })).toBe(false);
|
|
88
|
+
expect(() => typeAsserts(schema, { value: BigInt('123') })).toThrow(
|
|
89
|
+
`param.value should be number, but got a(n) bigint`
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(schema.validate({ value: null })).toBe(false);
|
|
93
|
+
expect(() => typeAsserts(schema, { value: null })).toThrow(`param.value should be number, but got null`);
|
|
94
|
+
|
|
95
|
+
expect(schema.validate({ value: undefined })).toBe(false);
|
|
96
|
+
expect(() => typeAsserts(schema, { value: undefined })).toThrow(
|
|
97
|
+
`param.value should be number, but got undefined`
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(schema.validate({ value: () => {} })).toBe(false);
|
|
101
|
+
expect(() => typeAsserts(schema, { value: () => {} })).toThrow(
|
|
102
|
+
`param.value should be number, but got a(n) function`
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
expect(schema.validate({ value: [] })).toBe(false);
|
|
106
|
+
expect(() => typeAsserts(schema, { value: [] })).toThrow(
|
|
107
|
+
`param.value should be number, but got a(n) array`
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(schema.validate({ value: {} })).toBe(false);
|
|
111
|
+
expect(() => typeAsserts(schema, { value: {} })).toThrow(
|
|
112
|
+
`param.value should be number, but got a(n) object`
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('boolean record', () => {
|
|
117
|
+
expect.hasAssertions();
|
|
118
|
+
const schema = t.record(t.boolean());
|
|
119
|
+
|
|
120
|
+
// valid
|
|
121
|
+
|
|
122
|
+
expect(schema.validate({ value: true })).toBe(true);
|
|
123
|
+
expect(() => typeAsserts(schema, { value: true })).not.toThrow();
|
|
124
|
+
|
|
125
|
+
expect(schema.validate({ value: false })).toBe(true);
|
|
126
|
+
expect(() => typeAsserts(schema, { value: false })).not.toThrow();
|
|
127
|
+
|
|
128
|
+
// invalid
|
|
129
|
+
|
|
130
|
+
expect(schema.validate({ value: 123 })).toBe(false);
|
|
131
|
+
expect(() => typeAsserts(schema, { value: 123 })).toThrow('param.value should be boolean, but got 123');
|
|
132
|
+
|
|
133
|
+
expect(schema.validate({ value: 'str' })).toBe(false);
|
|
134
|
+
expect(() => typeAsserts(schema, { value: 'str' })).toThrow(
|
|
135
|
+
'param.value should be boolean, but got "str"'
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
expect(schema.validate({ value: Symbol.for('mocked') })).toBe(false);
|
|
139
|
+
expect(() => typeAsserts(schema, { value: Symbol.for('mocked') })).toThrow(
|
|
140
|
+
`param.value should be boolean, but got a(n) symbol`
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
expect(schema.validate({ value: BigInt('123') })).toBe(false);
|
|
144
|
+
expect(() => typeAsserts(schema, { value: BigInt('123') })).toThrow(
|
|
145
|
+
`param.value should be boolean, but got a(n) bigint`
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(schema.validate({ value: null })).toBe(false);
|
|
149
|
+
expect(() => typeAsserts(schema, { value: null })).toThrow(`param.value should be boolean, but got null`);
|
|
150
|
+
|
|
151
|
+
expect(schema.validate({ value: undefined })).toBe(false);
|
|
152
|
+
expect(() => typeAsserts(schema, { value: undefined })).toThrow(
|
|
153
|
+
`param.value should be boolean, but got undefined`
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(schema.validate({ value: () => {} })).toBe(false);
|
|
157
|
+
expect(() => typeAsserts(schema, { value: () => {} })).toThrow(
|
|
158
|
+
`param.value should be boolean, but got a(n) function`
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
expect(schema.validate({ value: [] })).toBe(false);
|
|
162
|
+
expect(() => typeAsserts(schema, { value: [] })).toThrow(
|
|
163
|
+
`param.value should be boolean, but got a(n) array`
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
expect(schema.validate({ value: {} })).toBe(false);
|
|
167
|
+
expect(() => typeAsserts(schema, { value: {} })).toThrow(
|
|
168
|
+
`param.value should be boolean, but got a(n) object`
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('symbol record', () => {
|
|
173
|
+
expect.hasAssertions();
|
|
174
|
+
const schema = t.record(t.symbol());
|
|
175
|
+
|
|
176
|
+
expect(schema.validate({ value: Symbol.for('mocked') })).toBe(true);
|
|
177
|
+
expect(() => typeAsserts(schema, { value: Symbol.for('mocked') })).not.toThrow();
|
|
178
|
+
|
|
179
|
+
expect(schema.validate({ value: true })).toBe(false);
|
|
180
|
+
expect(() => typeAsserts(schema, { value: true })).toThrow('param.value should be symbol, but got true');
|
|
181
|
+
|
|
182
|
+
expect(schema.validate({ value: false })).toBe(false);
|
|
183
|
+
expect(() => typeAsserts(schema, { value: false })).toThrow(
|
|
184
|
+
'param.value should be symbol, but got false'
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
expect(schema.validate({ value: 123 })).toBe(false);
|
|
188
|
+
expect(() => typeAsserts(schema, { value: 123 })).toThrow('param.value should be symbol, but got 123');
|
|
189
|
+
|
|
190
|
+
expect(schema.validate({ value: 'str' })).toBe(false);
|
|
191
|
+
expect(() => typeAsserts(schema, { value: 'str' })).toThrow(
|
|
192
|
+
'param.value should be symbol, but got "str"'
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
expect(schema.validate({ value: BigInt('123') })).toBe(false);
|
|
196
|
+
expect(() => typeAsserts(schema, { value: BigInt('123') })).toThrow(
|
|
197
|
+
`param.value should be symbol, but got a(n) bigint`
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
expect(schema.validate({ value: null })).toBe(false);
|
|
201
|
+
expect(() => typeAsserts(schema, { value: null })).toThrow(`param.value should be symbol, but got null`);
|
|
202
|
+
|
|
203
|
+
expect(schema.validate({ value: undefined })).toBe(false);
|
|
204
|
+
expect(() => typeAsserts(schema, { value: undefined })).toThrow(
|
|
205
|
+
`param.value should be symbol, but got undefined`
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
expect(schema.validate({ value: () => {} })).toBe(false);
|
|
209
|
+
expect(() => typeAsserts(schema, { value: () => {} })).toThrow(
|
|
210
|
+
`param.value should be symbol, but got a(n) function`
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
expect(schema.validate({ value: [] })).toBe(false);
|
|
214
|
+
expect(() => typeAsserts(schema, { value: [] })).toThrow(
|
|
215
|
+
`param.value should be symbol, but got a(n) array`
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
expect(schema.validate({ value: {} })).toBe(false);
|
|
219
|
+
expect(() => typeAsserts(schema, { value: {} })).toThrow(
|
|
220
|
+
`param.value should be symbol, but got a(n) object`
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('special key', () => {
|
|
225
|
+
expect.hasAssertions();
|
|
226
|
+
const schema = t.record(t.string());
|
|
227
|
+
|
|
228
|
+
expect(() => {
|
|
229
|
+
typeAsserts(schema, {
|
|
230
|
+
'@@': 1
|
|
231
|
+
});
|
|
232
|
+
}).toThrow('param["@@"] should be string, but got 1');
|
|
233
|
+
|
|
234
|
+
expect(() => {
|
|
235
|
+
typeAsserts(schema, { '-a': 1 });
|
|
236
|
+
}).toThrow('param["-a"] should be string, but got 1');
|
|
237
|
+
|
|
238
|
+
expect(() => {
|
|
239
|
+
typeAsserts(schema, { 'a b': 1 });
|
|
240
|
+
}).toThrow('param["a b"] should be string, but got 1');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('optional', () => {
|
|
244
|
+
expect.hasAssertions();
|
|
245
|
+
const schema = t.record(t.string()).optional();
|
|
246
|
+
expect(schema.validate(undefined)).toBe(true);
|
|
247
|
+
expect(() => {
|
|
248
|
+
typeAsserts(schema, undefined);
|
|
249
|
+
}).not.toThrow();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('item optional', () => {
|
|
253
|
+
expect.hasAssertions();
|
|
254
|
+
const schema = t.record(t.string().optional());
|
|
255
|
+
const data = { a: undefined };
|
|
256
|
+
expect(schema.validate(data)).toBe(true);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('item default', () => {
|
|
260
|
+
expect.hasAssertions();
|
|
261
|
+
const schema = t.record(t.string().default('str'));
|
|
262
|
+
const data = { a: undefined };
|
|
263
|
+
expect(schema.validate(data)).toBe(true);
|
|
264
|
+
expect(data).toStrictEqual({ a: 'str' });
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('item fallback', () => {
|
|
268
|
+
expect.hasAssertions();
|
|
269
|
+
const schema = t.record(t.string().fallback('str'));
|
|
270
|
+
const data = { a: 111 };
|
|
271
|
+
expect(schema.validate(data)).toBe(true);
|
|
272
|
+
expect(data).toStrictEqual({ a: 'str' });
|
|
273
|
+
});
|
|
274
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* eslint-disable no-empty-function */
|
|
2
|
+
import { t, typeAsserts } from '../../index';
|
|
3
|
+
|
|
4
|
+
describe('string', () => {
|
|
5
|
+
it('string', () => {
|
|
6
|
+
expect.hasAssertions();
|
|
7
|
+
const schema = t.string();
|
|
8
|
+
expect(schema.validate('str')).toBe(true);
|
|
9
|
+
expect(() => typeAsserts(schema, 'str')).not.toThrow();
|
|
10
|
+
|
|
11
|
+
expect(schema.validate(1)).toBe(false);
|
|
12
|
+
expect(() => {
|
|
13
|
+
typeAsserts(schema, 1);
|
|
14
|
+
}).toThrow('param should be string, but got 1');
|
|
15
|
+
|
|
16
|
+
expect(schema.validate(Symbol.for('1'))).toBe(false);
|
|
17
|
+
expect(() => {
|
|
18
|
+
typeAsserts(schema, Symbol.for('1'));
|
|
19
|
+
}).toThrow('param should be string, but got a(n) symbol');
|
|
20
|
+
|
|
21
|
+
expect(schema.validate(null)).toBe(false);
|
|
22
|
+
expect(() => {
|
|
23
|
+
typeAsserts(schema, null);
|
|
24
|
+
}).toThrow('param should be string, but got null');
|
|
25
|
+
|
|
26
|
+
expect(schema.validate(undefined)).toBe(false);
|
|
27
|
+
expect(() => {
|
|
28
|
+
typeAsserts(schema, undefined);
|
|
29
|
+
}).toThrow('param should be string, but got undefined');
|
|
30
|
+
|
|
31
|
+
expect(schema.validate({})).toBe(false);
|
|
32
|
+
expect(() => {
|
|
33
|
+
typeAsserts(schema, {});
|
|
34
|
+
}).toThrow('param should be string, but got a(n) object');
|
|
35
|
+
|
|
36
|
+
expect(schema.validate([])).toBe(false);
|
|
37
|
+
expect(() => {
|
|
38
|
+
typeAsserts(schema, []);
|
|
39
|
+
}).toThrow('param should be string, but got a(n) array');
|
|
40
|
+
|
|
41
|
+
expect(schema.validate(() => {})).toBe(false);
|
|
42
|
+
expect(() => {
|
|
43
|
+
typeAsserts(schema, () => {});
|
|
44
|
+
}).toThrow('param should be string, but got a(n) function');
|
|
45
|
+
|
|
46
|
+
expect(schema.validate(BigInt(1))).toBe(false);
|
|
47
|
+
expect(() => {
|
|
48
|
+
typeAsserts(schema, BigInt(1));
|
|
49
|
+
}).toThrow('param should be string, but got a(n) bigint');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('optional string', () => {
|
|
53
|
+
expect.hasAssertions();
|
|
54
|
+
const schema = t.string().optional();
|
|
55
|
+
expect(schema.validate('str')).toBe(true);
|
|
56
|
+
expect(schema.validate(undefined)).toBe(true);
|
|
57
|
+
expect(() => typeAsserts(schema, 'str')).not.toThrow();
|
|
58
|
+
expect(() => typeAsserts(schema, undefined)).not.toThrow();
|
|
59
|
+
|
|
60
|
+
expect(schema.validate(1)).toBe(false);
|
|
61
|
+
expect(() => {
|
|
62
|
+
typeAsserts(schema, 1);
|
|
63
|
+
}).toThrow('param should be string or undefined, but got 1');
|
|
64
|
+
|
|
65
|
+
expect(schema.validate(Symbol(1))).toBe(false);
|
|
66
|
+
expect(() => {
|
|
67
|
+
typeAsserts(schema, Symbol(1));
|
|
68
|
+
}).toThrow('param should be string or undefined, but got a(n) symbol');
|
|
69
|
+
|
|
70
|
+
expect(schema.validate(null)).toBe(false);
|
|
71
|
+
expect(() => {
|
|
72
|
+
typeAsserts(schema, null);
|
|
73
|
+
}).toThrow('param should be string or undefined, but got null');
|
|
74
|
+
|
|
75
|
+
expect(schema.validate({})).toBe(false);
|
|
76
|
+
expect(() => {
|
|
77
|
+
typeAsserts(schema, {});
|
|
78
|
+
}).toThrow('param should be string or undefined, but got a(n) object');
|
|
79
|
+
|
|
80
|
+
expect(schema.validate(() => {})).toBe(false);
|
|
81
|
+
expect(() => {
|
|
82
|
+
typeAsserts(schema, () => {});
|
|
83
|
+
}).toThrow('param should be string or undefined, but got a(n) function');
|
|
84
|
+
|
|
85
|
+
expect(schema.validate(BigInt(1))).toBe(false);
|
|
86
|
+
expect(() => {
|
|
87
|
+
typeAsserts(schema, BigInt(1));
|
|
88
|
+
}).toThrow('param should be string or undefined, but got a(n) bigint');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('string pattern', () => {
|
|
92
|
+
it('string params', () => {
|
|
93
|
+
expect.hasAssertions();
|
|
94
|
+
const abc = t.string().pattern('abc');
|
|
95
|
+
expect(abc.validate('abc')).toBe(true);
|
|
96
|
+
expect(abc.validate('abd')).toBe(false);
|
|
97
|
+
|
|
98
|
+
const wss = t.string().pattern('^wss?://');
|
|
99
|
+
expect(wss.validate('wss://echo.socket.com/abc')).toBe(true);
|
|
100
|
+
expect(wss.validate('ws://echo.socket.com/abc')).toBe(true);
|
|
101
|
+
|
|
102
|
+
expect(wss.validate('wws://echo.socket.com/abc')).toBe(false);
|
|
103
|
+
expect(wss.validate('wss:echo.socket.com/abc')).toBe(false);
|
|
104
|
+
expect(wss.validate('http://echo.socket.com/abc')).toBe(false);
|
|
105
|
+
expect(wss.validate('https://echo.socket.com/abc')).toBe(false);
|
|
106
|
+
|
|
107
|
+
expect(() => {
|
|
108
|
+
typeAsserts(wss, 'xxx');
|
|
109
|
+
}).toThrow(`param should match pattern "^wss?://"`);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('string params2', () => {
|
|
113
|
+
expect.hasAssertions();
|
|
114
|
+
const abc = t.string().pattern(/abc/);
|
|
115
|
+
expect(abc.validate('abc')).toBe(true);
|
|
116
|
+
expect(abc.validate('abd')).toBe(false);
|
|
117
|
+
|
|
118
|
+
const wss = t.string().pattern(/^wss?:\/\//);
|
|
119
|
+
expect(wss.validate('wss://echo.socket.com/abc')).toBe(true);
|
|
120
|
+
expect(wss.validate('ws://echo.socket.com/abc')).toBe(true);
|
|
121
|
+
|
|
122
|
+
expect(wss.validate('wws://echo.socket.com/abc')).toBe(false);
|
|
123
|
+
expect(wss.validate('wss:echo.socket.com/abc')).toBe(false);
|
|
124
|
+
expect(wss.validate('http://echo.socket.com/abc')).toBe(false);
|
|
125
|
+
expect(wss.validate('https://echo.socket.com/abc')).toBe(false);
|
|
126
|
+
|
|
127
|
+
expect(() => {
|
|
128
|
+
typeAsserts(wss, 'xxx');
|
|
129
|
+
}).toThrow(`param should match pattern "/^wss?:\\/\\//"`);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('minLength', () => {
|
|
134
|
+
expect.hasAssertions();
|
|
135
|
+
const schema = t.string().minLength(3);
|
|
136
|
+
|
|
137
|
+
expect(schema.validate('123')).toBe(true);
|
|
138
|
+
expect(() => {
|
|
139
|
+
typeAsserts(schema, '123');
|
|
140
|
+
}).not.toThrow();
|
|
141
|
+
|
|
142
|
+
expect(schema.validate('1234')).toBe(true);
|
|
143
|
+
expect(() => {
|
|
144
|
+
typeAsserts(schema, '1234');
|
|
145
|
+
}).not.toThrow();
|
|
146
|
+
|
|
147
|
+
expect(schema.validate('')).toBe(false);
|
|
148
|
+
expect(() => {
|
|
149
|
+
typeAsserts(schema, '');
|
|
150
|
+
}).toThrow('the length of param should be greater than or equal to 3');
|
|
151
|
+
|
|
152
|
+
expect(schema.validate('12')).toBe(false);
|
|
153
|
+
expect(() => {
|
|
154
|
+
typeAsserts(schema, '12');
|
|
155
|
+
}).toThrow('the length of param should be greater than or equal to 3');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('maxLength', () => {
|
|
159
|
+
expect.hasAssertions();
|
|
160
|
+
const schema = t.string().maxLength(3);
|
|
161
|
+
|
|
162
|
+
expect(schema.validate('')).toBe(true);
|
|
163
|
+
expect(() => {
|
|
164
|
+
typeAsserts(schema, '');
|
|
165
|
+
}).not.toThrow();
|
|
166
|
+
|
|
167
|
+
expect(schema.validate('12')).toBe(true);
|
|
168
|
+
expect(() => {
|
|
169
|
+
typeAsserts(schema, '12');
|
|
170
|
+
}).not.toThrow();
|
|
171
|
+
|
|
172
|
+
expect(schema.validate('123')).toBe(true);
|
|
173
|
+
expect(() => {
|
|
174
|
+
typeAsserts(schema, '123');
|
|
175
|
+
}).not.toThrow();
|
|
176
|
+
|
|
177
|
+
expect(schema.validate('1234')).toBe(false);
|
|
178
|
+
expect(() => {
|
|
179
|
+
typeAsserts(schema, '1234');
|
|
180
|
+
}).toThrow('the length of param should be less than or equal to 3');
|
|
181
|
+
|
|
182
|
+
expect(schema.validate('12345')).toBe(false);
|
|
183
|
+
expect(() => {
|
|
184
|
+
typeAsserts(schema, '12345');
|
|
185
|
+
}).toThrow('the length of param should be less than or equal to 3');
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { t, typeAsserts } from '../../index';
|
|
2
|
+
|
|
3
|
+
describe('symbol', () => {
|
|
4
|
+
it('null', () => {
|
|
5
|
+
expect.hasAssertions();
|
|
6
|
+
const schema = t.symbol();
|
|
7
|
+
|
|
8
|
+
expect(schema.validate(Symbol.for('mocked'))).toBe(true);
|
|
9
|
+
expect(() => {
|
|
10
|
+
typeAsserts(schema, Symbol.for('mocked'));
|
|
11
|
+
}).not.toThrow();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('symbol optional', () => {
|
|
15
|
+
expect.hasAssertions();
|
|
16
|
+
|
|
17
|
+
const schema = t.symbol().optional();
|
|
18
|
+
expect(schema.validate(undefined)).toBe(true);
|
|
19
|
+
expect(() => {
|
|
20
|
+
typeAsserts(schema, undefined);
|
|
21
|
+
}).not.toThrow();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { t, typeAsserts } from '../../index';
|
|
2
|
+
|
|
3
|
+
describe('tuple', () => {
|
|
4
|
+
it('tuple', () => {
|
|
5
|
+
expect.hasAssertions();
|
|
6
|
+
const schema = t.tuple(t.string(), t.number(), t.object());
|
|
7
|
+
|
|
8
|
+
expect(schema.validate([123, 123, 123])).toBe(false);
|
|
9
|
+
expect(() => {
|
|
10
|
+
typeAsserts(schema, [123, 123, 123]);
|
|
11
|
+
}).toThrow('param[0] should be string, but got 123\nparam[2] should be object, but got 123');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('optional tuple', () => {
|
|
15
|
+
expect.hasAssertions();
|
|
16
|
+
const schema = t.tuple(t.string(), t.number(), t.object()).optional();
|
|
17
|
+
expect(schema.validate(undefined)).toBe(true);
|
|
18
|
+
|
|
19
|
+
expect(schema.validate({})).toBe(false);
|
|
20
|
+
expect(() => {
|
|
21
|
+
typeAsserts(schema, {});
|
|
22
|
+
}).toThrow('param should be array or undefined, but got a(n) object');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('tuple fixed length', () => {
|
|
26
|
+
expect.hasAssertions();
|
|
27
|
+
const schema = t.tuple(t.string(), t.number(), t.object());
|
|
28
|
+
|
|
29
|
+
expect(schema.validate(['str', 123, {}])).toBe(true);
|
|
30
|
+
|
|
31
|
+
expect(schema.validate(['str', 123])).toBe(false);
|
|
32
|
+
|
|
33
|
+
expect(() => {
|
|
34
|
+
typeAsserts(schema, ['str', 123]);
|
|
35
|
+
}).toThrow('the length of param should be greater than or equal to 3');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('optional length', () => {
|
|
39
|
+
expect.hasAssertions();
|
|
40
|
+
const schema = t.tuple(t.string(), t.number(), t.object().optional());
|
|
41
|
+
|
|
42
|
+
expect(schema.validate(['str', 123, {}])).toBe(true);
|
|
43
|
+
|
|
44
|
+
expect(schema.validate(['str', 123])).toBe(true);
|
|
45
|
+
|
|
46
|
+
expect(schema.validate(['str'])).toBe(false);
|
|
47
|
+
|
|
48
|
+
expect(() => {
|
|
49
|
+
typeAsserts(schema, ['str']);
|
|
50
|
+
}).toThrow('the length of param should be greater than or equal to 2');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('optional length should ignore optional items before item without optional', () => {
|
|
54
|
+
expect.hasAssertions();
|
|
55
|
+
const schema = t.tuple(t.string().optional(), t.number(), t.object().optional());
|
|
56
|
+
|
|
57
|
+
expect(schema.validate(['str', 123, {}])).toBe(true);
|
|
58
|
+
|
|
59
|
+
expect(schema.validate(['str', 123])).toBe(true);
|
|
60
|
+
|
|
61
|
+
expect(schema.validate(['str'])).toBe(false);
|
|
62
|
+
|
|
63
|
+
expect(schema.validate([])).toBe(false);
|
|
64
|
+
|
|
65
|
+
expect(() => {
|
|
66
|
+
typeAsserts(schema, ['str']);
|
|
67
|
+
}).toThrow('the length of param should be greater than or equal to 2');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('fallback', () => {
|
|
71
|
+
expect.hasAssertions();
|
|
72
|
+
const schema = t.tuple(t.string().fallback('str'));
|
|
73
|
+
const data = [123];
|
|
74
|
+
|
|
75
|
+
expect(schema.validate(data)).toBe(true);
|
|
76
|
+
expect(data).toStrictEqual(['str']);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('default', () => {
|
|
80
|
+
expect.hasAssertions();
|
|
81
|
+
const schema = t.tuple(t.string().default('str'));
|
|
82
|
+
const data: unknown[] = [];
|
|
83
|
+
expect(schema.validate(data)).toBe(true);
|
|
84
|
+
expect(data).toStrictEqual(['str']);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { t, typeAsserts } from '../../index';
|
|
2
|
+
|
|
3
|
+
describe('typeAsserts', () => {
|
|
4
|
+
it('custom Key', () => {
|
|
5
|
+
expect.hasAssertions();
|
|
6
|
+
|
|
7
|
+
const schema = t.number();
|
|
8
|
+
|
|
9
|
+
expect(() => {
|
|
10
|
+
typeAsserts(schema, '123', 'CUSTOM_KEY');
|
|
11
|
+
}).toThrow('CUSTOM_KEY should be number, but got "123"');
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/* eslint-disable no-empty-function */
|
|
2
|
+
import { echo, getType } from '../../validate';
|
|
3
|
+
|
|
4
|
+
describe('validate utils', () => {
|
|
5
|
+
it('getType', () => {
|
|
6
|
+
expect.hasAssertions();
|
|
7
|
+
expect(getType(undefined)).toBe('undefined');
|
|
8
|
+
expect(getType(null)).toBe('null');
|
|
9
|
+
expect(getType(Symbol.for(''))).toBe('symbol');
|
|
10
|
+
|
|
11
|
+
expect(getType(1)).toBe('number');
|
|
12
|
+
expect(getType(BigInt('11111'))).toBe('bigint');
|
|
13
|
+
|
|
14
|
+
expect(getType('str')).toBe('string');
|
|
15
|
+
expect(getType({})).toBe('object');
|
|
16
|
+
expect(getType([])).toBe('array');
|
|
17
|
+
|
|
18
|
+
expect(getType(() => {})).toBe('function');
|
|
19
|
+
expect(getType(async () => {})).toBe('function');
|
|
20
|
+
expect(getType(function* () {})).toBe('function');
|
|
21
|
+
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
23
|
+
expect(getType(class {})).toBe('function');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('echo has a(n)', () => {
|
|
27
|
+
expect.hasAssertions();
|
|
28
|
+
expect(echo({})).toBe(`a(n) object`);
|
|
29
|
+
expect(echo([])).toBe(`a(n) array`);
|
|
30
|
+
expect(echo(BigInt('123'))).toBe('a(n) bigint');
|
|
31
|
+
expect(echo(() => {})).toBe(`a(n) function`);
|
|
32
|
+
expect(echo(Symbol.for('abc'))).toBe(`a(n) symbol`);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('echo no a(n)', () => {
|
|
36
|
+
expect.hasAssertions();
|
|
37
|
+
expect(echo('1')).toBe(`"1"`);
|
|
38
|
+
expect(echo(1)).toBe(`1`);
|
|
39
|
+
expect(echo(null)).toBe(`null`);
|
|
40
|
+
expect(echo(undefined)).toBe(`undefined`);
|
|
41
|
+
expect(echo(true)).toBe('true');
|
|
42
|
+
expect(echo(false)).toBe('false');
|
|
43
|
+
});
|
|
44
|
+
});
|