@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,54 @@
1
+ /* eslint-disable no-empty-function */
2
+ import { t, typeAsserts } from '../../index';
3
+
4
+ describe('function', () => {
5
+ it('function', () => {
6
+ expect.hasAssertions();
7
+ const schema = t.function();
8
+
9
+ expect(schema.validate(() => {})).toBe(true);
10
+ expect(() => typeAsserts(schema, () => {})).not.toThrow();
11
+
12
+ expect(schema.validate(-1)).toBe(false);
13
+ expect(() => typeAsserts(schema, -1)).toThrow(`param should be function, but got -1`);
14
+
15
+ expect(schema.validate(+0)).toBe(false);
16
+ expect(() => typeAsserts(schema, +0)).toThrow(`param should be function, but got 0`);
17
+
18
+ expect(schema.validate(-0)).toBe(false);
19
+ expect(() => typeAsserts(schema, -0)).toThrow(`param should be function, but got 0`);
20
+
21
+ expect(schema.validate(1)).toBe(false);
22
+ expect(() => typeAsserts(schema, 1)).toThrow(`param should be function, but got 1`);
23
+
24
+ expect(schema.validate(NaN)).toBe(false);
25
+ expect(() => typeAsserts(schema, NaN)).toThrow(`param should be function, but got NaN`);
26
+
27
+ expect(schema.validate('str')).toBe(false);
28
+ expect(() => typeAsserts(schema, 'str')).toThrow(`param should be function, but got "str"`);
29
+
30
+ expect(schema.validate(Symbol(1))).toBe(false);
31
+ expect(() => typeAsserts(schema, Symbol(1))).toThrow(`param should be function, but got a(n) symbol`);
32
+
33
+ expect(schema.validate(null)).toBe(false);
34
+ expect(() => typeAsserts(schema, null)).toThrow(`param should be function, but got null`);
35
+
36
+ expect(schema.validate(undefined)).toBe(false);
37
+ expect(() => typeAsserts(schema, undefined)).toThrow(`param should be function, but got undefined`);
38
+
39
+ expect(schema.validate({})).toBe(false);
40
+ expect(() => typeAsserts(schema, {})).toThrow(`param should be function, but got a(n) object`);
41
+
42
+ expect(schema.validate(BigInt(1))).toBe(false);
43
+ expect(() => typeAsserts(schema, BigInt(1))).toThrow(`param should be function, but got a(n) bigint`);
44
+ });
45
+
46
+ it('optional', () => {
47
+ expect.hasAssertions();
48
+ const schema = t.function().optional();
49
+ expect(schema.validate(undefined)).toBe(true);
50
+ expect(() => {
51
+ typeAsserts(schema, undefined);
52
+ }).not.toThrow();
53
+ });
54
+ });
@@ -0,0 +1,130 @@
1
+ import { t, typeAsserts } from '../../index';
2
+
3
+ describe('literal', () => {
4
+ it('valid', () => {
5
+ expect.hasAssertions();
6
+ expect(t.literal(1).validate(1)).toBe(true);
7
+ expect(() => {
8
+ typeAsserts(t.literal(1), 1);
9
+ }).not.toThrow();
10
+
11
+ expect(t.literal('str').validate('str')).toBe(true);
12
+ expect(() => {
13
+ typeAsserts(t.literal('str'), 'str');
14
+ }).not.toThrow();
15
+
16
+ expect(t.literal(true).validate(true)).toBe(true);
17
+ expect(() => {
18
+ typeAsserts(t.literal(true), true);
19
+ }).not.toThrow();
20
+
21
+ expect(t.literal(false).validate(false)).toBe(true);
22
+ expect(() => {
23
+ typeAsserts(t.literal(false), false);
24
+ }).not.toThrow();
25
+
26
+ expect(t.literal(undefined).validate(undefined)).toBe(true);
27
+ expect(() => {
28
+ typeAsserts(t.literal(undefined), undefined);
29
+ }).not.toThrow();
30
+
31
+ expect(t.literal(null).validate(null)).toBe(true);
32
+ expect(() => {
33
+ typeAsserts(t.literal(null), null);
34
+ }).not.toThrow();
35
+ });
36
+
37
+ it('invalid', () => {
38
+ expect.hasAssertions();
39
+ expect(t.literal(1).validate(123)).toBe(false);
40
+ expect(() => {
41
+ typeAsserts(t.literal(1), 123);
42
+ }).toThrow('param should be 1, but got 123');
43
+
44
+ expect(t.literal('str').validate('string')).toBe(false);
45
+ expect(() => {
46
+ typeAsserts(t.literal('str'), 'string');
47
+ }).toThrow('param should be "str", but got "string"');
48
+
49
+ expect(t.literal(true).validate(false)).toBe(false);
50
+ expect(() => {
51
+ typeAsserts(t.literal(true), false);
52
+ }).toThrow('param should be true, but got false');
53
+
54
+ expect(t.literal(false).validate(true)).toBe(false);
55
+ expect(() => {
56
+ typeAsserts(t.literal(false), true);
57
+ }).toThrow('param should be false, but got true');
58
+
59
+ expect(t.literal(undefined).validate(null)).toBe(false);
60
+ expect(() => {
61
+ typeAsserts(t.literal(undefined), null);
62
+ }).toThrow('param should be undefined, but got null');
63
+
64
+ expect(t.literal(null).validate(undefined)).toBe(false);
65
+ expect(() => {
66
+ typeAsserts(t.literal(null), undefined);
67
+ }).toThrow('param should be null, but got undefined');
68
+ });
69
+
70
+ it('optional valid', () => {
71
+ expect.hasAssertions();
72
+ expect(t.literal(1).optional().validate(undefined)).toBe(true);
73
+ expect(() => {
74
+ typeAsserts(t.literal(1).optional(), undefined);
75
+ }).not.toThrow();
76
+
77
+ expect(t.literal(1).optional().validate(undefined)).toBe(true);
78
+ expect(() => {
79
+ typeAsserts(t.literal(1).optional(), undefined);
80
+ }).not.toThrow();
81
+
82
+ expect(t.literal('str').optional().validate(undefined)).toBe(true);
83
+ expect(() => {
84
+ typeAsserts(t.literal('str').optional(), undefined);
85
+ }).not.toThrow();
86
+
87
+ expect(t.literal(true).optional().validate(undefined)).toBe(true);
88
+ expect(() => {
89
+ typeAsserts(t.literal(true).optional(), undefined);
90
+ }).not.toThrow();
91
+
92
+ expect(t.literal(false).optional().validate(undefined)).toBe(true);
93
+ expect(() => {
94
+ typeAsserts(t.literal(false).optional(), undefined);
95
+ }).not.toThrow();
96
+
97
+ expect(t.literal(undefined).optional().validate(undefined)).toBe(true);
98
+ expect(() => {
99
+ typeAsserts(t.literal(undefined).optional(), undefined);
100
+ }).not.toThrow();
101
+ });
102
+
103
+ it('optional invalid', () => {
104
+ expect.hasAssertions();
105
+ expect(t.literal(1).optional().validate(123)).toBe(false);
106
+ expect(() => {
107
+ typeAsserts(t.literal(1).optional(), 123);
108
+ }).toThrow('param should be 1 or undefined, but got 123');
109
+
110
+ expect(t.literal('str').optional().validate(123)).toBe(false);
111
+ expect(() => {
112
+ typeAsserts(t.literal('str').optional(), 123);
113
+ }).toThrow('param should be "str" or undefined, but got 123');
114
+
115
+ expect(t.literal(true).optional().validate(false)).toBe(false);
116
+ expect(() => {
117
+ typeAsserts(t.literal(true).optional(), false);
118
+ }).toThrow('param should be true or undefined, but got false');
119
+
120
+ expect(t.literal(false).optional().validate(true)).toBe(false);
121
+ expect(() => {
122
+ typeAsserts(t.literal(false).optional(), true);
123
+ }).toThrow('param should be false or undefined, but got true');
124
+
125
+ expect(t.literal(undefined).optional().validate(123)).toBe(false);
126
+ expect(() => {
127
+ typeAsserts(t.literal(undefined).optional(), 123);
128
+ }).toThrow('param should be undefined, but got 123');
129
+ });
130
+ });
@@ -0,0 +1,41 @@
1
+ import { t, typeAsserts } from '../../index';
2
+
3
+ describe('nullish', () => {
4
+ it('null', () => {
5
+ expect.hasAssertions();
6
+ const schema = t.null();
7
+
8
+ expect(schema.validate(null)).toBe(true);
9
+ expect(() => {
10
+ typeAsserts(schema, null);
11
+ }).not.toThrow();
12
+ });
13
+
14
+ it('undefined', () => {
15
+ expect.hasAssertions();
16
+ const schema = t.undefined();
17
+
18
+ expect(schema.validate(undefined)).toBe(true);
19
+ expect(() => {
20
+ typeAsserts(schema, undefined);
21
+ }).not.toThrow();
22
+ });
23
+
24
+ it('null optional', () => {
25
+ expect.hasAssertions();
26
+ const schema = t.null().optional();
27
+ expect(schema.validate(undefined)).toBe(true);
28
+ expect(() => {
29
+ typeAsserts(schema, undefined);
30
+ }).not.toThrow();
31
+ });
32
+
33
+ it('undefined optional', () => {
34
+ expect.hasAssertions();
35
+ const schema = t.undefined().optional();
36
+ expect(schema.validate(undefined)).toBe(true);
37
+ expect(() => {
38
+ typeAsserts(schema, undefined);
39
+ }).not.toThrow();
40
+ });
41
+ });
@@ -0,0 +1,147 @@
1
+ /* eslint-disable no-empty-function */
2
+ import { t, typeAsserts } from '../../index';
3
+
4
+ describe('number', () => {
5
+ it('number', () => {
6
+ expect.hasAssertions();
7
+ const schema = t.number();
8
+
9
+ expect(schema.validate(-1)).toBe(true);
10
+ expect(() => typeAsserts(schema, -1)).not.toThrow();
11
+
12
+ expect(schema.validate(+0)).toBe(true);
13
+ expect(() => typeAsserts(schema, +0)).not.toThrow();
14
+
15
+ expect(schema.validate(-0)).toBe(true);
16
+ expect(() => typeAsserts(schema, -0)).not.toThrow();
17
+
18
+ expect(schema.validate(1)).toBe(true);
19
+ expect(() => typeAsserts(schema, 1)).not.toThrow();
20
+
21
+ expect(schema.validate(NaN)).toBe(true);
22
+ expect(() => typeAsserts(schema, NaN)).not.toThrow();
23
+
24
+ expect(schema.validate('str')).toBe(false);
25
+ expect(() => typeAsserts(schema, 'str')).toThrow(`param should be number, but got "str"`);
26
+
27
+ expect(schema.validate(Symbol(1))).toBe(false);
28
+ expect(() => typeAsserts(schema, Symbol(1))).toThrow(`param should be number, but got a(n) symbol`);
29
+
30
+ expect(schema.validate(null)).toBe(false);
31
+ expect(() => typeAsserts(schema, null)).toThrow(`param should be number, but got null`);
32
+
33
+ expect(schema.validate(undefined)).toBe(false);
34
+ expect(() => typeAsserts(schema, undefined)).toThrow(`param should be number, but got undefined`);
35
+
36
+ expect(schema.validate({})).toBe(false);
37
+ expect(() => typeAsserts(schema, {})).toThrow(`param should be number, but got a(n) object`);
38
+
39
+ expect(schema.validate(() => {})).toBe(false);
40
+ expect(() => typeAsserts(schema, () => {})).toThrow(`param should be number, but got a(n) function`);
41
+
42
+ expect(schema.validate(BigInt(1))).toBe(false);
43
+ expect(() => typeAsserts(schema, BigInt(1))).toThrow(`param should be number, but got a(n) bigint`);
44
+ });
45
+
46
+ it('optional number', () => {
47
+ expect.hasAssertions();
48
+ const schema = t.number().optional();
49
+
50
+ expect(schema.validate(-1)).toBe(true);
51
+ expect(() => typeAsserts(schema, -1)).not.toThrow();
52
+
53
+ expect(schema.validate(+0)).toBe(true);
54
+ expect(() => typeAsserts(schema, +0)).not.toThrow();
55
+
56
+ expect(schema.validate(-0)).toBe(true);
57
+ expect(() => typeAsserts(schema, -0)).not.toThrow();
58
+
59
+ expect(schema.validate(1)).toBe(true);
60
+ expect(() => typeAsserts(schema, 1)).not.toThrow();
61
+
62
+ expect(schema.validate(NaN)).toBe(true);
63
+ expect(() => typeAsserts(schema, NaN)).not.toThrow();
64
+
65
+ expect(schema.validate(undefined)).toBe(true);
66
+ expect(() => typeAsserts(schema, undefined)).not.toThrow();
67
+
68
+ expect(schema.validate('str')).toBe(false);
69
+ expect(() => typeAsserts(schema, 'str')).toThrow(`param should be number or undefined, but got "str"`);
70
+
71
+ expect(schema.validate(Symbol(1))).toBe(false);
72
+ expect(() => typeAsserts(schema, Symbol(1))).toThrow(
73
+ `param should be number or undefined, but got a(n) symbol`
74
+ );
75
+
76
+ expect(schema.validate(null)).toBe(false);
77
+ expect(() => typeAsserts(schema, null)).toThrow(`param should be number or undefined, but got null`);
78
+
79
+ expect(schema.validate({})).toBe(false);
80
+ expect(() => typeAsserts(schema, {})).toThrow(`param should be number or undefined, but got a(n) object`);
81
+
82
+ expect(schema.validate(() => {})).toBe(false);
83
+ expect(() => typeAsserts(schema, () => {})).toThrow(
84
+ `param should be number or undefined, but got a(n) function`
85
+ );
86
+
87
+ expect(schema.validate(BigInt(1))).toBe(false);
88
+ expect(() => typeAsserts(schema, BigInt(1))).toThrow(
89
+ `param should be number or undefined, but got a(n) bigint`
90
+ );
91
+ });
92
+
93
+ it('max', () => {
94
+ expect.hasAssertions();
95
+ const schema = t.number().max(100);
96
+
97
+ expect(schema.validate(1)).toBe(true);
98
+ expect(() => {
99
+ typeAsserts(schema, 1);
100
+ }).not.toThrow();
101
+
102
+ expect(schema.validate(100)).toBe(true);
103
+ expect(() => {
104
+ typeAsserts(schema, 100);
105
+ }).not.toThrow();
106
+
107
+ expect(schema.validate(-101)).toBe(true);
108
+ expect(() => {
109
+ typeAsserts(schema, -101);
110
+ }).not.toThrow();
111
+
112
+ expect(schema.validate(-0)).toBe(true);
113
+ expect(() => {
114
+ typeAsserts(schema, -0);
115
+ }).not.toThrow();
116
+
117
+ expect(schema.validate(101)).toBe(false);
118
+ expect(() => {
119
+ typeAsserts(schema, 101);
120
+ }).toThrow('param should be less than or equal to 100');
121
+ });
122
+
123
+ it('min', () => {
124
+ expect.hasAssertions();
125
+ const schema = t.number().min(100);
126
+
127
+ expect(schema.validate(100)).toBe(true);
128
+ expect(() => {
129
+ typeAsserts(schema, 100);
130
+ }).not.toThrow();
131
+
132
+ expect(schema.validate(101)).toBe(true);
133
+ expect(() => {
134
+ typeAsserts(schema, 101);
135
+ }).not.toThrow();
136
+
137
+ expect(schema.validate(99)).toBe(false);
138
+ expect(() => {
139
+ typeAsserts(schema, 99);
140
+ }).toThrow();
141
+
142
+ expect(schema.validate(-99)).toBe(false);
143
+ expect(() => {
144
+ typeAsserts(schema, -99);
145
+ }).toThrow('param should be greater than or equal to 100');
146
+ });
147
+ });
@@ -0,0 +1,131 @@
1
+ import { t, typeAsserts } from '../../index';
2
+
3
+ describe('object', () => {
4
+ it('object first key', () => {
5
+ expect.hasAssertions();
6
+ const schema = t.object({
7
+ name: t.string().optional(),
8
+ age: t.number()
9
+ });
10
+
11
+ expect(
12
+ schema.validate({
13
+ // name: 'yinz', optional
14
+ age: 30
15
+ })
16
+ ).toBe(true);
17
+
18
+ expect(
19
+ schema.validate({
20
+ // name: 'yinz', optional
21
+ age: 30
22
+ })
23
+ ).toBe(true);
24
+
25
+ expect(
26
+ schema.validate({
27
+ name: 1, // error here
28
+ age: 30
29
+ })
30
+ ).toBe(false);
31
+
32
+ expect(() =>
33
+ typeAsserts(schema, {
34
+ name: 1, // error here
35
+ age: 30
36
+ })
37
+ ).toThrow('param.name should be string or undefined, but got 1');
38
+
39
+ expect(() => typeAsserts(schema, [])).toThrow('param should be object, but got a(n) array');
40
+ });
41
+
42
+ it('object not first key', () => {
43
+ expect.hasAssertions();
44
+ const schema = t.object({
45
+ name: t.string().optional(),
46
+ age: t.number()
47
+ });
48
+
49
+ expect(
50
+ schema.validate({
51
+ age: 30
52
+ })
53
+ ).toBe(true);
54
+
55
+ expect(
56
+ schema.validate({
57
+ age: '30' // error here
58
+ })
59
+ ).toBe(false);
60
+
61
+ expect(() => {
62
+ typeAsserts(schema, {
63
+ age: '30' // error here
64
+ });
65
+ }).toThrow('param.age should be number, but got "30"');
66
+ });
67
+
68
+ it('optional object', () => {
69
+ expect.hasAssertions();
70
+ const schema = t.object({ name: t.string(), age: t.number() }).optional();
71
+
72
+ expect(schema.validate(undefined)).toBe(true);
73
+
74
+ expect(() => {
75
+ typeAsserts(schema, undefined);
76
+ }).not.toThrow();
77
+
78
+ expect(schema.validate({ name: 'yinz', age: 30 })).toBe(true);
79
+
80
+ expect(() => {
81
+ typeAsserts(schema, { name: 123, age: 30 });
82
+ }).toThrow('param.name should be string, but got 123');
83
+ });
84
+
85
+ it('default', () => {
86
+ expect.hasAssertions();
87
+ const schema = t.object({
88
+ a: t.boolean().default(true)
89
+ });
90
+ const data = {};
91
+ expect(schema.validate(data)).toBe(true);
92
+ expect(data).toStrictEqual({ a: true });
93
+ });
94
+
95
+ it('fallback', () => {
96
+ expect.hasAssertions();
97
+ const schema = t.object({
98
+ a: t.boolean().fallback(true)
99
+ });
100
+ const data = { a: [] };
101
+ expect(schema.validate(data)).toBe(true);
102
+ expect(data).toStrictEqual({ a: true });
103
+ });
104
+
105
+ it('special key', () => {
106
+ expect.hasAssertions();
107
+ const schema1 = t.object({
108
+ '@@': t.string()
109
+ });
110
+
111
+ expect(() => {
112
+ typeAsserts(schema1, {});
113
+ }).toThrow('param["@@"] should be string, but got undefined');
114
+
115
+ const schema2 = t.object({
116
+ '-a': t.string()
117
+ });
118
+
119
+ expect(() => {
120
+ typeAsserts(schema2, {});
121
+ }).toThrow('param["-a"] should be string, but got undefined');
122
+
123
+ const schema3 = t.object({
124
+ 'a b': t.string()
125
+ });
126
+
127
+ expect(() => {
128
+ typeAsserts(schema3, {});
129
+ }).toThrow('param["a b"] should be string, but got undefined');
130
+ });
131
+ });
@@ -0,0 +1,96 @@
1
+ import { t, typeAsserts } from '../../index';
2
+
3
+ describe('or', () => {
4
+ it('or', () => {
5
+ expect.hasAssertions();
6
+ const schema = t.or(t.string(), t.number(), t.object({ a: t.string() }));
7
+
8
+ expect(schema.validate(1)).toBe(true);
9
+ expect(schema.validate('1')).toBe(true);
10
+ expect(schema.validate({})).toBe(false);
11
+ expect(schema.validate({})).toBe(false);
12
+
13
+ expect(() => {
14
+ typeAsserts(schema, { a: 1 });
15
+ }).toThrow(
16
+ `param should be string, but got a(n) object
17
+ or param should be number, but got a(n) object
18
+ or param.a should be string, but got 1`
19
+ );
20
+ });
21
+
22
+ it('optional', () => {
23
+ expect.hasAssertions();
24
+ const schema = t.or(t.string(), t.null()).optional();
25
+ expect(schema.validate(undefined)).toBe(true);
26
+ expect(() => {
27
+ typeAsserts(schema, undefined);
28
+ }).not.toThrow();
29
+ });
30
+
31
+ it('optional2', () => {
32
+ expect.hasAssertions();
33
+ const schema = t.or(t.string(), t.null());
34
+ expect(schema.validate(null)).toBe(true);
35
+ });
36
+
37
+ it('$or in $object', () => {
38
+ expect.hasAssertions();
39
+ const schema = t.object({
40
+ src: t.or(t.string(), t.array(t.string()))
41
+ });
42
+
43
+ expect(schema.validate({ src: 'str' })).toBe(true);
44
+ expect(schema.validate({ src: ['str'] })).toBe(true);
45
+ expect(schema.validate({ src: 123 })).toBe(false);
46
+ expect(schema.validate({ src: [123] })).toBe(false);
47
+ expect(schema.validate({ a: 'str' })).toBe(false);
48
+
49
+ expect(() => {
50
+ typeAsserts(schema, { a: 'str' });
51
+ }).toThrow(
52
+ `param.src should be string, but got undefined
53
+ or param.src should be array, but got undefined`
54
+ );
55
+
56
+ expect(() => {
57
+ typeAsserts(schema, { src: 1 });
58
+ }).toThrow(
59
+ `param.src should be string, but got 1
60
+ or param.src should be array, but got 1`
61
+ );
62
+
63
+ expect(() => {
64
+ typeAsserts(schema, { src: [1] });
65
+ }).toThrow(
66
+ `param.src should be string, but got a(n) array
67
+ or param.src[0] should be string, but got 1`
68
+ );
69
+ });
70
+
71
+ it('$tuple in $or', () => {
72
+ expect.hasAssertions();
73
+ const schema = [t.or(t.tuple(t.number(), t.number()), t.tuple())];
74
+
75
+ const NUMBER = 1;
76
+ const STRING = 'abc';
77
+ const BOOLEAN = true;
78
+ const OBJECT = {};
79
+ const NULL_TUPLE: undefined[] = [];
80
+ const NUMBER_TUPLE = [1];
81
+ const STRING_TUPLE = ['abc'];
82
+ const NUMBER_STRING_TUPLE = [1, 'abc'];
83
+
84
+ const basisDataType = [NUMBER, STRING, BOOLEAN, OBJECT];
85
+ const tupleDataType = [NULL_TUPLE, NUMBER_TUPLE, STRING_TUPLE, NUMBER_STRING_TUPLE];
86
+
87
+ schema.forEach((s) => {
88
+ basisDataType.forEach((type) => {
89
+ expect(s.validate(type)).toBe(false);
90
+ });
91
+ tupleDataType.forEach((type) => {
92
+ expect(s.validate(type)).toBe(true);
93
+ });
94
+ });
95
+ });
96
+ });