@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.
Files changed (195) hide show
  1. package/.eslintrc.js +13 -0
  2. package/.rush/temp/package-deps_build.json +105 -0
  3. package/.rush/temp/shrinkwrap-deps.json +79 -0
  4. package/README.md +1 -0
  5. package/dist/common/api-factory/index.d.ts +21 -0
  6. package/dist/common/api-factory/validator/__tests__/validate/any.test.d.ts +1 -0
  7. package/dist/common/api-factory/validator/__tests__/validate/array.test.d.ts +1 -0
  8. package/dist/common/api-factory/validator/__tests__/validate/arraybuffer.test.d.ts +1 -0
  9. package/dist/common/api-factory/validator/__tests__/validate/boolean.test.d.ts +1 -0
  10. package/dist/common/api-factory/validator/__tests__/validate/enum.test.d.ts +1 -0
  11. package/dist/common/api-factory/validator/__tests__/validate/function.test.d.ts +1 -0
  12. package/dist/common/api-factory/validator/__tests__/validate/literal.test.d.ts +1 -0
  13. package/dist/common/api-factory/validator/__tests__/validate/nullish.test.d.ts +1 -0
  14. package/dist/common/api-factory/validator/__tests__/validate/number.test.d.ts +1 -0
  15. package/dist/common/api-factory/validator/__tests__/validate/object.test.d.ts +1 -0
  16. package/dist/common/api-factory/validator/__tests__/validate/or.test.d.ts +1 -0
  17. package/dist/common/api-factory/validator/__tests__/validate/record.test.d.ts +1 -0
  18. package/dist/common/api-factory/validator/__tests__/validate/string.test.d.ts +1 -0
  19. package/dist/common/api-factory/validator/__tests__/validate/symbol.test.d.ts +1 -0
  20. package/dist/common/api-factory/validator/__tests__/validate/tuple.test.d.ts +1 -0
  21. package/dist/common/api-factory/validator/__tests__/validate/type-asserts.test.d.ts +1 -0
  22. package/dist/common/api-factory/validator/__tests__/validate/utils.test.d.ts +1 -0
  23. package/dist/common/api-factory/validator/index.d.ts +29 -0
  24. package/dist/common/api-factory/validator/validate.d.ts +119 -0
  25. package/dist/common/can-i-use.d.ts +2 -0
  26. package/dist/common/context/index.d.ts +16 -0
  27. package/dist/common/context/types.d.ts +5 -0
  28. package/dist/common/context/url-parse.d.ts +22 -0
  29. package/dist/common/http/index.d.ts +13 -0
  30. package/dist/common/http/uuid.d.ts +2 -0
  31. package/dist/common/http/xua.d.ts +17 -0
  32. package/dist/common/report/base-tracker.d.ts +13 -0
  33. package/dist/common/report/create-trace.d.ts +7 -0
  34. package/dist/common/report/errors/error-types.d.ts +122 -0
  35. package/dist/common/report/errors/index.d.ts +13 -0
  36. package/dist/common/report/errors/report/index.d.ts +51 -0
  37. package/dist/common/report/errors/report/listeners.d.ts +1 -0
  38. package/dist/common/report/index.d.ts +3 -0
  39. package/dist/common/report/task-track/index.d.ts +25 -0
  40. package/dist/common/report/track.d.ts +3 -0
  41. package/dist/common/report/types.d.ts +75 -0
  42. package/dist/h5/ads/ads-action-detection.d.ts +6 -0
  43. package/dist/h5/ads/anti-cheating.d.ts +61 -0
  44. package/dist/h5/ads/index.d.ts +275 -0
  45. package/dist/h5/api/base.d.ts +13 -0
  46. package/dist/h5/api/get-system-info.d.ts +1 -0
  47. package/dist/h5/api/index.d.ts +4 -0
  48. package/dist/h5/api/lifecycle.d.ts +1 -0
  49. package/dist/h5/api/storage.d.ts +27 -0
  50. package/dist/h5/api/task.d.ts +1 -0
  51. package/dist/h5/bootstrap/index.d.ts +1 -0
  52. package/dist/h5/http/index.d.ts +33 -0
  53. package/dist/h5/http/utils/__tests__/uuid.test.d.ts +1 -0
  54. package/dist/h5/http/utils/__tests__/xua.test.d.ts +1 -0
  55. package/dist/h5/http/utils/index.d.ts +14 -0
  56. package/dist/h5/http/utils/session.d.ts +1 -0
  57. package/dist/h5/report/errors/index.d.ts +4 -0
  58. package/dist/h5/report/event-tracker.d.ts +8 -0
  59. package/dist/h5/report/index.d.ts +10 -0
  60. package/dist/h5/report/task-tracker.d.ts +18 -0
  61. package/dist/index.d.ts +3 -0
  62. package/dist/index.js +13 -0
  63. package/dist/index.native.d.ts +2 -0
  64. package/dist/index.native.js +3 -0
  65. package/dist/native/api/ads.d.ts +1 -0
  66. package/dist/native/api/base.d.ts +13 -0
  67. package/dist/native/api/get-system-info.d.ts +1 -0
  68. package/dist/native/api/index.d.ts +8 -0
  69. package/dist/native/api/keyboard.d.ts +9 -0
  70. package/dist/native/api/lifecycle.d.ts +1 -0
  71. package/dist/native/api/login.d.ts +1 -0
  72. package/dist/native/api/request.d.ts +1 -0
  73. package/dist/native/api/storage.d.ts +25 -0
  74. package/dist/native/api/task.d.ts +1 -0
  75. package/dist/native/bootstrap/bridge.d.ts +4 -0
  76. package/dist/native/bootstrap/index.d.ts +1 -0
  77. package/dist/native/js-bridge/const.d.ts +5 -0
  78. package/dist/native/js-bridge/index.d.ts +2 -0
  79. package/dist/native/js-bridge/invoke.d.ts +21 -0
  80. package/dist/native/js-bridge/js-bridge.d.ts +6 -0
  81. package/dist/native/js-bridge/report.d.ts +63 -0
  82. package/dist/native/js-bridge/subscribe.d.ts +8 -0
  83. package/dist/native/js-bridge/types.d.ts +14 -0
  84. package/dist/native/js-bridge/utils.d.ts +17 -0
  85. package/dist/native/js-core/index.d.ts +3 -0
  86. package/dist/native/js-core/jolibox-js-core.d.ts +45 -0
  87. package/dist/native/js-core/message-port.d.ts +12 -0
  88. package/dist/native/js-core/utils.d.ts +7 -0
  89. package/dist/native/network/create-fetch.d.ts +27 -0
  90. package/dist/native/network/index.d.ts +11 -0
  91. package/dist/native/network/report.d.ts +15 -0
  92. package/dist/native/network/types.d.ts +61 -0
  93. package/dist/native/network/utils.d.ts +9 -0
  94. package/dist/native/report/errors/index.d.ts +4 -0
  95. package/dist/native/report/index.d.ts +10 -0
  96. package/dist/native/report/task-tracker.d.ts +24 -0
  97. package/dist/utils/index.d.ts +0 -0
  98. package/esbuild.config.js +66 -0
  99. package/implement.build.log +9 -0
  100. package/package.json +30 -0
  101. package/src/common/api-factory/index.ts +188 -0
  102. package/src/common/api-factory/validator/__tests__/validate/any.test.ts +68 -0
  103. package/src/common/api-factory/validator/__tests__/validate/array.test.ts +402 -0
  104. package/src/common/api-factory/validator/__tests__/validate/arraybuffer.test.ts +48 -0
  105. package/src/common/api-factory/validator/__tests__/validate/boolean.test.ts +27 -0
  106. package/src/common/api-factory/validator/__tests__/validate/enum.test.ts +106 -0
  107. package/src/common/api-factory/validator/__tests__/validate/function.test.ts +54 -0
  108. package/src/common/api-factory/validator/__tests__/validate/literal.test.ts +130 -0
  109. package/src/common/api-factory/validator/__tests__/validate/nullish.test.ts +41 -0
  110. package/src/common/api-factory/validator/__tests__/validate/number.test.ts +147 -0
  111. package/src/common/api-factory/validator/__tests__/validate/object.test.ts +131 -0
  112. package/src/common/api-factory/validator/__tests__/validate/or.test.ts +96 -0
  113. package/src/common/api-factory/validator/__tests__/validate/record.test.ts +274 -0
  114. package/src/common/api-factory/validator/__tests__/validate/string.test.ts +187 -0
  115. package/src/common/api-factory/validator/__tests__/validate/symbol.test.ts +23 -0
  116. package/src/common/api-factory/validator/__tests__/validate/tuple.test.ts +86 -0
  117. package/src/common/api-factory/validator/__tests__/validate/type-asserts.test.ts +13 -0
  118. package/src/common/api-factory/validator/__tests__/validate/utils.test.ts +44 -0
  119. package/src/common/api-factory/validator/index.ts +107 -0
  120. package/src/common/api-factory/validator/validate.ts +641 -0
  121. package/src/common/can-i-use.ts +19 -0
  122. package/src/common/context/index.ts +85 -0
  123. package/src/common/context/types.ts +5 -0
  124. package/src/common/context/url-parse.ts +63 -0
  125. package/src/common/http/index.ts +29 -0
  126. package/src/common/http/uuid.ts +11 -0
  127. package/src/common/http/xua.ts +79 -0
  128. package/src/common/report/base-tracker.ts +134 -0
  129. package/src/common/report/create-trace.ts +17 -0
  130. package/src/common/report/errors/error-types.ts +206 -0
  131. package/src/common/report/errors/index.ts +20 -0
  132. package/src/common/report/errors/report/index.ts +63 -0
  133. package/src/common/report/errors/report/listeners.ts +80 -0
  134. package/src/common/report/index.ts +3 -0
  135. package/src/common/report/task-track/index.ts +102 -0
  136. package/src/common/report/track.ts +49 -0
  137. package/src/common/report/types.ts +90 -0
  138. package/src/h5/ads/ads-action-detection.ts +31 -0
  139. package/src/h5/ads/anti-cheating.ts +244 -0
  140. package/src/h5/ads/index.ts +658 -0
  141. package/src/h5/api/base.ts +9 -0
  142. package/src/h5/api/get-system-info.ts +59 -0
  143. package/src/h5/api/index.ts +4 -0
  144. package/src/h5/api/lifecycle.ts +95 -0
  145. package/src/h5/api/storage.ts +173 -0
  146. package/src/h5/api/task.ts +190 -0
  147. package/src/h5/bootstrap/index.ts +16 -0
  148. package/src/h5/http/index.ts +189 -0
  149. package/src/h5/http/utils/__tests__/uuid.test.ts +16 -0
  150. package/src/h5/http/utils/__tests__/xua.test.ts +27 -0
  151. package/src/h5/http/utils/index.ts +19 -0
  152. package/src/h5/http/utils/session.ts +10 -0
  153. package/src/h5/report/errors/index.ts +40 -0
  154. package/src/h5/report/event-tracker.ts +40 -0
  155. package/src/h5/report/index.ts +56 -0
  156. package/src/h5/report/task-tracker.ts +42 -0
  157. package/src/index.native.ts +7 -0
  158. package/src/index.ts +9 -0
  159. package/src/native/api/ads.ts +52 -0
  160. package/src/native/api/base.ts +8 -0
  161. package/src/native/api/get-system-info.ts +44 -0
  162. package/src/native/api/index.ts +8 -0
  163. package/src/native/api/keyboard.ts +75 -0
  164. package/src/native/api/lifecycle.ts +76 -0
  165. package/src/native/api/login.ts +73 -0
  166. package/src/native/api/request.ts +154 -0
  167. package/src/native/api/storage.ts +287 -0
  168. package/src/native/api/task.ts +227 -0
  169. package/src/native/bootstrap/bridge.ts +59 -0
  170. package/src/native/bootstrap/index.ts +59 -0
  171. package/src/native/js-bridge/const.ts +11 -0
  172. package/src/native/js-bridge/index.ts +2 -0
  173. package/src/native/js-bridge/invoke.ts +210 -0
  174. package/src/native/js-bridge/js-bridge.ts +23 -0
  175. package/src/native/js-bridge/report.ts +311 -0
  176. package/src/native/js-bridge/subscribe.ts +50 -0
  177. package/src/native/js-bridge/types.ts +26 -0
  178. package/src/native/js-bridge/utils.ts +116 -0
  179. package/src/native/js-core/index.ts +4 -0
  180. package/src/native/js-core/jolibox-js-core.ts +188 -0
  181. package/src/native/js-core/message-port.ts +52 -0
  182. package/src/native/js-core/utils.ts +9 -0
  183. package/src/native/network/create-fetch.ts +237 -0
  184. package/src/native/network/index.ts +15 -0
  185. package/src/native/network/report.ts +58 -0
  186. package/src/native/network/types.ts +77 -0
  187. package/src/native/network/utils.ts +90 -0
  188. package/src/native/report/errors/index.ts +27 -0
  189. package/src/native/report/index.ts +51 -0
  190. package/src/native/report/task-tracker.ts +72 -0
  191. package/src/native/types/global.d.ts +26 -0
  192. package/src/native/types/native-method-map.d.ts +282 -0
  193. package/src/native/types/native-method.d.ts +30 -0
  194. package/src/utils/index.ts +0 -0
  195. 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
+ });