@sinclair/typebox 0.31.28 → 0.32.0-dev-2

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 (629) hide show
  1. package/{compiler → cjs/compiler}/compiler.js +118 -107
  2. package/{compiler → cjs/compiler}/index.js +3 -3
  3. package/{errors → cjs/errors}/errors.js +48 -44
  4. package/cjs/index.js +205 -0
  5. package/cjs/system/errors.js +192 -0
  6. package/{value → cjs/system}/index.js +13 -14
  7. package/cjs/system/policy.js +74 -0
  8. package/cjs/system/system.js +71 -0
  9. package/cjs/type/any/any.js +36 -0
  10. package/{system → cjs/type/any}/index.js +2 -5
  11. package/cjs/type/array/array.js +42 -0
  12. package/cjs/type/array/index.js +44 -0
  13. package/cjs/type/async-iterator/async-iterator.js +42 -0
  14. package/cjs/type/async-iterator/index.js +44 -0
  15. package/cjs/type/awaited/awaited.js +66 -0
  16. package/cjs/type/awaited/index.js +44 -0
  17. package/cjs/type/bigint/bigint.js +40 -0
  18. package/cjs/type/bigint/index.js +44 -0
  19. package/cjs/type/boolean/boolean.js +40 -0
  20. package/cjs/type/boolean/index.js +44 -0
  21. package/cjs/type/clone/index.js +32 -0
  22. package/cjs/type/clone/type.js +41 -0
  23. package/cjs/type/clone/value.js +58 -0
  24. package/cjs/type/composite/composite.js +47 -0
  25. package/cjs/type/composite/index.js +44 -0
  26. package/cjs/type/const/const.js +84 -0
  27. package/cjs/type/const/index.js +44 -0
  28. package/cjs/type/constructor/constructor.js +43 -0
  29. package/cjs/type/constructor/index.js +44 -0
  30. package/cjs/type/constructor-parameters/constructor-parameters.js +37 -0
  31. package/cjs/type/constructor-parameters/index.js +44 -0
  32. package/cjs/type/date/date.js +40 -0
  33. package/cjs/type/date/index.js +44 -0
  34. package/cjs/type/deref/deref.js +136 -0
  35. package/cjs/type/deref/index.js +44 -0
  36. package/cjs/type/discard/discard.js +38 -0
  37. package/cjs/type/discard/index.js +44 -0
  38. package/cjs/type/enum/enum.js +46 -0
  39. package/cjs/type/enum/index.js +44 -0
  40. package/cjs/type/exclude/exclude.js +54 -0
  41. package/cjs/type/exclude/index.js +44 -0
  42. package/cjs/type/extends/extends-check.js +654 -0
  43. package/cjs/type/extends/extends-undefined.js +51 -0
  44. package/cjs/type/extends/extends.js +47 -0
  45. package/cjs/type/extends/index.js +46 -0
  46. package/cjs/type/extract/extract.js +54 -0
  47. package/cjs/type/extract/index.js +44 -0
  48. package/cjs/type/function/function.js +43 -0
  49. package/cjs/type/function/index.js +44 -0
  50. package/cjs/type/guard/index.js +32 -0
  51. package/cjs/type/guard/type.js +515 -0
  52. package/cjs/type/guard/value.js +100 -0
  53. package/cjs/type/helpers/helpers.js +35 -0
  54. package/cjs/type/helpers/index.js +44 -0
  55. package/cjs/type/index.js +113 -0
  56. package/cjs/type/indexed/index.js +46 -0
  57. package/cjs/type/indexed/indexed-key.js +65 -0
  58. package/cjs/type/indexed/indexed-type.js +106 -0
  59. package/cjs/type/indexed/indexed.js +41 -0
  60. package/cjs/type/instance-type/index.js +44 -0
  61. package/cjs/type/instance-type/instance-type.js +36 -0
  62. package/cjs/type/integer/index.js +44 -0
  63. package/cjs/type/integer/integer.js +40 -0
  64. package/cjs/type/intersect/index.js +44 -0
  65. package/cjs/type/intersect/intersect.js +59 -0
  66. package/cjs/type/intrinsic/index.js +44 -0
  67. package/cjs/type/intrinsic/intrinsic.js +106 -0
  68. package/cjs/type/iterator/index.js +44 -0
  69. package/cjs/type/iterator/iterator.js +42 -0
  70. package/cjs/type/keyof/index.js +46 -0
  71. package/cjs/type/keyof/keyof-string.js +103 -0
  72. package/cjs/type/keyof/keyof-type.js +48 -0
  73. package/cjs/type/keyof/keyof.js +38 -0
  74. package/cjs/type/literal/index.js +44 -0
  75. package/cjs/type/literal/literal.js +41 -0
  76. package/cjs/type/mapped/index.js +44 -0
  77. package/cjs/type/mapped/mapped.js +47 -0
  78. package/cjs/type/modifiers/index.js +44 -0
  79. package/cjs/type/modifiers/modifiers.js +78 -0
  80. package/cjs/type/never/index.js +44 -0
  81. package/cjs/type/never/never.js +40 -0
  82. package/cjs/type/not/index.js +44 -0
  83. package/cjs/type/not/not.js +41 -0
  84. package/cjs/type/null/index.js +44 -0
  85. package/cjs/type/null/null.js +40 -0
  86. package/cjs/type/number/index.js +44 -0
  87. package/cjs/type/number/number.js +40 -0
  88. package/cjs/type/object/index.js +44 -0
  89. package/cjs/type/object/object.js +47 -0
  90. package/cjs/type/omit/index.js +44 -0
  91. package/cjs/type/omit/omit.js +75 -0
  92. package/cjs/type/operators/index.js +44 -0
  93. package/cjs/type/operators/operators.js +119 -0
  94. package/cjs/type/optional/index.js +44 -0
  95. package/cjs/type/optional/optional.js +37 -0
  96. package/cjs/type/parameters/index.js +44 -0
  97. package/cjs/type/parameters/parameters.js +37 -0
  98. package/cjs/type/partial/index.js +44 -0
  99. package/cjs/type/partial/partial.js +73 -0
  100. package/cjs/type/patterns/index.js +44 -0
  101. package/cjs/type/patterns/patterns.js +36 -0
  102. package/cjs/type/pick/index.js +44 -0
  103. package/cjs/type/pick/pick.js +66 -0
  104. package/cjs/type/promise/index.js +44 -0
  105. package/cjs/type/promise/promise.js +42 -0
  106. package/cjs/type/readonly/index.js +44 -0
  107. package/cjs/type/readonly/readonly.js +37 -0
  108. package/cjs/type/readonly-optional/index.js +44 -0
  109. package/cjs/type/readonly-optional/readonly-optional.js +37 -0
  110. package/cjs/type/record/index.js +44 -0
  111. package/cjs/type/record/record.js +97 -0
  112. package/cjs/type/recursive/index.js +44 -0
  113. package/cjs/type/recursive/recursive.js +48 -0
  114. package/cjs/type/ref/index.js +44 -0
  115. package/cjs/type/ref/ref.js +45 -0
  116. package/cjs/type/regexp/index.js +44 -0
  117. package/cjs/type/regexp/regexp.js +38 -0
  118. package/cjs/type/registry/format.js +62 -0
  119. package/cjs/type/registry/index.js +32 -0
  120. package/cjs/type/registry/type.js +62 -0
  121. package/cjs/type/required/index.js +44 -0
  122. package/cjs/type/required/required.js +72 -0
  123. package/cjs/type/rest/index.js +44 -0
  124. package/cjs/type/rest/rest.js +41 -0
  125. package/cjs/type/return-type/index.js +44 -0
  126. package/cjs/type/return-type/return-type.js +36 -0
  127. package/cjs/type/schema/anyschema.js +29 -0
  128. package/cjs/type/schema/index.js +45 -0
  129. package/cjs/type/schema/schema.js +30 -0
  130. package/cjs/type/static/index.js +44 -0
  131. package/cjs/type/static/static.js +29 -0
  132. package/cjs/type/strict/index.js +44 -0
  133. package/cjs/type/strict/strict.js +35 -0
  134. package/cjs/type/string/index.js +44 -0
  135. package/cjs/type/string/string.js +36 -0
  136. package/cjs/type/symbol/index.js +44 -0
  137. package/cjs/type/symbol/symbol.js +36 -0
  138. package/cjs/type/symbols/index.js +44 -0
  139. package/cjs/type/symbols/symbols.js +40 -0
  140. package/cjs/type/template-literal/finite.js +71 -0
  141. package/cjs/type/template-literal/generate.js +74 -0
  142. package/cjs/type/template-literal/index.js +50 -0
  143. package/cjs/type/template-literal/parser.js +175 -0
  144. package/cjs/type/template-literal/pattern.js +61 -0
  145. package/cjs/type/template-literal/syntax.js +87 -0
  146. package/cjs/type/template-literal/template-literal.js +43 -0
  147. package/cjs/type/template-literal/union.js +48 -0
  148. package/cjs/type/transform/index.js +44 -0
  149. package/cjs/type/transform/transform.js +72 -0
  150. package/cjs/type/tuple/index.js +44 -0
  151. package/cjs/type/tuple/tuple.js +42 -0
  152. package/cjs/type/type/index.js +44 -0
  153. package/cjs/type/type/javascript.js +120 -0
  154. package/cjs/type/type/json.js +255 -0
  155. package/cjs/type/type/type.js +153 -0
  156. package/cjs/type/uint8array/index.js +44 -0
  157. package/cjs/type/uint8array/uint8array.js +36 -0
  158. package/cjs/type/undefined/index.js +44 -0
  159. package/cjs/type/undefined/undefined.js +36 -0
  160. package/cjs/type/union/index.js +44 -0
  161. package/cjs/type/union/union.js +50 -0
  162. package/cjs/type/unknown/index.js +44 -0
  163. package/cjs/type/unknown/unknown.js +39 -0
  164. package/cjs/type/unsafe/index.js +44 -0
  165. package/cjs/type/unsafe/unsafe.js +39 -0
  166. package/cjs/type/void/index.js +44 -0
  167. package/cjs/type/void/void.js +40 -0
  168. package/{value → cjs/value/cast}/cast.js +62 -83
  169. package/cjs/value/cast/index.js +44 -0
  170. package/cjs/value/check/check.js +468 -0
  171. package/cjs/value/check/index.js +44 -0
  172. package/cjs/value/clean/clean.js +167 -0
  173. package/cjs/value/clean/index.js +44 -0
  174. package/{value → cjs/value/clone}/clone.js +6 -6
  175. package/cjs/value/clone/index.js +44 -0
  176. package/{value → cjs/value/convert}/convert.js +93 -116
  177. package/cjs/value/convert/index.js +44 -0
  178. package/{value → cjs/value/create}/create.js +69 -62
  179. package/cjs/value/create/index.js +44 -0
  180. package/cjs/value/default/default.js +175 -0
  181. package/cjs/value/default/index.js +44 -0
  182. package/{value → cjs/value/delta}/delta.js +57 -53
  183. package/cjs/value/delta/index.js +44 -0
  184. package/{value → cjs/value/deref}/deref.js +1 -2
  185. package/cjs/value/deref/index.js +44 -0
  186. package/{value → cjs/value/equal}/equal.js +14 -14
  187. package/cjs/value/equal/index.js +44 -0
  188. package/cjs/value/guard/index.js +44 -0
  189. package/{value → cjs/value/hash}/hash.js +26 -26
  190. package/cjs/value/hash/index.js +44 -0
  191. package/cjs/value/index.js +123 -0
  192. package/cjs/value/mutate/index.js +44 -0
  193. package/{value → cjs/value/mutate}/mutate.js +25 -25
  194. package/cjs/value/pointer/index.js +31 -0
  195. package/cjs/value/pointer/pointer.js +151 -0
  196. package/cjs/value/transform/decode.js +211 -0
  197. package/cjs/value/transform/encode.js +219 -0
  198. package/cjs/value/transform/has.js +146 -0
  199. package/cjs/value/transform/index.js +46 -0
  200. package/cjs/value/value/index.js +31 -0
  201. package/cjs/value/value/value.js +125 -0
  202. package/{compiler/compiler.d.ts → esm/compiler/compiler.d.mts} +25 -18
  203. package/esm/compiler/compiler.mjs +658 -0
  204. package/{compiler/index.d.ts → esm/compiler/index.d.mts} +2 -2
  205. package/esm/compiler/index.mjs +29 -0
  206. package/{errors/errors.d.ts → esm/errors/errors.d.mts} +7 -7
  207. package/esm/errors/errors.mjs +583 -0
  208. package/esm/errors/index.d.mts +1 -0
  209. package/esm/errors/index.mjs +28 -0
  210. package/esm/index.d.mts +71 -0
  211. package/esm/index.mjs +105 -0
  212. package/esm/system/errors.d.mts +14 -0
  213. package/esm/system/errors.mjs +188 -0
  214. package/esm/system/index.d.mts +4 -0
  215. package/esm/system/index.mjs +31 -0
  216. package/esm/system/policy.d.mts +21 -0
  217. package/esm/system/policy.mjs +71 -0
  218. package/esm/system/system.d.mts +13 -0
  219. package/esm/system/system.mjs +66 -0
  220. package/esm/type/any/any.d.mts +8 -0
  221. package/esm/type/any/any.mjs +32 -0
  222. package/esm/type/any/index.d.mts +1 -0
  223. package/esm/type/any/index.mjs +28 -0
  224. package/esm/type/array/array.d.mts +25 -0
  225. package/esm/type/array/array.mjs +38 -0
  226. package/esm/type/array/index.d.mts +1 -0
  227. package/esm/type/array/index.mjs +28 -0
  228. package/esm/type/async-iterator/async-iterator.d.mts +11 -0
  229. package/esm/type/async-iterator/async-iterator.mjs +38 -0
  230. package/esm/type/async-iterator/index.d.mts +1 -0
  231. package/esm/type/async-iterator/index.mjs +28 -0
  232. package/esm/type/awaited/awaited.d.mts +12 -0
  233. package/esm/type/awaited/awaited.mjs +61 -0
  234. package/esm/type/awaited/index.d.mts +1 -0
  235. package/esm/type/awaited/index.mjs +28 -0
  236. package/esm/type/bigint/bigint.d.mts +16 -0
  237. package/esm/type/bigint/bigint.mjs +36 -0
  238. package/esm/type/bigint/index.d.mts +1 -0
  239. package/esm/type/bigint/index.mjs +28 -0
  240. package/esm/type/boolean/boolean.d.mts +9 -0
  241. package/esm/type/boolean/boolean.mjs +36 -0
  242. package/esm/type/boolean/index.d.mts +1 -0
  243. package/esm/type/boolean/index.mjs +28 -0
  244. package/esm/type/clone/index.d.mts +2 -0
  245. package/esm/type/clone/index.mjs +29 -0
  246. package/esm/type/clone/type.d.mts +5 -0
  247. package/esm/type/clone/type.mjs +36 -0
  248. package/esm/type/clone/value.d.mts +2 -0
  249. package/esm/type/clone/value.mjs +54 -0
  250. package/esm/type/composite/composite.d.mts +15 -0
  251. package/esm/type/composite/composite.mjs +42 -0
  252. package/esm/type/composite/index.d.mts +1 -0
  253. package/esm/type/composite/index.mjs +28 -0
  254. package/esm/type/const/const.d.mts +30 -0
  255. package/esm/type/const/const.mjs +80 -0
  256. package/esm/type/const/index.d.mts +1 -0
  257. package/esm/type/const/index.mjs +28 -0
  258. package/esm/type/constructor/constructor.d.mts +17 -0
  259. package/esm/type/constructor/constructor.mjs +39 -0
  260. package/esm/type/constructor/index.d.mts +1 -0
  261. package/esm/type/constructor/index.mjs +28 -0
  262. package/esm/type/constructor-parameters/constructor-parameters.d.mts +7 -0
  263. package/esm/type/constructor-parameters/constructor-parameters.mjs +33 -0
  264. package/esm/type/constructor-parameters/index.d.mts +1 -0
  265. package/esm/type/constructor-parameters/index.mjs +28 -0
  266. package/esm/type/date/date.d.mts +21 -0
  267. package/esm/type/date/date.mjs +36 -0
  268. package/esm/type/date/index.d.mts +1 -0
  269. package/esm/type/date/index.mjs +28 -0
  270. package/esm/type/deref/deref.d.mts +24 -0
  271. package/esm/type/deref/deref.mjs +131 -0
  272. package/esm/type/deref/index.d.mts +1 -0
  273. package/esm/type/deref/index.mjs +28 -0
  274. package/esm/type/discard/discard.d.mts +1 -0
  275. package/esm/type/discard/discard.mjs +34 -0
  276. package/esm/type/discard/index.d.mts +1 -0
  277. package/esm/type/discard/index.mjs +28 -0
  278. package/esm/type/enum/enum.d.mts +14 -0
  279. package/esm/type/enum/enum.mjs +42 -0
  280. package/esm/type/enum/index.d.mts +1 -0
  281. package/esm/type/enum/index.mjs +28 -0
  282. package/esm/type/exclude/exclude.d.mts +21 -0
  283. package/esm/type/exclude/exclude.mjs +49 -0
  284. package/esm/type/exclude/index.d.mts +1 -0
  285. package/esm/type/exclude/index.mjs +28 -0
  286. package/esm/type/extends/extends-check.d.mts +9 -0
  287. package/esm/type/extends/extends-check.mjs +649 -0
  288. package/esm/type/extends/extends-undefined.d.mts +3 -0
  289. package/esm/type/extends/extends-undefined.mjs +47 -0
  290. package/esm/type/extends/extends.d.mts +9 -0
  291. package/esm/type/extends/extends.mjs +42 -0
  292. package/esm/type/extends/index.d.mts +3 -0
  293. package/esm/type/extends/index.mjs +30 -0
  294. package/esm/type/extract/extract.d.mts +20 -0
  295. package/esm/type/extract/extract.mjs +49 -0
  296. package/esm/type/extract/index.d.mts +1 -0
  297. package/esm/type/extract/index.mjs +28 -0
  298. package/esm/type/function/function.d.mts +17 -0
  299. package/esm/type/function/function.mjs +39 -0
  300. package/esm/type/function/index.d.mts +1 -0
  301. package/esm/type/function/index.mjs +28 -0
  302. package/esm/type/guard/index.d.mts +2 -0
  303. package/esm/type/guard/index.mjs +29 -0
  304. package/esm/type/guard/type.d.mts +130 -0
  305. package/esm/type/guard/type.mjs +469 -0
  306. package/esm/type/guard/value.d.mts +28 -0
  307. package/esm/type/guard/value.mjs +83 -0
  308. package/esm/type/helpers/helpers.d.mts +42 -0
  309. package/esm/type/helpers/helpers.mjs +31 -0
  310. package/esm/type/helpers/index.d.mts +1 -0
  311. package/esm/type/helpers/index.mjs +28 -0
  312. package/esm/type/index.d.mts +70 -0
  313. package/esm/type/index.mjs +97 -0
  314. package/esm/type/indexed/index.d.mts +3 -0
  315. package/esm/type/indexed/index.mjs +30 -0
  316. package/esm/type/indexed/indexed-key.d.mts +17 -0
  317. package/esm/type/indexed/indexed-key.mjs +61 -0
  318. package/esm/type/indexed/indexed-type.d.mts +33 -0
  319. package/esm/type/indexed/indexed-type.mjs +102 -0
  320. package/esm/type/indexed/indexed.d.mts +8 -0
  321. package/esm/type/indexed/indexed.mjs +37 -0
  322. package/esm/type/instance-type/index.d.mts +1 -0
  323. package/esm/type/instance-type/index.mjs +28 -0
  324. package/esm/type/instance-type/instance-type.d.mts +5 -0
  325. package/esm/type/instance-type/instance-type.mjs +32 -0
  326. package/esm/type/integer/index.d.mts +1 -0
  327. package/esm/type/integer/index.mjs +28 -0
  328. package/esm/type/integer/integer.d.mts +16 -0
  329. package/esm/type/integer/integer.mjs +36 -0
  330. package/esm/type/intersect/index.d.mts +1 -0
  331. package/esm/type/intersect/index.mjs +28 -0
  332. package/esm/type/intersect/intersect.d.mts +22 -0
  333. package/esm/type/intersect/intersect.mjs +54 -0
  334. package/esm/type/intrinsic/index.d.mts +1 -0
  335. package/esm/type/intrinsic/index.mjs +28 -0
  336. package/esm/type/intrinsic/intrinsic.d.mts +27 -0
  337. package/esm/type/intrinsic/intrinsic.mjs +98 -0
  338. package/esm/type/iterator/index.d.mts +1 -0
  339. package/esm/type/iterator/index.mjs +28 -0
  340. package/esm/type/iterator/iterator.d.mts +11 -0
  341. package/esm/type/iterator/iterator.mjs +38 -0
  342. package/esm/type/keyof/index.d.mts +3 -0
  343. package/esm/type/keyof/index.mjs +30 -0
  344. package/esm/type/keyof/keyof-string.d.mts +30 -0
  345. package/esm/type/keyof/keyof-string.mjs +98 -0
  346. package/esm/type/keyof/keyof-type.d.mts +11 -0
  347. package/esm/type/keyof/keyof-type.mjs +44 -0
  348. package/esm/type/keyof/keyof.d.mts +5 -0
  349. package/esm/type/keyof/keyof.mjs +34 -0
  350. package/esm/type/literal/index.d.mts +1 -0
  351. package/esm/type/literal/index.mjs +28 -0
  352. package/esm/type/literal/literal.d.mts +10 -0
  353. package/esm/type/literal/literal.mjs +37 -0
  354. package/esm/type/mapped/index.d.mts +1 -0
  355. package/esm/type/mapped/index.mjs +28 -0
  356. package/esm/type/mapped/mapped.d.mts +26 -0
  357. package/esm/type/mapped/mapped.mjs +43 -0
  358. package/esm/type/modifiers/index.d.mts +1 -0
  359. package/esm/type/modifiers/index.mjs +28 -0
  360. package/esm/type/modifiers/modifiers.d.mts +18 -0
  361. package/esm/type/modifiers/modifiers.mjs +73 -0
  362. package/esm/type/never/index.d.mts +1 -0
  363. package/esm/type/never/index.mjs +28 -0
  364. package/esm/type/never/never.d.mts +9 -0
  365. package/esm/type/never/never.mjs +36 -0
  366. package/esm/type/not/index.d.mts +1 -0
  367. package/esm/type/not/index.mjs +28 -0
  368. package/esm/type/not/not.d.mts +10 -0
  369. package/esm/type/not/not.mjs +37 -0
  370. package/esm/type/null/index.d.mts +1 -0
  371. package/esm/type/null/index.mjs +28 -0
  372. package/esm/type/null/null.d.mts +9 -0
  373. package/esm/type/null/null.mjs +36 -0
  374. package/esm/type/number/index.d.mts +1 -0
  375. package/esm/type/number/index.mjs +28 -0
  376. package/esm/type/number/number.d.mts +16 -0
  377. package/esm/type/number/number.mjs +36 -0
  378. package/esm/type/object/index.d.mts +1 -0
  379. package/esm/type/object/index.mjs +28 -0
  380. package/esm/type/object/object.d.mts +44 -0
  381. package/esm/type/object/object.mjs +43 -0
  382. package/esm/type/omit/index.d.mts +1 -0
  383. package/esm/type/omit/index.mjs +28 -0
  384. package/esm/type/omit/omit.d.mts +21 -0
  385. package/esm/type/omit/omit.mjs +70 -0
  386. package/esm/type/operators/index.d.mts +1 -0
  387. package/esm/type/operators/index.mjs +28 -0
  388. package/esm/type/operators/operators.d.mts +24 -0
  389. package/esm/type/operators/operators.mjs +108 -0
  390. package/esm/type/optional/index.d.mts +1 -0
  391. package/esm/type/optional/index.mjs +28 -0
  392. package/esm/type/optional/optional.d.mts +7 -0
  393. package/esm/type/optional/optional.mjs +33 -0
  394. package/esm/type/parameters/index.d.mts +1 -0
  395. package/esm/type/parameters/index.mjs +28 -0
  396. package/esm/type/parameters/parameters.d.mts +7 -0
  397. package/esm/type/parameters/parameters.mjs +33 -0
  398. package/esm/type/partial/index.d.mts +1 -0
  399. package/esm/type/partial/index.mjs +28 -0
  400. package/esm/type/partial/partial.d.mts +23 -0
  401. package/esm/type/partial/partial.mjs +68 -0
  402. package/esm/type/patterns/index.d.mts +1 -0
  403. package/esm/type/patterns/index.mjs +28 -0
  404. package/esm/type/patterns/patterns.d.mts +6 -0
  405. package/esm/type/patterns/patterns.mjs +33 -0
  406. package/esm/type/pick/index.d.mts +1 -0
  407. package/esm/type/pick/index.mjs +28 -0
  408. package/esm/type/pick/pick.d.mts +21 -0
  409. package/esm/type/pick/pick.mjs +61 -0
  410. package/esm/type/promise/index.d.mts +1 -0
  411. package/esm/type/promise/index.mjs +28 -0
  412. package/esm/type/promise/promise.d.mts +11 -0
  413. package/esm/type/promise/promise.mjs +38 -0
  414. package/esm/type/readonly/index.d.mts +1 -0
  415. package/esm/type/readonly/index.mjs +28 -0
  416. package/esm/type/readonly/readonly.d.mts +7 -0
  417. package/esm/type/readonly/readonly.mjs +33 -0
  418. package/esm/type/readonly-optional/index.d.mts +1 -0
  419. package/esm/type/readonly-optional/index.mjs +28 -0
  420. package/esm/type/readonly-optional/readonly-optional.d.mts +6 -0
  421. package/esm/type/readonly-optional/readonly-optional.mjs +33 -0
  422. package/esm/type/record/index.d.mts +1 -0
  423. package/esm/type/record/index.mjs +28 -0
  424. package/esm/type/record/record.d.mts +55 -0
  425. package/esm/type/record/record.mjs +92 -0
  426. package/esm/type/recursive/index.d.mts +1 -0
  427. package/esm/type/recursive/index.mjs +28 -0
  428. package/esm/type/recursive/recursive.d.mts +15 -0
  429. package/esm/type/recursive/recursive.mjs +44 -0
  430. package/esm/type/ref/index.d.mts +1 -0
  431. package/esm/type/ref/index.mjs +28 -0
  432. package/esm/type/ref/ref.d.mts +12 -0
  433. package/esm/type/ref/ref.mjs +41 -0
  434. package/esm/type/regexp/index.d.mts +1 -0
  435. package/esm/type/regexp/index.mjs +28 -0
  436. package/esm/type/regexp/regexp.d.mts +7 -0
  437. package/esm/type/regexp/regexp.mjs +34 -0
  438. package/esm/type/registry/format.d.mts +13 -0
  439. package/esm/type/registry/format.mjs +53 -0
  440. package/esm/type/registry/index.d.mts +2 -0
  441. package/esm/type/registry/index.mjs +29 -0
  442. package/esm/type/registry/type.d.mts +13 -0
  443. package/esm/type/registry/type.mjs +53 -0
  444. package/esm/type/required/index.d.mts +1 -0
  445. package/esm/type/required/index.mjs +28 -0
  446. package/esm/type/required/required.d.mts +23 -0
  447. package/esm/type/required/required.mjs +67 -0
  448. package/esm/type/rest/index.d.mts +1 -0
  449. package/esm/type/rest/index.mjs +28 -0
  450. package/esm/type/rest/rest.d.mts +10 -0
  451. package/esm/type/rest/rest.mjs +36 -0
  452. package/esm/type/return-type/index.d.mts +1 -0
  453. package/esm/type/return-type/index.mjs +28 -0
  454. package/esm/type/return-type/return-type.d.mts +5 -0
  455. package/esm/type/return-type/return-type.mjs +32 -0
  456. package/esm/type/schema/anyschema.d.mts +32 -0
  457. package/esm/type/schema/anyschema.mjs +28 -0
  458. package/esm/type/schema/index.d.mts +2 -0
  459. package/esm/type/schema/index.mjs +29 -0
  460. package/esm/type/schema/schema.d.mts +29 -0
  461. package/esm/type/schema/schema.mjs +28 -0
  462. package/esm/type/static/index.d.mts +1 -0
  463. package/esm/type/static/index.mjs +28 -0
  464. package/esm/type/static/static.d.mts +34 -0
  465. package/esm/type/static/static.mjs +28 -0
  466. package/esm/type/strict/index.d.mts +1 -0
  467. package/esm/type/strict/index.mjs +28 -0
  468. package/esm/type/strict/strict.d.mts +3 -0
  469. package/esm/type/strict/strict.mjs +31 -0
  470. package/esm/type/string/index.d.mts +1 -0
  471. package/esm/type/string/index.mjs +28 -0
  472. package/esm/type/string/string.d.mts +25 -0
  473. package/esm/type/string/string.mjs +32 -0
  474. package/esm/type/symbol/index.d.mts +1 -0
  475. package/esm/type/symbol/index.mjs +28 -0
  476. package/esm/type/symbol/symbol.d.mts +10 -0
  477. package/esm/type/symbol/symbol.mjs +32 -0
  478. package/esm/type/symbols/index.d.mts +1 -0
  479. package/esm/type/symbols/index.mjs +28 -0
  480. package/esm/type/symbols/symbols.d.mts +10 -0
  481. package/esm/type/symbols/symbols.mjs +37 -0
  482. package/esm/type/template-literal/finite.d.mts +16 -0
  483. package/esm/type/template-literal/finite.mjs +66 -0
  484. package/esm/type/template-literal/generate.d.mts +15 -0
  485. package/esm/type/template-literal/generate.mjs +69 -0
  486. package/esm/type/template-literal/index.d.mts +7 -0
  487. package/esm/type/template-literal/index.mjs +34 -0
  488. package/esm/type/template-literal/parser.d.mts +19 -0
  489. package/esm/type/template-literal/parser.mjs +169 -0
  490. package/esm/type/template-literal/pattern.d.mts +4 -0
  491. package/esm/type/template-literal/pattern.mjs +56 -0
  492. package/esm/type/template-literal/syntax.d.mts +20 -0
  493. package/esm/type/template-literal/syntax.mjs +83 -0
  494. package/esm/type/template-literal/template-literal.d.mts +30 -0
  495. package/esm/type/template-literal/template-literal.mjs +39 -0
  496. package/esm/type/template-literal/union.d.mts +7 -0
  497. package/esm/type/template-literal/union.mjs +44 -0
  498. package/esm/type/transform/index.d.mts +1 -0
  499. package/esm/type/transform/index.mjs +28 -0
  500. package/esm/type/transform/transform.d.mts +29 -0
  501. package/esm/type/transform/transform.mjs +69 -0
  502. package/esm/type/tuple/index.d.mts +1 -0
  503. package/esm/type/tuple/index.mjs +28 -0
  504. package/esm/type/tuple/tuple.d.mts +15 -0
  505. package/esm/type/tuple/tuple.mjs +38 -0
  506. package/esm/type/type/index.d.mts +6 -0
  507. package/esm/type/type/index.mjs +40 -0
  508. package/esm/type/type/javascript.d.mts +58 -0
  509. package/esm/type/type/javascript.mjs +116 -0
  510. package/esm/type/type/json.d.mts +139 -0
  511. package/esm/type/type/json.mjs +251 -0
  512. package/esm/type/type/type.d.mts +58 -0
  513. package/esm/type/type/type.mjs +88 -0
  514. package/esm/type/uint8array/index.d.mts +1 -0
  515. package/esm/type/uint8array/index.mjs +28 -0
  516. package/esm/type/uint8array/uint8array.d.mts +13 -0
  517. package/esm/type/uint8array/uint8array.mjs +32 -0
  518. package/esm/type/undefined/index.d.mts +1 -0
  519. package/esm/type/undefined/index.mjs +28 -0
  520. package/esm/type/undefined/undefined.d.mts +9 -0
  521. package/esm/type/undefined/undefined.mjs +32 -0
  522. package/esm/type/union/index.d.mts +1 -0
  523. package/esm/type/union/index.mjs +28 -0
  524. package/esm/type/union/union.d.mts +16 -0
  525. package/esm/type/union/union.mjs +45 -0
  526. package/esm/type/unknown/index.d.mts +1 -0
  527. package/esm/type/unknown/index.mjs +28 -0
  528. package/esm/type/unknown/unknown.d.mts +8 -0
  529. package/esm/type/unknown/unknown.mjs +35 -0
  530. package/esm/type/unsafe/index.d.mts +1 -0
  531. package/esm/type/unsafe/index.mjs +28 -0
  532. package/esm/type/unsafe/unsafe.d.mts +11 -0
  533. package/esm/type/unsafe/unsafe.mjs +35 -0
  534. package/esm/type/void/index.d.mts +1 -0
  535. package/esm/type/void/index.mjs +28 -0
  536. package/esm/type/void/void.d.mts +9 -0
  537. package/esm/type/void/void.mjs +36 -0
  538. package/esm/value/cast/cast.d.mts +23 -0
  539. package/esm/value/cast/cast.mjs +260 -0
  540. package/esm/value/cast/index.d.mts +1 -0
  541. package/esm/value/cast/index.mjs +28 -0
  542. package/esm/value/check/check.d.mts +10 -0
  543. package/{value/check.js → esm/value/check/check.mjs} +52 -51
  544. package/esm/value/check/index.d.mts +1 -0
  545. package/esm/value/check/index.mjs +28 -0
  546. package/esm/value/clean/clean.d.mts +5 -0
  547. package/esm/value/clean/clean.mjs +163 -0
  548. package/esm/value/clean/index.d.mts +1 -0
  549. package/esm/value/clean/index.mjs +28 -0
  550. package/esm/value/clone/clone.mjs +64 -0
  551. package/esm/value/clone/index.d.mts +1 -0
  552. package/esm/value/clone/index.mjs +28 -0
  553. package/esm/value/convert/convert.d.mts +9 -0
  554. package/esm/value/convert/convert.mjs +287 -0
  555. package/esm/value/convert/index.d.mts +1 -0
  556. package/esm/value/convert/index.mjs +28 -0
  557. package/esm/value/create/create.d.mts +31 -0
  558. package/esm/value/create/create.mjs +504 -0
  559. package/esm/value/create/index.d.mts +1 -0
  560. package/esm/value/create/index.mjs +28 -0
  561. package/esm/value/default/default.d.mts +5 -0
  562. package/esm/value/default/default.mjs +171 -0
  563. package/esm/value/default/index.d.mts +1 -0
  564. package/esm/value/default/index.mjs +28 -0
  565. package/esm/value/delta/delta.d.mts +41 -0
  566. package/esm/value/delta/delta.mjs +194 -0
  567. package/esm/value/delta/index.d.mts +1 -0
  568. package/esm/value/delta/index.mjs +28 -0
  569. package/{value/deref.d.ts → esm/value/deref/deref.d.mts} +4 -2
  570. package/esm/value/deref/deref.mjs +41 -0
  571. package/esm/value/deref/index.d.mts +1 -0
  572. package/esm/value/deref/index.mjs +28 -0
  573. package/esm/value/equal/equal.mjs +73 -0
  574. package/esm/value/equal/index.d.mts +1 -0
  575. package/esm/value/equal/index.mjs +28 -0
  576. package/esm/value/guard/guard.mjs +123 -0
  577. package/esm/value/guard/index.d.mts +1 -0
  578. package/esm/value/guard/index.mjs +28 -0
  579. package/{value/hash.d.ts → esm/value/hash/hash.d.mts} +0 -13
  580. package/esm/value/hash/hash.mjs +173 -0
  581. package/esm/value/hash/index.d.mts +1 -0
  582. package/esm/value/hash/index.mjs +28 -0
  583. package/esm/value/index.d.mts +18 -0
  584. package/esm/value/index.mjs +57 -0
  585. package/esm/value/mutate/index.d.mts +1 -0
  586. package/esm/value/mutate/index.mjs +28 -0
  587. package/{value/mutate.d.ts → esm/value/mutate/mutate.d.mts} +1 -1
  588. package/esm/value/mutate/mutate.mjs +123 -0
  589. package/esm/value/pointer/index.d.mts +1 -0
  590. package/esm/value/pointer/index.mjs +28 -0
  591. package/esm/value/pointer/pointer.d.mts +21 -0
  592. package/esm/value/pointer/pointer.mjs +146 -0
  593. package/esm/value/transform/decode.d.mts +19 -0
  594. package/esm/value/transform/decode.mjs +210 -0
  595. package/esm/value/transform/encode.d.mts +20 -0
  596. package/esm/value/transform/encode.mjs +218 -0
  597. package/esm/value/transform/has.d.mts +3 -0
  598. package/esm/value/transform/has.mjs +142 -0
  599. package/esm/value/transform/index.d.mts +3 -0
  600. package/esm/value/transform/index.mjs +30 -0
  601. package/esm/value/value/index.d.mts +1 -0
  602. package/esm/value/value/index.mjs +28 -0
  603. package/esm/value/value/value.d.mts +53 -0
  604. package/esm/value/value/value.mjs +107 -0
  605. package/package.json +37 -27
  606. package/readme.md +183 -155
  607. package/errors/index.d.ts +0 -1
  608. package/system/index.d.ts +0 -2
  609. package/system/system.d.ts +0 -48
  610. package/system/system.js +0 -274
  611. package/typebox.d.ts +0 -962
  612. package/typebox.js +0 -2364
  613. package/value/cast.d.ts +0 -24
  614. package/value/check.d.ts +0 -9
  615. package/value/convert.d.ts +0 -10
  616. package/value/create.d.ts +0 -30
  617. package/value/delta.d.ts +0 -41
  618. package/value/index.d.ts +0 -5
  619. package/value/pointer.d.ts +0 -24
  620. package/value/pointer.js +0 -148
  621. package/value/transform.d.ts +0 -37
  622. package/value/transform.js +0 -473
  623. package/value/value.d.ts +0 -47
  624. package/value/value.js +0 -117
  625. /package/{errors → cjs/errors}/index.js +0 -0
  626. /package/{value → cjs/value/guard}/guard.js +0 -0
  627. /package/{value/clone.d.ts → esm/value/clone/clone.d.mts} +0 -0
  628. /package/{value/equal.d.ts → esm/value/equal/equal.d.mts} +0 -0
  629. /package/{value/guard.d.ts → esm/value/guard/guard.d.mts} +0 -0
package/typebox.js DELETED
@@ -1,2364 +0,0 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in
18
- all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- THE SOFTWARE.
27
-
28
- ---------------------------------------------------------------------------*/
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Type = exports.JsonType = exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.TypeBuilder = exports.TypeBuilderError = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.TemplateLiteralDslParser = exports.TemplateLiteralGenerator = exports.TemplateLiteralGeneratorError = exports.TemplateLiteralFinite = exports.TemplateLiteralFiniteError = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.TemplateLiteralPatternError = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyArrayResolverError = exports.KeyResolver = exports.ObjectMap = exports.Intrinsic = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.TypeExtendsError = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.ValueGuard = exports.FormatRegistry = exports.TypeBoxError = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Optional = exports.Readonly = exports.Transform = void 0;
31
- // --------------------------------------------------------------------------
32
- // Symbols
33
- // --------------------------------------------------------------------------
34
- exports.Transform = Symbol.for('TypeBox.Transform');
35
- exports.Readonly = Symbol.for('TypeBox.Readonly');
36
- exports.Optional = Symbol.for('TypeBox.Optional');
37
- exports.Hint = Symbol.for('TypeBox.Hint');
38
- exports.Kind = Symbol.for('TypeBox.Kind');
39
- // --------------------------------------------------------------------------
40
- // Patterns
41
- // --------------------------------------------------------------------------
42
- exports.PatternBoolean = '(true|false)';
43
- exports.PatternNumber = '(0|[1-9][0-9]*)';
44
- exports.PatternString = '(.*)';
45
- exports.PatternBooleanExact = `^${exports.PatternBoolean}$`;
46
- exports.PatternNumberExact = `^${exports.PatternNumber}$`;
47
- exports.PatternStringExact = `^${exports.PatternString}$`;
48
- /** A registry for user defined types */
49
- var TypeRegistry;
50
- (function (TypeRegistry) {
51
- const map = new Map();
52
- /** Returns the entries in this registry */
53
- function Entries() {
54
- return new Map(map);
55
- }
56
- TypeRegistry.Entries = Entries;
57
- /** Clears all user defined types */
58
- function Clear() {
59
- return map.clear();
60
- }
61
- TypeRegistry.Clear = Clear;
62
- /** Deletes a registered type */
63
- function Delete(kind) {
64
- return map.delete(kind);
65
- }
66
- TypeRegistry.Delete = Delete;
67
- /** Returns true if this registry contains this kind */
68
- function Has(kind) {
69
- return map.has(kind);
70
- }
71
- TypeRegistry.Has = Has;
72
- /** Sets a validation function for a user defined type */
73
- function Set(kind, func) {
74
- map.set(kind, func);
75
- }
76
- TypeRegistry.Set = Set;
77
- /** Gets a custom validation function for a user defined type */
78
- function Get(kind) {
79
- return map.get(kind);
80
- }
81
- TypeRegistry.Get = Get;
82
- })(TypeRegistry || (exports.TypeRegistry = TypeRegistry = {}));
83
- // --------------------------------------------------------------------------
84
- // TypeBoxError
85
- // --------------------------------------------------------------------------
86
- class TypeBoxError extends Error {
87
- constructor(message) {
88
- super(message);
89
- }
90
- }
91
- exports.TypeBoxError = TypeBoxError;
92
- /** A registry for user defined string formats */
93
- var FormatRegistry;
94
- (function (FormatRegistry) {
95
- const map = new Map();
96
- /** Returns the entries in this registry */
97
- function Entries() {
98
- return new Map(map);
99
- }
100
- FormatRegistry.Entries = Entries;
101
- /** Clears all user defined string formats */
102
- function Clear() {
103
- return map.clear();
104
- }
105
- FormatRegistry.Clear = Clear;
106
- /** Deletes a registered format */
107
- function Delete(format) {
108
- return map.delete(format);
109
- }
110
- FormatRegistry.Delete = Delete;
111
- /** Returns true if the user defined string format exists */
112
- function Has(format) {
113
- return map.has(format);
114
- }
115
- FormatRegistry.Has = Has;
116
- /** Sets a validation function for a user defined string format */
117
- function Set(format, func) {
118
- map.set(format, func);
119
- }
120
- FormatRegistry.Set = Set;
121
- /** Gets a validation function for a user defined string format */
122
- function Get(format) {
123
- return map.get(format);
124
- }
125
- FormatRegistry.Get = Get;
126
- })(FormatRegistry || (exports.FormatRegistry = FormatRegistry = {}));
127
- // --------------------------------------------------------------------------
128
- // ValueGuard
129
- // --------------------------------------------------------------------------
130
- /** Provides functions to type guard raw JavaScript values */
131
- var ValueGuard;
132
- (function (ValueGuard) {
133
- /** Returns true if this value is an array */
134
- function IsArray(value) {
135
- return Array.isArray(value);
136
- }
137
- ValueGuard.IsArray = IsArray;
138
- /** Returns true if this value is bigint */
139
- function IsBigInt(value) {
140
- return typeof value === 'bigint';
141
- }
142
- ValueGuard.IsBigInt = IsBigInt;
143
- /** Returns true if this value is a boolean */
144
- function IsBoolean(value) {
145
- return typeof value === 'boolean';
146
- }
147
- ValueGuard.IsBoolean = IsBoolean;
148
- /** Returns true if this value is a Date object */
149
- function IsDate(value) {
150
- return value instanceof globalThis.Date;
151
- }
152
- ValueGuard.IsDate = IsDate;
153
- /** Returns true if this value is null */
154
- function IsNull(value) {
155
- return value === null;
156
- }
157
- ValueGuard.IsNull = IsNull;
158
- /** Returns true if this value is number */
159
- function IsNumber(value) {
160
- return typeof value === 'number';
161
- }
162
- ValueGuard.IsNumber = IsNumber;
163
- /** Returns true if this value is an object */
164
- function IsObject(value) {
165
- return typeof value === 'object' && value !== null;
166
- }
167
- ValueGuard.IsObject = IsObject;
168
- /** Returns true if this value is string */
169
- function IsString(value) {
170
- return typeof value === 'string';
171
- }
172
- ValueGuard.IsString = IsString;
173
- /** Returns true if this value is a Uint8Array */
174
- function IsUint8Array(value) {
175
- return value instanceof globalThis.Uint8Array;
176
- }
177
- ValueGuard.IsUint8Array = IsUint8Array;
178
- /** Returns true if this value is undefined */
179
- function IsUndefined(value) {
180
- return value === undefined;
181
- }
182
- ValueGuard.IsUndefined = IsUndefined;
183
- })(ValueGuard || (exports.ValueGuard = ValueGuard = {}));
184
- // --------------------------------------------------------------------------
185
- // TypeGuard
186
- // --------------------------------------------------------------------------
187
- class TypeGuardUnknownTypeError extends TypeBoxError {
188
- }
189
- exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
190
- /** Provides functions to test if JavaScript values are TypeBox types */
191
- var TypeGuard;
192
- (function (TypeGuard) {
193
- function IsPattern(value) {
194
- try {
195
- new RegExp(value);
196
- return true;
197
- }
198
- catch {
199
- return false;
200
- }
201
- }
202
- function IsControlCharacterFree(value) {
203
- if (!ValueGuard.IsString(value))
204
- return false;
205
- for (let i = 0; i < value.length; i++) {
206
- const code = value.charCodeAt(i);
207
- if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
208
- return false;
209
- }
210
- }
211
- return true;
212
- }
213
- function IsAdditionalProperties(value) {
214
- return IsOptionalBoolean(value) || TSchema(value);
215
- }
216
- function IsOptionalBigInt(value) {
217
- return ValueGuard.IsUndefined(value) || ValueGuard.IsBigInt(value);
218
- }
219
- function IsOptionalNumber(value) {
220
- return ValueGuard.IsUndefined(value) || ValueGuard.IsNumber(value);
221
- }
222
- function IsOptionalBoolean(value) {
223
- return ValueGuard.IsUndefined(value) || ValueGuard.IsBoolean(value);
224
- }
225
- function IsOptionalString(value) {
226
- return ValueGuard.IsUndefined(value) || ValueGuard.IsString(value);
227
- }
228
- function IsOptionalPattern(value) {
229
- return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value) && IsPattern(value));
230
- }
231
- function IsOptionalFormat(value) {
232
- return ValueGuard.IsUndefined(value) || (ValueGuard.IsString(value) && IsControlCharacterFree(value));
233
- }
234
- function IsOptionalSchema(value) {
235
- return ValueGuard.IsUndefined(value) || TSchema(value);
236
- }
237
- // ----------------------------------------------------------------
238
- // Types
239
- // ----------------------------------------------------------------
240
- /** Returns true if the given value is TAny */
241
- function TAny(schema) {
242
- // prettier-ignore
243
- return (TKindOf(schema, 'Any') &&
244
- IsOptionalString(schema.$id));
245
- }
246
- TypeGuard.TAny = TAny;
247
- /** Returns true if the given value is TArray */
248
- function TArray(schema) {
249
- return (TKindOf(schema, 'Array') &&
250
- schema.type === 'array' &&
251
- IsOptionalString(schema.$id) &&
252
- TSchema(schema.items) &&
253
- IsOptionalNumber(schema.minItems) &&
254
- IsOptionalNumber(schema.maxItems) &&
255
- IsOptionalBoolean(schema.uniqueItems) &&
256
- IsOptionalSchema(schema.contains) &&
257
- IsOptionalNumber(schema.minContains) &&
258
- IsOptionalNumber(schema.maxContains));
259
- }
260
- TypeGuard.TArray = TArray;
261
- /** Returns true if the given value is TAsyncIterator */
262
- function TAsyncIterator(schema) {
263
- // prettier-ignore
264
- return (TKindOf(schema, 'AsyncIterator') &&
265
- schema.type === 'AsyncIterator' &&
266
- IsOptionalString(schema.$id) &&
267
- TSchema(schema.items));
268
- }
269
- TypeGuard.TAsyncIterator = TAsyncIterator;
270
- /** Returns true if the given value is TBigInt */
271
- function TBigInt(schema) {
272
- // prettier-ignore
273
- return (TKindOf(schema, 'BigInt') &&
274
- schema.type === 'bigint' &&
275
- IsOptionalString(schema.$id) &&
276
- IsOptionalBigInt(schema.exclusiveMaximum) &&
277
- IsOptionalBigInt(schema.exclusiveMinimum) &&
278
- IsOptionalBigInt(schema.maximum) &&
279
- IsOptionalBigInt(schema.minimum) &&
280
- IsOptionalBigInt(schema.multipleOf));
281
- }
282
- TypeGuard.TBigInt = TBigInt;
283
- /** Returns true if the given value is TBoolean */
284
- function TBoolean(schema) {
285
- // prettier-ignore
286
- return (TKindOf(schema, 'Boolean') &&
287
- schema.type === 'boolean' &&
288
- IsOptionalString(schema.$id));
289
- }
290
- TypeGuard.TBoolean = TBoolean;
291
- /** Returns true if the given value is TConstructor */
292
- function TConstructor(schema) {
293
- // prettier-ignore
294
- return (TKindOf(schema, 'Constructor') &&
295
- schema.type === 'Constructor' &&
296
- IsOptionalString(schema.$id) &&
297
- ValueGuard.IsArray(schema.parameters) &&
298
- schema.parameters.every(schema => TSchema(schema)) &&
299
- TSchema(schema.returns));
300
- }
301
- TypeGuard.TConstructor = TConstructor;
302
- /** Returns true if the given value is TDate */
303
- function TDate(schema) {
304
- return (TKindOf(schema, 'Date') &&
305
- schema.type === 'Date' &&
306
- IsOptionalString(schema.$id) &&
307
- IsOptionalNumber(schema.exclusiveMaximumTimestamp) &&
308
- IsOptionalNumber(schema.exclusiveMinimumTimestamp) &&
309
- IsOptionalNumber(schema.maximumTimestamp) &&
310
- IsOptionalNumber(schema.minimumTimestamp) &&
311
- IsOptionalNumber(schema.multipleOfTimestamp));
312
- }
313
- TypeGuard.TDate = TDate;
314
- /** Returns true if the given value is TFunction */
315
- function TFunction(schema) {
316
- // prettier-ignore
317
- return (TKindOf(schema, 'Function') &&
318
- schema.type === 'Function' &&
319
- IsOptionalString(schema.$id) &&
320
- ValueGuard.IsArray(schema.parameters) &&
321
- schema.parameters.every(schema => TSchema(schema)) &&
322
- TSchema(schema.returns));
323
- }
324
- TypeGuard.TFunction = TFunction;
325
- /** Returns true if the given value is TInteger */
326
- function TInteger(schema) {
327
- return (TKindOf(schema, 'Integer') &&
328
- schema.type === 'integer' &&
329
- IsOptionalString(schema.$id) &&
330
- IsOptionalNumber(schema.exclusiveMaximum) &&
331
- IsOptionalNumber(schema.exclusiveMinimum) &&
332
- IsOptionalNumber(schema.maximum) &&
333
- IsOptionalNumber(schema.minimum) &&
334
- IsOptionalNumber(schema.multipleOf));
335
- }
336
- TypeGuard.TInteger = TInteger;
337
- /** Returns true if the given value is TIntersect */
338
- function TIntersect(schema) {
339
- // prettier-ignore
340
- return (TKindOf(schema, 'Intersect') &&
341
- (ValueGuard.IsString(schema.type) && schema.type !== 'object' ? false : true) &&
342
- ValueGuard.IsArray(schema.allOf) &&
343
- schema.allOf.every(schema => TSchema(schema) && !TTransform(schema)) &&
344
- IsOptionalString(schema.type) &&
345
- (IsOptionalBoolean(schema.unevaluatedProperties) || IsOptionalSchema(schema.unevaluatedProperties)) &&
346
- IsOptionalString(schema.$id));
347
- }
348
- TypeGuard.TIntersect = TIntersect;
349
- /** Returns true if the given value is TIterator */
350
- function TIterator(schema) {
351
- // prettier-ignore
352
- return (TKindOf(schema, 'Iterator') &&
353
- schema.type === 'Iterator' &&
354
- IsOptionalString(schema.$id) &&
355
- TSchema(schema.items));
356
- }
357
- TypeGuard.TIterator = TIterator;
358
- /** Returns true if the given value is a TKind with the given name. */
359
- function TKindOf(schema, kind) {
360
- return TKind(schema) && schema[exports.Kind] === kind;
361
- }
362
- TypeGuard.TKindOf = TKindOf;
363
- /** Returns true if the given value is TKind */
364
- function TKind(schema) {
365
- return ValueGuard.IsObject(schema) && exports.Kind in schema && ValueGuard.IsString(schema[exports.Kind]);
366
- }
367
- TypeGuard.TKind = TKind;
368
- /** Returns true if the given value is TLiteral<string> */
369
- function TLiteralString(schema) {
370
- return TLiteral(schema) && ValueGuard.IsString(schema.const);
371
- }
372
- TypeGuard.TLiteralString = TLiteralString;
373
- /** Returns true if the given value is TLiteral<number> */
374
- function TLiteralNumber(schema) {
375
- return TLiteral(schema) && ValueGuard.IsNumber(schema.const);
376
- }
377
- TypeGuard.TLiteralNumber = TLiteralNumber;
378
- /** Returns true if the given value is TLiteral<boolean> */
379
- function TLiteralBoolean(schema) {
380
- return TLiteral(schema) && ValueGuard.IsBoolean(schema.const);
381
- }
382
- TypeGuard.TLiteralBoolean = TLiteralBoolean;
383
- /** Returns true if the given value is TLiteral */
384
- function TLiteral(schema) {
385
- // prettier-ignore
386
- return (TKindOf(schema, 'Literal') &&
387
- IsOptionalString(schema.$id) && (ValueGuard.IsBoolean(schema.const) ||
388
- ValueGuard.IsNumber(schema.const) ||
389
- ValueGuard.IsString(schema.const)));
390
- }
391
- TypeGuard.TLiteral = TLiteral;
392
- /** Returns true if the given value is TNever */
393
- function TNever(schema) {
394
- // prettier-ignore
395
- return (TKindOf(schema, 'Never') &&
396
- ValueGuard.IsObject(schema.not) &&
397
- Object.getOwnPropertyNames(schema.not).length === 0);
398
- }
399
- TypeGuard.TNever = TNever;
400
- /** Returns true if the given value is TNot */
401
- function TNot(schema) {
402
- // prettier-ignore
403
- return (TKindOf(schema, 'Not') &&
404
- TSchema(schema.not));
405
- }
406
- TypeGuard.TNot = TNot;
407
- /** Returns true if the given value is TNull */
408
- function TNull(schema) {
409
- // prettier-ignore
410
- return (TKindOf(schema, 'Null') &&
411
- schema.type === 'null' &&
412
- IsOptionalString(schema.$id));
413
- }
414
- TypeGuard.TNull = TNull;
415
- /** Returns true if the given value is TNumber */
416
- function TNumber(schema) {
417
- return (TKindOf(schema, 'Number') &&
418
- schema.type === 'number' &&
419
- IsOptionalString(schema.$id) &&
420
- IsOptionalNumber(schema.exclusiveMaximum) &&
421
- IsOptionalNumber(schema.exclusiveMinimum) &&
422
- IsOptionalNumber(schema.maximum) &&
423
- IsOptionalNumber(schema.minimum) &&
424
- IsOptionalNumber(schema.multipleOf));
425
- }
426
- TypeGuard.TNumber = TNumber;
427
- /** Returns true if the given value is TObject */
428
- function TObject(schema) {
429
- // prettier-ignore
430
- return (TKindOf(schema, 'Object') &&
431
- schema.type === 'object' &&
432
- IsOptionalString(schema.$id) &&
433
- ValueGuard.IsObject(schema.properties) &&
434
- IsAdditionalProperties(schema.additionalProperties) &&
435
- IsOptionalNumber(schema.minProperties) &&
436
- IsOptionalNumber(schema.maxProperties) &&
437
- Object.entries(schema.properties).every(([key, schema]) => IsControlCharacterFree(key) && TSchema(schema)));
438
- }
439
- TypeGuard.TObject = TObject;
440
- /** Returns true if the given value is TPromise */
441
- function TPromise(schema) {
442
- // prettier-ignore
443
- return (TKindOf(schema, 'Promise') &&
444
- schema.type === 'Promise' &&
445
- IsOptionalString(schema.$id) &&
446
- TSchema(schema.item));
447
- }
448
- TypeGuard.TPromise = TPromise;
449
- /** Returns true if the given value is TRecord */
450
- function TRecord(schema) {
451
- // prettier-ignore
452
- return (TKindOf(schema, 'Record') &&
453
- schema.type === 'object' &&
454
- IsOptionalString(schema.$id) &&
455
- IsAdditionalProperties(schema.additionalProperties) &&
456
- ValueGuard.IsObject(schema.patternProperties) &&
457
- ((schema) => {
458
- const keys = Object.getOwnPropertyNames(schema.patternProperties);
459
- return (keys.length === 1 &&
460
- IsPattern(keys[0]) &&
461
- ValueGuard.IsObject(schema.patternProperties) &&
462
- TSchema(schema.patternProperties[keys[0]]));
463
- })(schema));
464
- }
465
- TypeGuard.TRecord = TRecord;
466
- /** Returns true if this value is TRecursive */
467
- function TRecursive(schema) {
468
- return ValueGuard.IsObject(schema) && exports.Hint in schema && schema[exports.Hint] === 'Recursive';
469
- }
470
- TypeGuard.TRecursive = TRecursive;
471
- /** Returns true if the given value is TRef */
472
- function TRef(schema) {
473
- // prettier-ignore
474
- return (TKindOf(schema, 'Ref') &&
475
- IsOptionalString(schema.$id) &&
476
- ValueGuard.IsString(schema.$ref));
477
- }
478
- TypeGuard.TRef = TRef;
479
- /** Returns true if the given value is TString */
480
- function TString(schema) {
481
- // prettier-ignore
482
- return (TKindOf(schema, 'String') &&
483
- schema.type === 'string' &&
484
- IsOptionalString(schema.$id) &&
485
- IsOptionalNumber(schema.minLength) &&
486
- IsOptionalNumber(schema.maxLength) &&
487
- IsOptionalPattern(schema.pattern) &&
488
- IsOptionalFormat(schema.format));
489
- }
490
- TypeGuard.TString = TString;
491
- /** Returns true if the given value is TSymbol */
492
- function TSymbol(schema) {
493
- // prettier-ignore
494
- return (TKindOf(schema, 'Symbol') &&
495
- schema.type === 'symbol' &&
496
- IsOptionalString(schema.$id));
497
- }
498
- TypeGuard.TSymbol = TSymbol;
499
- /** Returns true if the given value is TTemplateLiteral */
500
- function TTemplateLiteral(schema) {
501
- // prettier-ignore
502
- return (TKindOf(schema, 'TemplateLiteral') &&
503
- schema.type === 'string' &&
504
- ValueGuard.IsString(schema.pattern) &&
505
- schema.pattern[0] === '^' &&
506
- schema.pattern[schema.pattern.length - 1] === '$');
507
- }
508
- TypeGuard.TTemplateLiteral = TTemplateLiteral;
509
- /** Returns true if the given value is TThis */
510
- function TThis(schema) {
511
- // prettier-ignore
512
- return (TKindOf(schema, 'This') &&
513
- IsOptionalString(schema.$id) &&
514
- ValueGuard.IsString(schema.$ref));
515
- }
516
- TypeGuard.TThis = TThis;
517
- /** Returns true of this value is TTransform */
518
- function TTransform(schema) {
519
- return ValueGuard.IsObject(schema) && exports.Transform in schema;
520
- }
521
- TypeGuard.TTransform = TTransform;
522
- /** Returns true if the given value is TTuple */
523
- function TTuple(schema) {
524
- // prettier-ignore
525
- return (TKindOf(schema, 'Tuple') &&
526
- schema.type === 'array' &&
527
- IsOptionalString(schema.$id) &&
528
- ValueGuard.IsNumber(schema.minItems) &&
529
- ValueGuard.IsNumber(schema.maxItems) &&
530
- schema.minItems === schema.maxItems &&
531
- (( // empty
532
- ValueGuard.IsUndefined(schema.items) &&
533
- ValueGuard.IsUndefined(schema.additionalItems) &&
534
- schema.minItems === 0) || (ValueGuard.IsArray(schema.items) &&
535
- schema.items.every(schema => TSchema(schema)))));
536
- }
537
- TypeGuard.TTuple = TTuple;
538
- /** Returns true if the given value is TUndefined */
539
- function TUndefined(schema) {
540
- // prettier-ignore
541
- return (TKindOf(schema, 'Undefined') &&
542
- schema.type === 'undefined' &&
543
- IsOptionalString(schema.$id));
544
- }
545
- TypeGuard.TUndefined = TUndefined;
546
- /** Returns true if the given value is TUnion<Literal<string | number>[]> */
547
- function TUnionLiteral(schema) {
548
- return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
549
- }
550
- TypeGuard.TUnionLiteral = TUnionLiteral;
551
- /** Returns true if the given value is TUnion */
552
- function TUnion(schema) {
553
- // prettier-ignore
554
- return (TKindOf(schema, 'Union') &&
555
- IsOptionalString(schema.$id) &&
556
- ValueGuard.IsObject(schema) &&
557
- ValueGuard.IsArray(schema.anyOf) &&
558
- schema.anyOf.every(schema => TSchema(schema)));
559
- }
560
- TypeGuard.TUnion = TUnion;
561
- /** Returns true if the given value is TUint8Array */
562
- function TUint8Array(schema) {
563
- // prettier-ignore
564
- return (TKindOf(schema, 'Uint8Array') &&
565
- schema.type === 'Uint8Array' &&
566
- IsOptionalString(schema.$id) &&
567
- IsOptionalNumber(schema.minByteLength) &&
568
- IsOptionalNumber(schema.maxByteLength));
569
- }
570
- TypeGuard.TUint8Array = TUint8Array;
571
- /** Returns true if the given value is TUnknown */
572
- function TUnknown(schema) {
573
- // prettier-ignore
574
- return (TKindOf(schema, 'Unknown') &&
575
- IsOptionalString(schema.$id));
576
- }
577
- TypeGuard.TUnknown = TUnknown;
578
- /** Returns true if the given value is a raw TUnsafe */
579
- function TUnsafe(schema) {
580
- return TKindOf(schema, 'Unsafe');
581
- }
582
- TypeGuard.TUnsafe = TUnsafe;
583
- /** Returns true if the given value is TVoid */
584
- function TVoid(schema) {
585
- // prettier-ignore
586
- return (TKindOf(schema, 'Void') &&
587
- schema.type === 'void' &&
588
- IsOptionalString(schema.$id));
589
- }
590
- TypeGuard.TVoid = TVoid;
591
- /** Returns true if this value has a Readonly symbol */
592
- function TReadonly(schema) {
593
- return ValueGuard.IsObject(schema) && schema[exports.Readonly] === 'Readonly';
594
- }
595
- TypeGuard.TReadonly = TReadonly;
596
- /** Returns true if this value has a Optional symbol */
597
- function TOptional(schema) {
598
- return ValueGuard.IsObject(schema) && schema[exports.Optional] === 'Optional';
599
- }
600
- TypeGuard.TOptional = TOptional;
601
- /** Returns true if the given value is TSchema */
602
- function TSchema(schema) {
603
- // prettier-ignore
604
- return (ValueGuard.IsObject(schema)) && (TAny(schema) ||
605
- TArray(schema) ||
606
- TBoolean(schema) ||
607
- TBigInt(schema) ||
608
- TAsyncIterator(schema) ||
609
- TConstructor(schema) ||
610
- TDate(schema) ||
611
- TFunction(schema) ||
612
- TInteger(schema) ||
613
- TIntersect(schema) ||
614
- TIterator(schema) ||
615
- TLiteral(schema) ||
616
- TNever(schema) ||
617
- TNot(schema) ||
618
- TNull(schema) ||
619
- TNumber(schema) ||
620
- TObject(schema) ||
621
- TPromise(schema) ||
622
- TRecord(schema) ||
623
- TRef(schema) ||
624
- TString(schema) ||
625
- TSymbol(schema) ||
626
- TTemplateLiteral(schema) ||
627
- TThis(schema) ||
628
- TTuple(schema) ||
629
- TUndefined(schema) ||
630
- TUnion(schema) ||
631
- TUint8Array(schema) ||
632
- TUnknown(schema) ||
633
- TUnsafe(schema) ||
634
- TVoid(schema) ||
635
- (TKind(schema) && TypeRegistry.Has(schema[exports.Kind])));
636
- }
637
- TypeGuard.TSchema = TSchema;
638
- })(TypeGuard || (exports.TypeGuard = TypeGuard = {}));
639
- // --------------------------------------------------------------------------
640
- // ExtendsUndefined
641
- // --------------------------------------------------------------------------
642
- /** Fast undefined check used for properties of type undefined */
643
- var ExtendsUndefined;
644
- (function (ExtendsUndefined) {
645
- function Check(schema) {
646
- return schema[exports.Kind] === 'Intersect'
647
- ? schema.allOf.every((schema) => Check(schema))
648
- : schema[exports.Kind] === 'Union'
649
- ? schema.anyOf.some((schema) => Check(schema))
650
- : schema[exports.Kind] === 'Undefined'
651
- ? true
652
- : schema[exports.Kind] === 'Not'
653
- ? !Check(schema.not)
654
- : false;
655
- }
656
- ExtendsUndefined.Check = Check;
657
- })(ExtendsUndefined || (exports.ExtendsUndefined = ExtendsUndefined = {}));
658
- // --------------------------------------------------------------------------
659
- // TypeExtends
660
- // --------------------------------------------------------------------------
661
- class TypeExtendsError extends TypeBoxError {
662
- }
663
- exports.TypeExtendsError = TypeExtendsError;
664
- var TypeExtendsResult;
665
- (function (TypeExtendsResult) {
666
- TypeExtendsResult[TypeExtendsResult["Union"] = 0] = "Union";
667
- TypeExtendsResult[TypeExtendsResult["True"] = 1] = "True";
668
- TypeExtendsResult[TypeExtendsResult["False"] = 2] = "False";
669
- })(TypeExtendsResult || (exports.TypeExtendsResult = TypeExtendsResult = {}));
670
- var TypeExtends;
671
- (function (TypeExtends) {
672
- // --------------------------------------------------------------------------
673
- // IntoBooleanResult
674
- // --------------------------------------------------------------------------
675
- function IntoBooleanResult(result) {
676
- return result === TypeExtendsResult.False ? result : TypeExtendsResult.True;
677
- }
678
- // --------------------------------------------------------------------------
679
- // Throw
680
- // --------------------------------------------------------------------------
681
- function Throw(message) {
682
- throw new TypeExtendsError(message);
683
- }
684
- // --------------------------------------------------------------------------
685
- // StructuralRight
686
- // --------------------------------------------------------------------------
687
- function IsStructuralRight(right) {
688
- // prettier-ignore
689
- return (TypeGuard.TNever(right) ||
690
- TypeGuard.TIntersect(right) ||
691
- TypeGuard.TUnion(right) ||
692
- TypeGuard.TUnknown(right) ||
693
- TypeGuard.TAny(right));
694
- }
695
- function StructuralRight(left, right) {
696
- // prettier-ignore
697
- return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
698
- TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
699
- TypeGuard.TUnion(right) ? TUnionRight(left, right) :
700
- TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
701
- TypeGuard.TAny(right) ? TAnyRight(left, right) :
702
- Throw('StructuralRight'));
703
- }
704
- // --------------------------------------------------------------------------
705
- // Any
706
- // --------------------------------------------------------------------------
707
- function TAnyRight(left, right) {
708
- return TypeExtendsResult.True;
709
- }
710
- function TAny(left, right) {
711
- // prettier-ignore
712
- return (TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
713
- (TypeGuard.TUnion(right) && right.anyOf.some((schema) => TypeGuard.TAny(schema) || TypeGuard.TUnknown(schema))) ? TypeExtendsResult.True :
714
- TypeGuard.TUnion(right) ? TypeExtendsResult.Union :
715
- TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
716
- TypeGuard.TAny(right) ? TypeExtendsResult.True :
717
- TypeExtendsResult.Union);
718
- }
719
- // --------------------------------------------------------------------------
720
- // Array
721
- // --------------------------------------------------------------------------
722
- function TArrayRight(left, right) {
723
- // prettier-ignore
724
- return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
725
- TypeGuard.TAny(left) ? TypeExtendsResult.Union :
726
- TypeGuard.TNever(left) ? TypeExtendsResult.True :
727
- TypeExtendsResult.False);
728
- }
729
- function TArray(left, right) {
730
- // prettier-ignore
731
- return (TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
732
- IsStructuralRight(right) ? StructuralRight(left, right) :
733
- !TypeGuard.TArray(right) ? TypeExtendsResult.False :
734
- IntoBooleanResult(Visit(left.items, right.items)));
735
- }
736
- // --------------------------------------------------------------------------
737
- // AsyncIterator
738
- // --------------------------------------------------------------------------
739
- function TAsyncIterator(left, right) {
740
- // prettier-ignore
741
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
742
- !TypeGuard.TAsyncIterator(right) ? TypeExtendsResult.False :
743
- IntoBooleanResult(Visit(left.items, right.items)));
744
- }
745
- // --------------------------------------------------------------------------
746
- // BigInt
747
- // --------------------------------------------------------------------------
748
- function TBigInt(left, right) {
749
- // prettier-ignore
750
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
751
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
752
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
753
- TypeGuard.TBigInt(right) ? TypeExtendsResult.True :
754
- TypeExtendsResult.False);
755
- }
756
- // --------------------------------------------------------------------------
757
- // Boolean
758
- // --------------------------------------------------------------------------
759
- function TBooleanRight(left, right) {
760
- return TypeGuard.TLiteral(left) && ValueGuard.IsBoolean(left.const) ? TypeExtendsResult.True : TypeGuard.TBoolean(left) ? TypeExtendsResult.True : TypeExtendsResult.False;
761
- }
762
- function TBoolean(left, right) {
763
- // prettier-ignore
764
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
765
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
766
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
767
- TypeGuard.TBoolean(right) ? TypeExtendsResult.True :
768
- TypeExtendsResult.False);
769
- }
770
- // --------------------------------------------------------------------------
771
- // Constructor
772
- // --------------------------------------------------------------------------
773
- function TConstructor(left, right) {
774
- // prettier-ignore
775
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
776
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
777
- !TypeGuard.TConstructor(right) ? TypeExtendsResult.False :
778
- left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
779
- (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
780
- IntoBooleanResult(Visit(left.returns, right.returns)));
781
- }
782
- // --------------------------------------------------------------------------
783
- // Date
784
- // --------------------------------------------------------------------------
785
- function TDate(left, right) {
786
- // prettier-ignore
787
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
788
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
789
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
790
- TypeGuard.TDate(right) ? TypeExtendsResult.True :
791
- TypeExtendsResult.False);
792
- }
793
- // --------------------------------------------------------------------------
794
- // Function
795
- // --------------------------------------------------------------------------
796
- function TFunction(left, right) {
797
- // prettier-ignore
798
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
799
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
800
- !TypeGuard.TFunction(right) ? TypeExtendsResult.False :
801
- left.parameters.length > right.parameters.length ? TypeExtendsResult.False :
802
- (!left.parameters.every((schema, index) => IntoBooleanResult(Visit(right.parameters[index], schema)) === TypeExtendsResult.True)) ? TypeExtendsResult.False :
803
- IntoBooleanResult(Visit(left.returns, right.returns)));
804
- }
805
- // --------------------------------------------------------------------------
806
- // Integer
807
- // --------------------------------------------------------------------------
808
- function TIntegerRight(left, right) {
809
- // prettier-ignore
810
- return (TypeGuard.TLiteral(left) && ValueGuard.IsNumber(left.const) ? TypeExtendsResult.True :
811
- TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
812
- TypeExtendsResult.False);
813
- }
814
- function TInteger(left, right) {
815
- // prettier-ignore
816
- return (TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
817
- IsStructuralRight(right) ? StructuralRight(left, right) :
818
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
819
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
820
- TypeExtendsResult.False);
821
- }
822
- // --------------------------------------------------------------------------
823
- // Intersect
824
- // --------------------------------------------------------------------------
825
- function TIntersectRight(left, right) {
826
- // prettier-ignore
827
- return right.allOf.every((schema) => Visit(left, schema) === TypeExtendsResult.True)
828
- ? TypeExtendsResult.True
829
- : TypeExtendsResult.False;
830
- }
831
- function TIntersect(left, right) {
832
- // prettier-ignore
833
- return left.allOf.some((schema) => Visit(schema, right) === TypeExtendsResult.True)
834
- ? TypeExtendsResult.True
835
- : TypeExtendsResult.False;
836
- }
837
- // --------------------------------------------------------------------------
838
- // Iterator
839
- // --------------------------------------------------------------------------
840
- function TIterator(left, right) {
841
- // prettier-ignore
842
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
843
- !TypeGuard.TIterator(right) ? TypeExtendsResult.False :
844
- IntoBooleanResult(Visit(left.items, right.items)));
845
- }
846
- // --------------------------------------------------------------------------
847
- // Literal
848
- // --------------------------------------------------------------------------
849
- function TLiteral(left, right) {
850
- // prettier-ignore
851
- return (TypeGuard.TLiteral(right) && right.const === left.const ? TypeExtendsResult.True :
852
- IsStructuralRight(right) ? StructuralRight(left, right) :
853
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
854
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
855
- TypeGuard.TString(right) ? TStringRight(left, right) :
856
- TypeGuard.TNumber(right) ? TNumberRight(left, right) :
857
- TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
858
- TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
859
- TypeExtendsResult.False);
860
- }
861
- // --------------------------------------------------------------------------
862
- // Never
863
- // --------------------------------------------------------------------------
864
- function TNeverRight(left, right) {
865
- return TypeExtendsResult.False;
866
- }
867
- function TNever(left, right) {
868
- return TypeExtendsResult.True;
869
- }
870
- // --------------------------------------------------------------------------
871
- // Not
872
- // --------------------------------------------------------------------------
873
- function UnwrapTNot(schema) {
874
- let [current, depth] = [schema, 0];
875
- while (true) {
876
- if (!TypeGuard.TNot(current))
877
- break;
878
- current = current.not;
879
- depth += 1;
880
- }
881
- return depth % 2 === 0 ? current : exports.Type.Unknown();
882
- }
883
- function TNot(left, right) {
884
- // TypeScript has no concept of negated types, and attempts to correctly check the negated
885
- // type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
886
- // the type. Instead we unwrap to either unknown or T and continue evaluating.
887
- // prettier-ignore
888
- return (TypeGuard.TNot(left) ? Visit(UnwrapTNot(left), right) :
889
- TypeGuard.TNot(right) ? Visit(left, UnwrapTNot(right)) :
890
- Throw('Invalid fallthrough for Not'));
891
- }
892
- // --------------------------------------------------------------------------
893
- // Null
894
- // --------------------------------------------------------------------------
895
- function TNull(left, right) {
896
- // prettier-ignore
897
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
898
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
899
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
900
- TypeGuard.TNull(right) ? TypeExtendsResult.True :
901
- TypeExtendsResult.False);
902
- }
903
- // --------------------------------------------------------------------------
904
- // Number
905
- // --------------------------------------------------------------------------
906
- function TNumberRight(left, right) {
907
- // prettier-ignore
908
- return (TypeGuard.TLiteralNumber(left) ? TypeExtendsResult.True :
909
- TypeGuard.TNumber(left) || TypeGuard.TInteger(left) ? TypeExtendsResult.True :
910
- TypeExtendsResult.False);
911
- }
912
- function TNumber(left, right) {
913
- // prettier-ignore
914
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
915
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
916
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
917
- TypeGuard.TInteger(right) || TypeGuard.TNumber(right) ? TypeExtendsResult.True :
918
- TypeExtendsResult.False);
919
- }
920
- // --------------------------------------------------------------------------
921
- // Object
922
- // --------------------------------------------------------------------------
923
- function IsObjectPropertyCount(schema, count) {
924
- return Object.getOwnPropertyNames(schema.properties).length === count;
925
- }
926
- function IsObjectStringLike(schema) {
927
- return IsObjectArrayLike(schema);
928
- }
929
- function IsObjectSymbolLike(schema) {
930
- // prettier-ignore
931
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'description' in schema.properties && TypeGuard.TUnion(schema.properties.description) && schema.properties.description.anyOf.length === 2 && ((TypeGuard.TString(schema.properties.description.anyOf[0]) &&
932
- TypeGuard.TUndefined(schema.properties.description.anyOf[1])) || (TypeGuard.TString(schema.properties.description.anyOf[1]) &&
933
- TypeGuard.TUndefined(schema.properties.description.anyOf[0]))));
934
- }
935
- function IsObjectNumberLike(schema) {
936
- return IsObjectPropertyCount(schema, 0);
937
- }
938
- function IsObjectBooleanLike(schema) {
939
- return IsObjectPropertyCount(schema, 0);
940
- }
941
- function IsObjectBigIntLike(schema) {
942
- return IsObjectPropertyCount(schema, 0);
943
- }
944
- function IsObjectDateLike(schema) {
945
- return IsObjectPropertyCount(schema, 0);
946
- }
947
- function IsObjectUint8ArrayLike(schema) {
948
- return IsObjectArrayLike(schema);
949
- }
950
- function IsObjectFunctionLike(schema) {
951
- const length = exports.Type.Number();
952
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
953
- }
954
- function IsObjectConstructorLike(schema) {
955
- return IsObjectPropertyCount(schema, 0);
956
- }
957
- function IsObjectArrayLike(schema) {
958
- const length = exports.Type.Number();
959
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'length' in schema.properties && IntoBooleanResult(Visit(schema.properties['length'], length)) === TypeExtendsResult.True);
960
- }
961
- function IsObjectPromiseLike(schema) {
962
- const then = exports.Type.Function([exports.Type.Any()], exports.Type.Any());
963
- return IsObjectPropertyCount(schema, 0) || (IsObjectPropertyCount(schema, 1) && 'then' in schema.properties && IntoBooleanResult(Visit(schema.properties['then'], then)) === TypeExtendsResult.True);
964
- }
965
- // --------------------------------------------------------------------------
966
- // Property
967
- // --------------------------------------------------------------------------
968
- function Property(left, right) {
969
- // prettier-ignore
970
- return (Visit(left, right) === TypeExtendsResult.False ? TypeExtendsResult.False :
971
- TypeGuard.TOptional(left) && !TypeGuard.TOptional(right) ? TypeExtendsResult.False :
972
- TypeExtendsResult.True);
973
- }
974
- function TObjectRight(left, right) {
975
- // prettier-ignore
976
- return (TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
977
- TypeGuard.TAny(left) ? TypeExtendsResult.Union : (TypeGuard.TNever(left) ||
978
- (TypeGuard.TLiteralString(left) && IsObjectStringLike(right)) ||
979
- (TypeGuard.TLiteralNumber(left) && IsObjectNumberLike(right)) ||
980
- (TypeGuard.TLiteralBoolean(left) && IsObjectBooleanLike(right)) ||
981
- (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
982
- (TypeGuard.TBigInt(left) && IsObjectBigIntLike(right)) ||
983
- (TypeGuard.TString(left) && IsObjectStringLike(right)) ||
984
- (TypeGuard.TSymbol(left) && IsObjectSymbolLike(right)) ||
985
- (TypeGuard.TNumber(left) && IsObjectNumberLike(right)) ||
986
- (TypeGuard.TInteger(left) && IsObjectNumberLike(right)) ||
987
- (TypeGuard.TBoolean(left) && IsObjectBooleanLike(right)) ||
988
- (TypeGuard.TUint8Array(left) && IsObjectUint8ArrayLike(right)) ||
989
- (TypeGuard.TDate(left) && IsObjectDateLike(right)) ||
990
- (TypeGuard.TConstructor(left) && IsObjectConstructorLike(right)) ||
991
- (TypeGuard.TFunction(left) && IsObjectFunctionLike(right))) ? TypeExtendsResult.True :
992
- (TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => {
993
- // When expressing a Record with literal key values, the Record is converted into a Object with
994
- // the Hint assigned as `Record`. This is used to invert the extends logic.
995
- return right[exports.Hint] === 'Record' ? TypeExtendsResult.True : TypeExtendsResult.False;
996
- })() :
997
- (TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => {
998
- return IsObjectPropertyCount(right, 0)
999
- ? TypeExtendsResult.True
1000
- : TypeExtendsResult.False;
1001
- })() :
1002
- TypeExtendsResult.False);
1003
- }
1004
- function TObject(left, right) {
1005
- // prettier-ignore
1006
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1007
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1008
- !TypeGuard.TObject(right) ? TypeExtendsResult.False :
1009
- (() => {
1010
- for (const key of Object.getOwnPropertyNames(right.properties)) {
1011
- if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) {
1012
- return TypeExtendsResult.False;
1013
- }
1014
- if (TypeGuard.TOptional(right.properties[key])) {
1015
- return TypeExtendsResult.True;
1016
- }
1017
- if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
1018
- return TypeExtendsResult.False;
1019
- }
1020
- }
1021
- return TypeExtendsResult.True;
1022
- })());
1023
- }
1024
- // --------------------------------------------------------------------------
1025
- // Promise
1026
- // --------------------------------------------------------------------------
1027
- function TPromise(left, right) {
1028
- // prettier-ignore
1029
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1030
- TypeGuard.TObject(right) && IsObjectPromiseLike(right) ? TypeExtendsResult.True :
1031
- !TypeGuard.TPromise(right) ? TypeExtendsResult.False :
1032
- IntoBooleanResult(Visit(left.item, right.item)));
1033
- }
1034
- // --------------------------------------------------------------------------
1035
- // Record
1036
- // --------------------------------------------------------------------------
1037
- function RecordKey(schema) {
1038
- // prettier-ignore
1039
- return (exports.PatternNumberExact in schema.patternProperties ? exports.Type.Number() :
1040
- exports.PatternStringExact in schema.patternProperties ? exports.Type.String() :
1041
- Throw('Unknown record key pattern'));
1042
- }
1043
- function RecordValue(schema) {
1044
- // prettier-ignore
1045
- return (exports.PatternNumberExact in schema.patternProperties ? schema.patternProperties[exports.PatternNumberExact] :
1046
- exports.PatternStringExact in schema.patternProperties ? schema.patternProperties[exports.PatternStringExact] :
1047
- Throw('Unable to get record value schema'));
1048
- }
1049
- function TRecordRight(left, right) {
1050
- const [Key, Value] = [RecordKey(right), RecordValue(right)];
1051
- // prettier-ignore
1052
- return ((TypeGuard.TLiteralString(left) && TypeGuard.TNumber(Key) && IntoBooleanResult(Visit(left, Value)) === TypeExtendsResult.True) ? TypeExtendsResult.True :
1053
- TypeGuard.TUint8Array(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1054
- TypeGuard.TString(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1055
- TypeGuard.TArray(left) && TypeGuard.TNumber(Key) ? Visit(left, Value) :
1056
- TypeGuard.TObject(left) ? (() => {
1057
- for (const key of Object.getOwnPropertyNames(left.properties)) {
1058
- if (Property(Value, left.properties[key]) === TypeExtendsResult.False) {
1059
- return TypeExtendsResult.False;
1060
- }
1061
- }
1062
- return TypeExtendsResult.True;
1063
- })() :
1064
- TypeExtendsResult.False);
1065
- }
1066
- function TRecord(left, right) {
1067
- // prettier-ignore
1068
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1069
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1070
- !TypeGuard.TRecord(right) ? TypeExtendsResult.False :
1071
- Visit(RecordValue(left), RecordValue(right)));
1072
- }
1073
- // --------------------------------------------------------------------------
1074
- // String
1075
- // --------------------------------------------------------------------------
1076
- function TStringRight(left, right) {
1077
- // prettier-ignore
1078
- return (TypeGuard.TLiteral(left) && ValueGuard.IsString(left.const) ? TypeExtendsResult.True :
1079
- TypeGuard.TString(left) ? TypeExtendsResult.True :
1080
- TypeExtendsResult.False);
1081
- }
1082
- function TString(left, right) {
1083
- // prettier-ignore
1084
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1085
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1086
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1087
- TypeGuard.TString(right) ? TypeExtendsResult.True :
1088
- TypeExtendsResult.False);
1089
- }
1090
- // --------------------------------------------------------------------------
1091
- // Symbol
1092
- // --------------------------------------------------------------------------
1093
- function TSymbol(left, right) {
1094
- // prettier-ignore
1095
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1096
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1097
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1098
- TypeGuard.TSymbol(right) ? TypeExtendsResult.True :
1099
- TypeExtendsResult.False);
1100
- }
1101
- // --------------------------------------------------------------------------
1102
- // TemplateLiteral
1103
- // --------------------------------------------------------------------------
1104
- function TTemplateLiteral(left, right) {
1105
- // TemplateLiteral types are resolved to either unions for finite expressions or string
1106
- // for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
1107
- // either type and continue evaluating.
1108
- // prettier-ignore
1109
- return (TypeGuard.TTemplateLiteral(left) ? Visit(TemplateLiteralResolver.Resolve(left), right) :
1110
- TypeGuard.TTemplateLiteral(right) ? Visit(left, TemplateLiteralResolver.Resolve(right)) :
1111
- Throw('Invalid fallthrough for TemplateLiteral'));
1112
- }
1113
- // --------------------------------------------------------------------------
1114
- // Tuple
1115
- // --------------------------------------------------------------------------
1116
- function IsArrayOfTuple(left, right) {
1117
- // prettier-ignore
1118
- return (TypeGuard.TArray(right) &&
1119
- left.items !== undefined &&
1120
- left.items.every((schema) => Visit(schema, right.items) === TypeExtendsResult.True));
1121
- }
1122
- function TTupleRight(left, right) {
1123
- // prettier-ignore
1124
- return (TypeGuard.TNever(left) ? TypeExtendsResult.True :
1125
- TypeGuard.TUnknown(left) ? TypeExtendsResult.False :
1126
- TypeGuard.TAny(left) ? TypeExtendsResult.Union :
1127
- TypeExtendsResult.False);
1128
- }
1129
- function TTuple(left, right) {
1130
- // prettier-ignore
1131
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1132
- TypeGuard.TObject(right) && IsObjectArrayLike(right) ? TypeExtendsResult.True :
1133
- TypeGuard.TArray(right) && IsArrayOfTuple(left, right) ? TypeExtendsResult.True :
1134
- !TypeGuard.TTuple(right) ? TypeExtendsResult.False :
1135
- (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) || (!ValueGuard.IsUndefined(left.items) && ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.False :
1136
- (ValueGuard.IsUndefined(left.items) && !ValueGuard.IsUndefined(right.items)) ? TypeExtendsResult.True :
1137
- left.items.every((schema, index) => Visit(schema, right.items[index]) === TypeExtendsResult.True) ? TypeExtendsResult.True :
1138
- TypeExtendsResult.False);
1139
- }
1140
- // --------------------------------------------------------------------------
1141
- // Uint8Array
1142
- // --------------------------------------------------------------------------
1143
- function TUint8Array(left, right) {
1144
- // prettier-ignore
1145
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1146
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1147
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1148
- TypeGuard.TUint8Array(right) ? TypeExtendsResult.True :
1149
- TypeExtendsResult.False);
1150
- }
1151
- // --------------------------------------------------------------------------
1152
- // Undefined
1153
- // --------------------------------------------------------------------------
1154
- function TUndefined(left, right) {
1155
- // prettier-ignore
1156
- return (IsStructuralRight(right) ? StructuralRight(left, right) :
1157
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1158
- TypeGuard.TRecord(right) ? TRecordRight(left, right) :
1159
- TypeGuard.TVoid(right) ? VoidRight(left, right) :
1160
- TypeGuard.TUndefined(right) ? TypeExtendsResult.True :
1161
- TypeExtendsResult.False);
1162
- }
1163
- // --------------------------------------------------------------------------
1164
- // Union
1165
- // --------------------------------------------------------------------------
1166
- function TUnionRight(left, right) {
1167
- // prettier-ignore
1168
- return right.anyOf.some((schema) => Visit(left, schema) === TypeExtendsResult.True)
1169
- ? TypeExtendsResult.True
1170
- : TypeExtendsResult.False;
1171
- }
1172
- function TUnion(left, right) {
1173
- // prettier-ignore
1174
- return left.anyOf.every((schema) => Visit(schema, right) === TypeExtendsResult.True)
1175
- ? TypeExtendsResult.True
1176
- : TypeExtendsResult.False;
1177
- }
1178
- // --------------------------------------------------------------------------
1179
- // Unknown
1180
- // --------------------------------------------------------------------------
1181
- function TUnknownRight(left, right) {
1182
- return TypeExtendsResult.True;
1183
- }
1184
- function TUnknown(left, right) {
1185
- // prettier-ignore
1186
- return (TypeGuard.TNever(right) ? TNeverRight(left, right) :
1187
- TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
1188
- TypeGuard.TUnion(right) ? TUnionRight(left, right) :
1189
- TypeGuard.TAny(right) ? TAnyRight(left, right) :
1190
- TypeGuard.TString(right) ? TStringRight(left, right) :
1191
- TypeGuard.TNumber(right) ? TNumberRight(left, right) :
1192
- TypeGuard.TInteger(right) ? TIntegerRight(left, right) :
1193
- TypeGuard.TBoolean(right) ? TBooleanRight(left, right) :
1194
- TypeGuard.TArray(right) ? TArrayRight(left, right) :
1195
- TypeGuard.TTuple(right) ? TTupleRight(left, right) :
1196
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1197
- TypeGuard.TUnknown(right) ? TypeExtendsResult.True :
1198
- TypeExtendsResult.False);
1199
- }
1200
- // --------------------------------------------------------------------------
1201
- // Void
1202
- // --------------------------------------------------------------------------
1203
- function VoidRight(left, right) {
1204
- // prettier-ignore
1205
- return TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
1206
- TypeGuard.TUndefined(left) ? TypeExtendsResult.True :
1207
- TypeExtendsResult.False;
1208
- }
1209
- function TVoid(left, right) {
1210
- // prettier-ignore
1211
- return TypeGuard.TIntersect(right) ? TIntersectRight(left, right) :
1212
- TypeGuard.TUnion(right) ? TUnionRight(left, right) :
1213
- TypeGuard.TUnknown(right) ? TUnknownRight(left, right) :
1214
- TypeGuard.TAny(right) ? TAnyRight(left, right) :
1215
- TypeGuard.TObject(right) ? TObjectRight(left, right) :
1216
- TypeGuard.TVoid(right) ? TypeExtendsResult.True :
1217
- TypeExtendsResult.False;
1218
- }
1219
- function Visit(left, right) {
1220
- // prettier-ignore
1221
- return (
1222
- // resolvable
1223
- (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right)) ? TTemplateLiteral(left, right) :
1224
- (TypeGuard.TNot(left) || TypeGuard.TNot(right)) ? TNot(left, right) :
1225
- // standard
1226
- TypeGuard.TAny(left) ? TAny(left, right) :
1227
- TypeGuard.TArray(left) ? TArray(left, right) :
1228
- TypeGuard.TBigInt(left) ? TBigInt(left, right) :
1229
- TypeGuard.TBoolean(left) ? TBoolean(left, right) :
1230
- TypeGuard.TAsyncIterator(left) ? TAsyncIterator(left, right) :
1231
- TypeGuard.TConstructor(left) ? TConstructor(left, right) :
1232
- TypeGuard.TDate(left) ? TDate(left, right) :
1233
- TypeGuard.TFunction(left) ? TFunction(left, right) :
1234
- TypeGuard.TInteger(left) ? TInteger(left, right) :
1235
- TypeGuard.TIntersect(left) ? TIntersect(left, right) :
1236
- TypeGuard.TIterator(left) ? TIterator(left, right) :
1237
- TypeGuard.TLiteral(left) ? TLiteral(left, right) :
1238
- TypeGuard.TNever(left) ? TNever(left, right) :
1239
- TypeGuard.TNull(left) ? TNull(left, right) :
1240
- TypeGuard.TNumber(left) ? TNumber(left, right) :
1241
- TypeGuard.TObject(left) ? TObject(left, right) :
1242
- TypeGuard.TRecord(left) ? TRecord(left, right) :
1243
- TypeGuard.TString(left) ? TString(left, right) :
1244
- TypeGuard.TSymbol(left) ? TSymbol(left, right) :
1245
- TypeGuard.TTuple(left) ? TTuple(left, right) :
1246
- TypeGuard.TPromise(left) ? TPromise(left, right) :
1247
- TypeGuard.TUint8Array(left) ? TUint8Array(left, right) :
1248
- TypeGuard.TUndefined(left) ? TUndefined(left, right) :
1249
- TypeGuard.TUnion(left) ? TUnion(left, right) :
1250
- TypeGuard.TUnknown(left) ? TUnknown(left, right) :
1251
- TypeGuard.TVoid(left) ? TVoid(left, right) :
1252
- Throw(`Unknown left type operand '${left[exports.Kind]}'`));
1253
- }
1254
- function Extends(left, right) {
1255
- return Visit(left, right);
1256
- }
1257
- TypeExtends.Extends = Extends;
1258
- })(TypeExtends || (exports.TypeExtends = TypeExtends = {}));
1259
- // --------------------------------------------------------------------------
1260
- // TypeClone
1261
- // --------------------------------------------------------------------------
1262
- /** Specialized Clone for Types */
1263
- var TypeClone;
1264
- (function (TypeClone) {
1265
- function ArrayType(value) {
1266
- return value.map((value) => Visit(value));
1267
- }
1268
- function DateType(value) {
1269
- return new Date(value.getTime());
1270
- }
1271
- function Uint8ArrayType(value) {
1272
- return new Uint8Array(value);
1273
- }
1274
- function ObjectType(value) {
1275
- const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
1276
- const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
1277
- return { ...clonedProperties, ...clonedSymbols };
1278
- }
1279
- function Visit(value) {
1280
- // prettier-ignore
1281
- return (ValueGuard.IsArray(value) ? ArrayType(value) :
1282
- ValueGuard.IsDate(value) ? DateType(value) :
1283
- ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
1284
- ValueGuard.IsObject(value) ? ObjectType(value) :
1285
- value);
1286
- }
1287
- /** Clones a Rest */
1288
- function Rest(schemas) {
1289
- return schemas.map((schema) => Type(schema));
1290
- }
1291
- TypeClone.Rest = Rest;
1292
- /** Clones a Type */
1293
- function Type(schema, options = {}) {
1294
- return { ...Visit(schema), ...options };
1295
- }
1296
- TypeClone.Type = Type;
1297
- })(TypeClone || (exports.TypeClone = TypeClone = {}));
1298
- // --------------------------------------------------------------------------
1299
- // IndexedAccessor
1300
- // --------------------------------------------------------------------------
1301
- var IndexedAccessor;
1302
- (function (IndexedAccessor) {
1303
- function OptionalUnwrap(schema) {
1304
- return schema.map((schema) => {
1305
- const { [exports.Optional]: _, ...clone } = TypeClone.Type(schema);
1306
- return clone;
1307
- });
1308
- }
1309
- function IsIntersectOptional(schema) {
1310
- return schema.every((schema) => TypeGuard.TOptional(schema));
1311
- }
1312
- function IsUnionOptional(schema) {
1313
- return schema.some((schema) => TypeGuard.TOptional(schema));
1314
- }
1315
- function ResolveIntersect(schema) {
1316
- return IsIntersectOptional(schema.allOf) ? exports.Type.Optional(exports.Type.Intersect(OptionalUnwrap(schema.allOf))) : schema;
1317
- }
1318
- function ResolveUnion(schema) {
1319
- return IsUnionOptional(schema.anyOf) ? exports.Type.Optional(exports.Type.Union(OptionalUnwrap(schema.anyOf))) : schema;
1320
- }
1321
- function ResolveOptional(schema) {
1322
- // prettier-ignore
1323
- return schema[exports.Kind] === 'Intersect' ? ResolveIntersect(schema) :
1324
- schema[exports.Kind] === 'Union' ? ResolveUnion(schema) :
1325
- schema;
1326
- }
1327
- function TIntersect(schema, key) {
1328
- const resolved = schema.allOf.reduce((acc, schema) => {
1329
- const indexed = Visit(schema, key);
1330
- return indexed[exports.Kind] === 'Never' ? acc : [...acc, indexed];
1331
- }, []);
1332
- return ResolveOptional(exports.Type.Intersect(resolved));
1333
- }
1334
- function TUnion(schema, key) {
1335
- const resolved = schema.anyOf.map((schema) => Visit(schema, key));
1336
- return ResolveOptional(exports.Type.Union(resolved));
1337
- }
1338
- function TObject(schema, key) {
1339
- const property = schema.properties[key];
1340
- return ValueGuard.IsUndefined(property) ? exports.Type.Never() : exports.Type.Union([property]);
1341
- }
1342
- function TTuple(schema, key) {
1343
- const items = schema.items;
1344
- if (ValueGuard.IsUndefined(items))
1345
- return exports.Type.Never();
1346
- const element = items[key]; //
1347
- if (ValueGuard.IsUndefined(element))
1348
- return exports.Type.Never();
1349
- return element;
1350
- }
1351
- function Visit(schema, key) {
1352
- // prettier-ignore
1353
- return schema[exports.Kind] === 'Intersect' ? TIntersect(schema, key) :
1354
- schema[exports.Kind] === 'Union' ? TUnion(schema, key) :
1355
- schema[exports.Kind] === 'Object' ? TObject(schema, key) :
1356
- schema[exports.Kind] === 'Tuple' ? TTuple(schema, key) :
1357
- exports.Type.Never();
1358
- }
1359
- function Resolve(schema, keys, options = {}) {
1360
- const resolved = keys.map((key) => Visit(schema, key.toString()));
1361
- return ResolveOptional(exports.Type.Union(resolved, options));
1362
- }
1363
- IndexedAccessor.Resolve = Resolve;
1364
- })(IndexedAccessor || (exports.IndexedAccessor = IndexedAccessor = {}));
1365
- // --------------------------------------------------------------------------
1366
- // Intrinsic
1367
- // --------------------------------------------------------------------------
1368
- var Intrinsic;
1369
- (function (Intrinsic) {
1370
- function Uncapitalize(value) {
1371
- const [first, rest] = [value.slice(0, 1), value.slice(1)];
1372
- return `${first.toLowerCase()}${rest}`;
1373
- }
1374
- function Capitalize(value) {
1375
- const [first, rest] = [value.slice(0, 1), value.slice(1)];
1376
- return `${first.toUpperCase()}${rest}`;
1377
- }
1378
- function Uppercase(value) {
1379
- return value.toUpperCase();
1380
- }
1381
- function Lowercase(value) {
1382
- return value.toLowerCase();
1383
- }
1384
- function IntrinsicTemplateLiteral(schema, mode) {
1385
- // note: template literals require special runtime handling as they are encoded in string patterns.
1386
- // This diverges from the mapped type which would otherwise map on the template literal kind.
1387
- const expression = TemplateLiteralParser.ParseExact(schema.pattern);
1388
- const finite = TemplateLiteralFinite.Check(expression);
1389
- if (!finite)
1390
- return { ...schema, pattern: IntrinsicLiteral(schema.pattern, mode) };
1391
- const strings = [...TemplateLiteralGenerator.Generate(expression)];
1392
- const literals = strings.map((value) => exports.Type.Literal(value));
1393
- const mapped = IntrinsicRest(literals, mode);
1394
- const union = exports.Type.Union(mapped);
1395
- return exports.Type.TemplateLiteral([union]);
1396
- }
1397
- function IntrinsicLiteral(value, mode) {
1398
- // prettier-ignore
1399
- return typeof value === 'string' ? (mode === 'Uncapitalize' ? Uncapitalize(value) :
1400
- mode === 'Capitalize' ? Capitalize(value) :
1401
- mode === 'Uppercase' ? Uppercase(value) :
1402
- mode === 'Lowercase' ? Lowercase(value) :
1403
- value) : value.toString();
1404
- }
1405
- function IntrinsicRest(schema, mode) {
1406
- if (schema.length === 0)
1407
- return [];
1408
- const [L, ...R] = schema;
1409
- return [Map(L, mode), ...IntrinsicRest(R, mode)];
1410
- }
1411
- function Visit(schema, mode) {
1412
- // prettier-ignore
1413
- return TypeGuard.TTemplateLiteral(schema) ? IntrinsicTemplateLiteral(schema, mode) :
1414
- TypeGuard.TUnion(schema) ? exports.Type.Union(IntrinsicRest(schema.anyOf, mode)) :
1415
- TypeGuard.TLiteral(schema) ? exports.Type.Literal(IntrinsicLiteral(schema.const, mode)) :
1416
- schema;
1417
- }
1418
- /** Applies an intrinsic string manipulation to the given type. */
1419
- function Map(schema, mode) {
1420
- return Visit(schema, mode);
1421
- }
1422
- Intrinsic.Map = Map;
1423
- })(Intrinsic || (exports.Intrinsic = Intrinsic = {}));
1424
- // --------------------------------------------------------------------------
1425
- // ObjectMap
1426
- // --------------------------------------------------------------------------
1427
- var ObjectMap;
1428
- (function (ObjectMap) {
1429
- function TIntersect(schema, callback) {
1430
- // prettier-ignore
1431
- return exports.Type.Intersect(schema.allOf.map((inner) => Visit(inner, callback)), { ...schema });
1432
- }
1433
- function TUnion(schema, callback) {
1434
- // prettier-ignore
1435
- return exports.Type.Union(schema.anyOf.map((inner) => Visit(inner, callback)), { ...schema });
1436
- }
1437
- function TObject(schema, callback) {
1438
- return callback(schema);
1439
- }
1440
- function Visit(schema, callback) {
1441
- // There are cases where users need to map objects with unregistered kinds. Using a TypeGuard here would
1442
- // prevent sub schema mapping as unregistered kinds will not pass TSchema checks. This is notable in the
1443
- // case of TObject where unregistered property kinds cause the TObject check to fail. As mapping is only
1444
- // used for composition, we use explicit checks instead.
1445
- // prettier-ignore
1446
- return (schema[exports.Kind] === 'Intersect' ? TIntersect(schema, callback) :
1447
- schema[exports.Kind] === 'Union' ? TUnion(schema, callback) :
1448
- schema[exports.Kind] === 'Object' ? TObject(schema, callback) :
1449
- schema);
1450
- }
1451
- function Map(schema, callback, options) {
1452
- return { ...Visit(TypeClone.Type(schema), callback), ...options };
1453
- }
1454
- ObjectMap.Map = Map;
1455
- })(ObjectMap || (exports.ObjectMap = ObjectMap = {}));
1456
- var KeyResolver;
1457
- (function (KeyResolver) {
1458
- function UnwrapPattern(key) {
1459
- return key[0] === '^' && key[key.length - 1] === '$' ? key.slice(1, key.length - 1) : key;
1460
- }
1461
- function TIntersect(schema, options) {
1462
- return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, options)], []);
1463
- }
1464
- function TUnion(schema, options) {
1465
- const sets = schema.anyOf.map((inner) => Visit(inner, options));
1466
- return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
1467
- }
1468
- function TObject(schema, options) {
1469
- return Object.getOwnPropertyNames(schema.properties);
1470
- }
1471
- function TRecord(schema, options) {
1472
- return options.includePatterns ? Object.getOwnPropertyNames(schema.patternProperties) : [];
1473
- }
1474
- function Visit(schema, options) {
1475
- // prettier-ignore
1476
- return (TypeGuard.TIntersect(schema) ? TIntersect(schema, options) :
1477
- TypeGuard.TUnion(schema) ? TUnion(schema, options) :
1478
- TypeGuard.TObject(schema) ? TObject(schema, options) :
1479
- TypeGuard.TRecord(schema) ? TRecord(schema, options) :
1480
- []);
1481
- }
1482
- /** Resolves an array of keys in this schema */
1483
- function ResolveKeys(schema, options) {
1484
- return [...new Set(Visit(schema, options))];
1485
- }
1486
- KeyResolver.ResolveKeys = ResolveKeys;
1487
- /** Resolves a regular expression pattern matching all keys in this schema */
1488
- function ResolvePattern(schema) {
1489
- const keys = ResolveKeys(schema, { includePatterns: true });
1490
- const pattern = keys.map((key) => `(${UnwrapPattern(key)})`);
1491
- return `^(${pattern.join('|')})$`;
1492
- }
1493
- KeyResolver.ResolvePattern = ResolvePattern;
1494
- })(KeyResolver || (exports.KeyResolver = KeyResolver = {}));
1495
- // --------------------------------------------------------------------------
1496
- // KeyArrayResolver
1497
- // --------------------------------------------------------------------------
1498
- class KeyArrayResolverError extends TypeBoxError {
1499
- }
1500
- exports.KeyArrayResolverError = KeyArrayResolverError;
1501
- var KeyArrayResolver;
1502
- (function (KeyArrayResolver) {
1503
- /** Resolves an array of string[] keys from the given schema or array type. */
1504
- function Resolve(schema) {
1505
- // prettier-ignore
1506
- return Array.isArray(schema) ? schema :
1507
- TypeGuard.TUnionLiteral(schema) ? schema.anyOf.map((schema) => schema.const.toString()) :
1508
- TypeGuard.TLiteral(schema) ? [schema.const] :
1509
- TypeGuard.TTemplateLiteral(schema) ? (() => {
1510
- const expression = TemplateLiteralParser.ParseExact(schema.pattern);
1511
- if (!TemplateLiteralFinite.Check(expression))
1512
- throw new KeyArrayResolverError('Cannot resolve keys from infinite template expression');
1513
- return [...TemplateLiteralGenerator.Generate(expression)];
1514
- })() : [];
1515
- }
1516
- KeyArrayResolver.Resolve = Resolve;
1517
- })(KeyArrayResolver || (exports.KeyArrayResolver = KeyArrayResolver = {}));
1518
- // --------------------------------------------------------------------------
1519
- // UnionResolver
1520
- // --------------------------------------------------------------------------
1521
- var UnionResolver;
1522
- (function (UnionResolver) {
1523
- function* TUnion(union) {
1524
- for (const schema of union.anyOf) {
1525
- if (schema[exports.Kind] === 'Union') {
1526
- yield* TUnion(schema);
1527
- }
1528
- else {
1529
- yield schema;
1530
- }
1531
- }
1532
- }
1533
- /** Returns a resolved union with interior unions flattened */
1534
- function Resolve(union) {
1535
- return exports.Type.Union([...TUnion(union)], { ...union });
1536
- }
1537
- UnionResolver.Resolve = Resolve;
1538
- })(UnionResolver || (exports.UnionResolver = UnionResolver = {}));
1539
- // --------------------------------------------------------------------------
1540
- // TemplateLiteralPattern
1541
- // --------------------------------------------------------------------------
1542
- class TemplateLiteralPatternError extends TypeBoxError {
1543
- }
1544
- exports.TemplateLiteralPatternError = TemplateLiteralPatternError;
1545
- var TemplateLiteralPattern;
1546
- (function (TemplateLiteralPattern) {
1547
- function Throw(message) {
1548
- throw new TemplateLiteralPatternError(message);
1549
- }
1550
- function Escape(value) {
1551
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1552
- }
1553
- function Visit(schema, acc) {
1554
- // prettier-ignore
1555
- return (TypeGuard.TTemplateLiteral(schema) ? schema.pattern.slice(1, schema.pattern.length - 1) :
1556
- TypeGuard.TUnion(schema) ? `(${schema.anyOf.map((schema) => Visit(schema, acc)).join('|')})` :
1557
- TypeGuard.TNumber(schema) ? `${acc}${exports.PatternNumber}` :
1558
- TypeGuard.TInteger(schema) ? `${acc}${exports.PatternNumber}` :
1559
- TypeGuard.TBigInt(schema) ? `${acc}${exports.PatternNumber}` :
1560
- TypeGuard.TString(schema) ? `${acc}${exports.PatternString}` :
1561
- TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` :
1562
- TypeGuard.TBoolean(schema) ? `${acc}${exports.PatternBoolean}` :
1563
- Throw(`Unexpected Kind '${schema[exports.Kind]}'`));
1564
- }
1565
- function Create(kinds) {
1566
- return `^${kinds.map((schema) => Visit(schema, '')).join('')}\$`;
1567
- }
1568
- TemplateLiteralPattern.Create = Create;
1569
- })(TemplateLiteralPattern || (exports.TemplateLiteralPattern = TemplateLiteralPattern = {}));
1570
- // --------------------------------------------------------------------------------------
1571
- // TemplateLiteralResolver
1572
- // --------------------------------------------------------------------------------------
1573
- var TemplateLiteralResolver;
1574
- (function (TemplateLiteralResolver) {
1575
- /** Resolves a template literal as a TUnion */
1576
- function Resolve(template) {
1577
- const expression = TemplateLiteralParser.ParseExact(template.pattern);
1578
- if (!TemplateLiteralFinite.Check(expression))
1579
- return exports.Type.String();
1580
- const literals = [...TemplateLiteralGenerator.Generate(expression)].map((value) => exports.Type.Literal(value));
1581
- return exports.Type.Union(literals);
1582
- }
1583
- TemplateLiteralResolver.Resolve = Resolve;
1584
- })(TemplateLiteralResolver || (exports.TemplateLiteralResolver = TemplateLiteralResolver = {}));
1585
- // --------------------------------------------------------------------------------------
1586
- // TemplateLiteralParser
1587
- // --------------------------------------------------------------------------------------
1588
- class TemplateLiteralParserError extends TypeBoxError {
1589
- }
1590
- exports.TemplateLiteralParserError = TemplateLiteralParserError;
1591
- var TemplateLiteralParser;
1592
- (function (TemplateLiteralParser) {
1593
- function IsNonEscaped(pattern, index, char) {
1594
- return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
1595
- }
1596
- function IsOpenParen(pattern, index) {
1597
- return IsNonEscaped(pattern, index, '(');
1598
- }
1599
- function IsCloseParen(pattern, index) {
1600
- return IsNonEscaped(pattern, index, ')');
1601
- }
1602
- function IsSeparator(pattern, index) {
1603
- return IsNonEscaped(pattern, index, '|');
1604
- }
1605
- function IsGroup(pattern) {
1606
- if (!(IsOpenParen(pattern, 0) && IsCloseParen(pattern, pattern.length - 1)))
1607
- return false;
1608
- let count = 0;
1609
- for (let index = 0; index < pattern.length; index++) {
1610
- if (IsOpenParen(pattern, index))
1611
- count += 1;
1612
- if (IsCloseParen(pattern, index))
1613
- count -= 1;
1614
- if (count === 0 && index !== pattern.length - 1)
1615
- return false;
1616
- }
1617
- return true;
1618
- }
1619
- function InGroup(pattern) {
1620
- return pattern.slice(1, pattern.length - 1);
1621
- }
1622
- function IsPrecedenceOr(pattern) {
1623
- let count = 0;
1624
- for (let index = 0; index < pattern.length; index++) {
1625
- if (IsOpenParen(pattern, index))
1626
- count += 1;
1627
- if (IsCloseParen(pattern, index))
1628
- count -= 1;
1629
- if (IsSeparator(pattern, index) && count === 0)
1630
- return true;
1631
- }
1632
- return false;
1633
- }
1634
- function IsPrecedenceAnd(pattern) {
1635
- for (let index = 0; index < pattern.length; index++) {
1636
- if (IsOpenParen(pattern, index))
1637
- return true;
1638
- }
1639
- return false;
1640
- }
1641
- function Or(pattern) {
1642
- let [count, start] = [0, 0];
1643
- const expressions = [];
1644
- for (let index = 0; index < pattern.length; index++) {
1645
- if (IsOpenParen(pattern, index))
1646
- count += 1;
1647
- if (IsCloseParen(pattern, index))
1648
- count -= 1;
1649
- if (IsSeparator(pattern, index) && count === 0) {
1650
- const range = pattern.slice(start, index);
1651
- if (range.length > 0)
1652
- expressions.push(Parse(range));
1653
- start = index + 1;
1654
- }
1655
- }
1656
- const range = pattern.slice(start);
1657
- if (range.length > 0)
1658
- expressions.push(Parse(range));
1659
- if (expressions.length === 0)
1660
- return { type: 'const', const: '' };
1661
- if (expressions.length === 1)
1662
- return expressions[0];
1663
- return { type: 'or', expr: expressions };
1664
- }
1665
- function And(pattern) {
1666
- function Group(value, index) {
1667
- if (!IsOpenParen(value, index))
1668
- throw new TemplateLiteralParserError(`TemplateLiteralParser: Index must point to open parens`);
1669
- let count = 0;
1670
- for (let scan = index; scan < value.length; scan++) {
1671
- if (IsOpenParen(value, scan))
1672
- count += 1;
1673
- if (IsCloseParen(value, scan))
1674
- count -= 1;
1675
- if (count === 0)
1676
- return [index, scan];
1677
- }
1678
- throw new TemplateLiteralParserError(`TemplateLiteralParser: Unclosed group parens in expression`);
1679
- }
1680
- function Range(pattern, index) {
1681
- for (let scan = index; scan < pattern.length; scan++) {
1682
- if (IsOpenParen(pattern, scan))
1683
- return [index, scan];
1684
- }
1685
- return [index, pattern.length];
1686
- }
1687
- const expressions = [];
1688
- for (let index = 0; index < pattern.length; index++) {
1689
- if (IsOpenParen(pattern, index)) {
1690
- const [start, end] = Group(pattern, index);
1691
- const range = pattern.slice(start, end + 1);
1692
- expressions.push(Parse(range));
1693
- index = end;
1694
- }
1695
- else {
1696
- const [start, end] = Range(pattern, index);
1697
- const range = pattern.slice(start, end);
1698
- if (range.length > 0)
1699
- expressions.push(Parse(range));
1700
- index = end - 1;
1701
- }
1702
- }
1703
- // prettier-ignore
1704
- return (expressions.length === 0) ? { type: 'const', const: '' } :
1705
- (expressions.length === 1) ? expressions[0] :
1706
- { type: 'and', expr: expressions };
1707
- }
1708
- /** Parses a pattern and returns an expression tree */
1709
- function Parse(pattern) {
1710
- // prettier-ignore
1711
- return IsGroup(pattern) ? Parse(InGroup(pattern)) :
1712
- IsPrecedenceOr(pattern) ? Or(pattern) :
1713
- IsPrecedenceAnd(pattern) ? And(pattern) :
1714
- { type: 'const', const: pattern };
1715
- }
1716
- TemplateLiteralParser.Parse = Parse;
1717
- /** Parses a pattern and strips forward and trailing ^ and $ */
1718
- function ParseExact(pattern) {
1719
- return Parse(pattern.slice(1, pattern.length - 1));
1720
- }
1721
- TemplateLiteralParser.ParseExact = ParseExact;
1722
- })(TemplateLiteralParser || (exports.TemplateLiteralParser = TemplateLiteralParser = {}));
1723
- // --------------------------------------------------------------------------------------
1724
- // TemplateLiteralFinite
1725
- // --------------------------------------------------------------------------------------
1726
- class TemplateLiteralFiniteError extends TypeBoxError {
1727
- }
1728
- exports.TemplateLiteralFiniteError = TemplateLiteralFiniteError;
1729
- var TemplateLiteralFinite;
1730
- (function (TemplateLiteralFinite) {
1731
- function Throw(message) {
1732
- throw new TemplateLiteralFiniteError(message);
1733
- }
1734
- function IsNumber(expression) {
1735
- // prettier-ignore
1736
- return (expression.type === 'or' &&
1737
- expression.expr.length === 2 &&
1738
- expression.expr[0].type === 'const' &&
1739
- expression.expr[0].const === '0' &&
1740
- expression.expr[1].type === 'const' &&
1741
- expression.expr[1].const === '[1-9][0-9]*');
1742
- }
1743
- function IsBoolean(expression) {
1744
- // prettier-ignore
1745
- return (expression.type === 'or' &&
1746
- expression.expr.length === 2 &&
1747
- expression.expr[0].type === 'const' &&
1748
- expression.expr[0].const === 'true' &&
1749
- expression.expr[1].type === 'const' &&
1750
- expression.expr[1].const === 'false');
1751
- }
1752
- function IsString(expression) {
1753
- return expression.type === 'const' && expression.const === '.*';
1754
- }
1755
- function Check(expression) {
1756
- // prettier-ignore
1757
- return IsBoolean(expression) ? true :
1758
- IsNumber(expression) || IsString(expression) ? false :
1759
- (expression.type === 'and') ? expression.expr.every((expr) => Check(expr)) :
1760
- (expression.type === 'or') ? expression.expr.every((expr) => Check(expr)) :
1761
- (expression.type === 'const') ? true :
1762
- Throw(`Unknown expression type`);
1763
- }
1764
- TemplateLiteralFinite.Check = Check;
1765
- })(TemplateLiteralFinite || (exports.TemplateLiteralFinite = TemplateLiteralFinite = {}));
1766
- // --------------------------------------------------------------------------------------
1767
- // TemplateLiteralGenerator
1768
- // --------------------------------------------------------------------------------------
1769
- class TemplateLiteralGeneratorError extends TypeBoxError {
1770
- }
1771
- exports.TemplateLiteralGeneratorError = TemplateLiteralGeneratorError;
1772
- var TemplateLiteralGenerator;
1773
- (function (TemplateLiteralGenerator) {
1774
- function* Reduce(buffer) {
1775
- if (buffer.length === 1)
1776
- return yield* buffer[0];
1777
- for (const left of buffer[0]) {
1778
- for (const right of Reduce(buffer.slice(1))) {
1779
- yield `${left}${right}`;
1780
- }
1781
- }
1782
- }
1783
- function* And(expression) {
1784
- return yield* Reduce(expression.expr.map((expr) => [...Generate(expr)]));
1785
- }
1786
- function* Or(expression) {
1787
- for (const expr of expression.expr)
1788
- yield* Generate(expr);
1789
- }
1790
- function* Const(expression) {
1791
- return yield expression.const;
1792
- }
1793
- function* Generate(expression) {
1794
- // prettier-ignore
1795
- return (expression.type === 'and' ? yield* And(expression) :
1796
- expression.type === 'or' ? yield* Or(expression) :
1797
- expression.type === 'const' ? yield* Const(expression) :
1798
- (() => { throw new TemplateLiteralGeneratorError('Unknown expression'); })());
1799
- }
1800
- TemplateLiteralGenerator.Generate = Generate;
1801
- })(TemplateLiteralGenerator || (exports.TemplateLiteralGenerator = TemplateLiteralGenerator = {}));
1802
- // ---------------------------------------------------------------------
1803
- // TemplateLiteralDslParser
1804
- // ---------------------------------------------------------------------
1805
- var TemplateLiteralDslParser;
1806
- (function (TemplateLiteralDslParser) {
1807
- function* ParseUnion(template) {
1808
- const trim = template.trim().replace(/"|'/g, '');
1809
- // prettier-ignore
1810
- return (trim === 'boolean' ? yield exports.Type.Boolean() :
1811
- trim === 'number' ? yield exports.Type.Number() :
1812
- trim === 'bigint' ? yield exports.Type.BigInt() :
1813
- trim === 'string' ? yield exports.Type.String() :
1814
- yield (() => {
1815
- const literals = trim.split('|').map((literal) => exports.Type.Literal(literal.trim()));
1816
- return (literals.length === 0 ? exports.Type.Never() :
1817
- literals.length === 1 ? literals[0] :
1818
- exports.Type.Union(literals));
1819
- })());
1820
- }
1821
- function* ParseTerminal(template) {
1822
- if (template[1] !== '{') {
1823
- const L = exports.Type.Literal('$');
1824
- const R = ParseLiteral(template.slice(1));
1825
- return yield* [L, ...R];
1826
- }
1827
- for (let i = 2; i < template.length; i++) {
1828
- if (template[i] === '}') {
1829
- const L = ParseUnion(template.slice(2, i));
1830
- const R = ParseLiteral(template.slice(i + 1));
1831
- return yield* [...L, ...R];
1832
- }
1833
- }
1834
- yield exports.Type.Literal(template);
1835
- }
1836
- function* ParseLiteral(template) {
1837
- for (let i = 0; i < template.length; i++) {
1838
- if (template[i] === '$') {
1839
- const L = exports.Type.Literal(template.slice(0, i));
1840
- const R = ParseTerminal(template.slice(i));
1841
- return yield* [L, ...R];
1842
- }
1843
- }
1844
- yield exports.Type.Literal(template);
1845
- }
1846
- function Parse(template_dsl) {
1847
- return [...ParseLiteral(template_dsl)];
1848
- }
1849
- TemplateLiteralDslParser.Parse = Parse;
1850
- })(TemplateLiteralDslParser || (exports.TemplateLiteralDslParser = TemplateLiteralDslParser = {}));
1851
- // ---------------------------------------------------------------------
1852
- // TransformBuilder
1853
- // ---------------------------------------------------------------------
1854
- class TransformDecodeBuilder {
1855
- constructor(schema) {
1856
- this.schema = schema;
1857
- }
1858
- Decode(decode) {
1859
- return new TransformEncodeBuilder(this.schema, decode);
1860
- }
1861
- }
1862
- exports.TransformDecodeBuilder = TransformDecodeBuilder;
1863
- class TransformEncodeBuilder {
1864
- constructor(schema, decode) {
1865
- this.schema = schema;
1866
- this.decode = decode;
1867
- }
1868
- Encode(encode) {
1869
- const schema = TypeClone.Type(this.schema);
1870
- // prettier-ignore
1871
- return (TypeGuard.TTransform(schema) ? (() => {
1872
- const Encode = (value) => schema[exports.Transform].Encode(encode(value));
1873
- const Decode = (value) => this.decode(schema[exports.Transform].Decode(value));
1874
- const Codec = { Encode: Encode, Decode: Decode };
1875
- return { ...schema, [exports.Transform]: Codec };
1876
- })() : (() => {
1877
- const Codec = { Decode: this.decode, Encode: encode };
1878
- return { ...schema, [exports.Transform]: Codec };
1879
- })());
1880
- }
1881
- }
1882
- exports.TransformEncodeBuilder = TransformEncodeBuilder;
1883
- // --------------------------------------------------------------------------
1884
- // TypeOrdinal: Used for auto $id generation
1885
- // --------------------------------------------------------------------------
1886
- let TypeOrdinal = 0;
1887
- // --------------------------------------------------------------------------
1888
- // TypeBuilder
1889
- // --------------------------------------------------------------------------
1890
- class TypeBuilderError extends TypeBoxError {
1891
- }
1892
- exports.TypeBuilderError = TypeBuilderError;
1893
- class TypeBuilder {
1894
- /** `[Internal]` Creates a schema without `static` and `params` types */
1895
- Create(schema) {
1896
- return schema;
1897
- }
1898
- /** `[Internal]` Throws a TypeBuilder error with the given message */
1899
- Throw(message) {
1900
- throw new TypeBuilderError(message);
1901
- }
1902
- /** `[Internal]` Discards property keys from the given record type */
1903
- Discard(record, keys) {
1904
- return keys.reduce((acc, key) => {
1905
- const { [key]: _, ...rest } = acc;
1906
- return rest;
1907
- }, record);
1908
- }
1909
- /** `[Json]` Omits compositing symbols from this schema */
1910
- Strict(schema) {
1911
- return JSON.parse(JSON.stringify(schema));
1912
- }
1913
- }
1914
- exports.TypeBuilder = TypeBuilder;
1915
- // --------------------------------------------------------------------------
1916
- // JsonTypeBuilder
1917
- // --------------------------------------------------------------------------
1918
- class JsonTypeBuilder extends TypeBuilder {
1919
- // ------------------------------------------------------------------------
1920
- // Modifiers
1921
- // ------------------------------------------------------------------------
1922
- /** `[Json]` Creates a Readonly and Optional property */
1923
- ReadonlyOptional(schema) {
1924
- return this.Readonly(this.Optional(schema));
1925
- }
1926
- /** `[Json]` Creates a Readonly property */
1927
- Readonly(schema) {
1928
- return { ...TypeClone.Type(schema), [exports.Readonly]: 'Readonly' };
1929
- }
1930
- /** `[Json]` Creates an Optional property */
1931
- Optional(schema) {
1932
- return { ...TypeClone.Type(schema), [exports.Optional]: 'Optional' };
1933
- }
1934
- // ------------------------------------------------------------------------
1935
- // Types
1936
- // ------------------------------------------------------------------------
1937
- /** `[Json]` Creates an Any type */
1938
- Any(options = {}) {
1939
- return this.Create({ ...options, [exports.Kind]: 'Any' });
1940
- }
1941
- /** `[Json]` Creates an Array type */
1942
- Array(schema, options = {}) {
1943
- return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Type(schema) });
1944
- }
1945
- /** `[Json]` Creates a Boolean type */
1946
- Boolean(options = {}) {
1947
- return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
1948
- }
1949
- /** `[Json]` Intrinsic function to Capitalize LiteralString types */
1950
- Capitalize(schema, options = {}) {
1951
- return { ...Intrinsic.Map(TypeClone.Type(schema), 'Capitalize'), ...options };
1952
- }
1953
- /** `[Json]` Creates a Composite object type */
1954
- Composite(objects, options) {
1955
- const intersect = exports.Type.Intersect(objects, {});
1956
- const keys = KeyResolver.ResolveKeys(intersect, { includePatterns: false });
1957
- const properties = keys.reduce((acc, key) => ({ ...acc, [key]: exports.Type.Index(intersect, [key]) }), {});
1958
- return exports.Type.Object(properties, options);
1959
- }
1960
- /** `[Json]` Creates a Enum type */
1961
- Enum(item, options = {}) {
1962
- if (ValueGuard.IsUndefined(item))
1963
- return this.Throw('Enum undefined or empty');
1964
- // prettier-ignore
1965
- const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
1966
- const values2 = [...new Set(values1)];
1967
- const anyOf = values2.map((value) => exports.Type.Literal(value));
1968
- return this.Union(anyOf, { ...options, [exports.Hint]: 'Enum' });
1969
- }
1970
- /** `[Json]` Creates a Conditional type */
1971
- Extends(left, right, trueType, falseType, options = {}) {
1972
- switch (TypeExtends.Extends(left, right)) {
1973
- case TypeExtendsResult.Union:
1974
- return this.Union([TypeClone.Type(trueType, options), TypeClone.Type(falseType, options)]);
1975
- case TypeExtendsResult.True:
1976
- return TypeClone.Type(trueType, options);
1977
- case TypeExtendsResult.False:
1978
- return TypeClone.Type(falseType, options);
1979
- }
1980
- }
1981
- /** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
1982
- Exclude(unionType, excludedMembers, options = {}) {
1983
- // prettier-ignore
1984
- return (TypeGuard.TTemplateLiteral(unionType) ? this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options) :
1985
- TypeGuard.TTemplateLiteral(excludedMembers) ? this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options) :
1986
- TypeGuard.TUnion(unionType) ? (() => {
1987
- const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False);
1988
- return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
1989
- })() :
1990
- TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) :
1991
- TypeClone.Type(unionType, options));
1992
- }
1993
- /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
1994
- Extract(type, union, options = {}) {
1995
- // prettier-ignore
1996
- return (TypeGuard.TTemplateLiteral(type) ? this.Extract(TemplateLiteralResolver.Resolve(type), union, options) :
1997
- TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union), options) :
1998
- TypeGuard.TUnion(type) ? (() => {
1999
- const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False);
2000
- return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options));
2001
- })() :
2002
- TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Type(type, options) :
2003
- this.Never(options));
2004
- }
2005
- /** `[Json]` Returns an Indexed property type for the given keys */
2006
- Index(schema, unresolved, options = {}) {
2007
- // prettier-ignore
2008
- return (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved) ? (() => {
2009
- return TypeClone.Type(schema.items, options);
2010
- })() :
2011
- TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved) ? (() => {
2012
- const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
2013
- const cloned = items.map((schema) => TypeClone.Type(schema));
2014
- return this.Union(cloned, options);
2015
- })() : (() => {
2016
- const keys = KeyArrayResolver.Resolve(unresolved);
2017
- const clone = TypeClone.Type(schema);
2018
- return IndexedAccessor.Resolve(clone, keys, options);
2019
- })());
2020
- }
2021
- /** `[Json]` Creates an Integer type */
2022
- Integer(options = {}) {
2023
- return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
2024
- }
2025
- /** `[Json]` Creates an Intersect type */
2026
- Intersect(allOf, options = {}) {
2027
- if (allOf.length === 0)
2028
- return exports.Type.Never();
2029
- if (allOf.length === 1)
2030
- return TypeClone.Type(allOf[0], options);
2031
- if (allOf.some((schema) => TypeGuard.TTransform(schema)))
2032
- this.Throw('Cannot intersect transform types');
2033
- const objects = allOf.every((schema) => TypeGuard.TObject(schema));
2034
- const cloned = TypeClone.Rest(allOf);
2035
- // prettier-ignore
2036
- const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties)
2037
- ? { unevaluatedProperties: TypeClone.Type(options.unevaluatedProperties) }
2038
- : {};
2039
- return options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects
2040
- ? this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned })
2041
- : this.Create({ ...options, ...clonedUnevaluatedProperties, [exports.Kind]: 'Intersect', allOf: cloned });
2042
- }
2043
- /** `[Json]` Creates a KeyOf type */
2044
- KeyOf(schema, options = {}) {
2045
- // prettier-ignore
2046
- return (TypeGuard.TRecord(schema) ? (() => {
2047
- const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
2048
- return (pattern === exports.PatternNumberExact ? this.Number(options) :
2049
- pattern === exports.PatternStringExact ? this.String(options) :
2050
- this.Throw('Unable to resolve key type from Record key pattern'));
2051
- })() :
2052
- TypeGuard.TTuple(schema) ? (() => {
2053
- const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items;
2054
- const literals = items.map((_, index) => exports.Type.Literal(index.toString()));
2055
- return this.Union(literals, options);
2056
- })() :
2057
- TypeGuard.TArray(schema) ? (() => {
2058
- return this.Number(options);
2059
- })() : (() => {
2060
- const keys = KeyResolver.ResolveKeys(schema, { includePatterns: false });
2061
- if (keys.length === 0)
2062
- return this.Never(options);
2063
- const literals = keys.map((key) => this.Literal(key));
2064
- return this.Union(literals, options);
2065
- })());
2066
- }
2067
- /** `[Json]` Creates a Literal type */
2068
- Literal(value, options = {}) {
2069
- return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
2070
- }
2071
- /** `[Json]` Intrinsic function to Lowercase LiteralString types */
2072
- Lowercase(schema, options = {}) {
2073
- return { ...Intrinsic.Map(TypeClone.Type(schema), 'Lowercase'), ...options };
2074
- }
2075
- /** `[Json]` Creates a Never type */
2076
- Never(options = {}) {
2077
- return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
2078
- }
2079
- /** `[Json]` Creates a Not type */
2080
- Not(schema, options) {
2081
- return this.Create({ ...options, [exports.Kind]: 'Not', not: TypeClone.Type(schema) });
2082
- }
2083
- /** `[Json]` Creates a Null type */
2084
- Null(options = {}) {
2085
- return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
2086
- }
2087
- /** `[Json]` Creates a Number type */
2088
- Number(options = {}) {
2089
- return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
2090
- }
2091
- /** `[Json]` Creates an Object type */
2092
- Object(properties, options = {}) {
2093
- const propertyKeys = Object.getOwnPropertyNames(properties);
2094
- const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]));
2095
- const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
2096
- const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Type(options.additionalProperties) } : {};
2097
- const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(properties[key]) }), {});
2098
- return requiredKeys.length > 0
2099
- ? this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys })
2100
- : this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
2101
- }
2102
- /** `[Json]` Constructs a type whose keys are omitted from the given type */
2103
- Omit(schema, unresolved, options = {}) {
2104
- const keys = KeyArrayResolver.Resolve(unresolved);
2105
- // prettier-ignore
2106
- return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
2107
- if (ValueGuard.IsArray(object.required)) {
2108
- object.required = object.required.filter((key) => !keys.includes(key));
2109
- if (object.required.length === 0)
2110
- delete object.required;
2111
- }
2112
- for (const key of Object.getOwnPropertyNames(object.properties)) {
2113
- if (keys.includes(key))
2114
- delete object.properties[key];
2115
- }
2116
- return this.Create(object);
2117
- }, options);
2118
- }
2119
- /** `[Json]` Constructs a type where all properties are optional */
2120
- Partial(schema, options = {}) {
2121
- // prettier-ignore
2122
- return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
2123
- const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
2124
- return { ...acc, [key]: this.Optional(object.properties[key]) };
2125
- }, {});
2126
- return this.Object(properties, this.Discard(object, ['required']) /* object used as options to retain other constraints */);
2127
- }, options);
2128
- }
2129
- /** `[Json]` Constructs a type whose keys are picked from the given type */
2130
- Pick(schema, unresolved, options = {}) {
2131
- const keys = KeyArrayResolver.Resolve(unresolved);
2132
- // prettier-ignore
2133
- return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
2134
- if (ValueGuard.IsArray(object.required)) {
2135
- object.required = object.required.filter((key) => keys.includes(key));
2136
- if (object.required.length === 0)
2137
- delete object.required;
2138
- }
2139
- for (const key of Object.getOwnPropertyNames(object.properties)) {
2140
- if (!keys.includes(key))
2141
- delete object.properties[key];
2142
- }
2143
- return this.Create(object);
2144
- }, options);
2145
- }
2146
- /** `[Json]` Creates a Record type */
2147
- Record(key, schema, options = {}) {
2148
- // prettier-ignore
2149
- return (TypeGuard.TTemplateLiteral(key) ? (() => {
2150
- const expression = TemplateLiteralParser.ParseExact(key.pattern);
2151
- // prettier-ignore
2152
- return TemplateLiteralFinite.Check(expression)
2153
- ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(schema) }), {}), options))
2154
- : this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Type(schema) } });
2155
- })() :
2156
- TypeGuard.TUnion(key) ? (() => {
2157
- const union = UnionResolver.Resolve(key);
2158
- if (TypeGuard.TUnionLiteral(union)) {
2159
- const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Type(schema) }), {});
2160
- return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
2161
- }
2162
- else
2163
- this.Throw('Record key of type union contains non-literal types');
2164
- })() :
2165
- TypeGuard.TLiteral(key) ? (() => {
2166
- // prettier-ignore
2167
- return (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const))
2168
- ? this.Object({ [key.const]: TypeClone.Type(schema) }, options)
2169
- : this.Throw('Record key of type literal is not of type string or number');
2170
- })() :
2171
- TypeGuard.TInteger(key) || TypeGuard.TNumber(key) ? (() => {
2172
- return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [exports.PatternNumberExact]: TypeClone.Type(schema) } });
2173
- })() :
2174
- TypeGuard.TString(key) ? (() => {
2175
- const pattern = ValueGuard.IsUndefined(key.pattern) ? exports.PatternStringExact : key.pattern;
2176
- return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(schema) } });
2177
- })() :
2178
- this.Never());
2179
- }
2180
- /** `[Json]` Creates a Recursive type */
2181
- Recursive(callback, options = {}) {
2182
- if (ValueGuard.IsUndefined(options.$id))
2183
- options.$id = `T${TypeOrdinal++}`;
2184
- const thisType = callback({ [exports.Kind]: 'This', $ref: `${options.$id}` });
2185
- thisType.$id = options.$id;
2186
- return this.Create({ ...options, [exports.Hint]: 'Recursive', ...thisType });
2187
- }
2188
- /** `[Json]` Creates a Ref type. */
2189
- Ref(unresolved, options = {}) {
2190
- if (ValueGuard.IsString(unresolved))
2191
- return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved });
2192
- if (ValueGuard.IsUndefined(unresolved.$id))
2193
- this.Throw('Reference target type must specify an $id');
2194
- return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved.$id });
2195
- }
2196
- /** `[Json]` Constructs a type where all properties are required */
2197
- Required(schema, options = {}) {
2198
- // prettier-ignore
2199
- return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
2200
- const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
2201
- return { ...acc, [key]: this.Discard(object.properties[key], [exports.Optional]) };
2202
- }, {});
2203
- return this.Object(properties, object /* object used as options to retain other constraints */);
2204
- }, options);
2205
- }
2206
- /** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
2207
- Rest(schema) {
2208
- return (TypeGuard.TTuple(schema) && !ValueGuard.IsUndefined(schema.items) ? TypeClone.Rest(schema.items) : TypeGuard.TIntersect(schema) ? TypeClone.Rest(schema.allOf) : TypeGuard.TUnion(schema) ? TypeClone.Rest(schema.anyOf) : []);
2209
- }
2210
- /** `[Json]` Creates a String type */
2211
- String(options = {}) {
2212
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
2213
- }
2214
- /** `[Json]` Creates a TemplateLiteral type */
2215
- TemplateLiteral(unresolved, options = {}) {
2216
- // prettier-ignore
2217
- const pattern = ValueGuard.IsString(unresolved)
2218
- ? TemplateLiteralPattern.Create(TemplateLiteralDslParser.Parse(unresolved))
2219
- : TemplateLiteralPattern.Create(unresolved);
2220
- return this.Create({ ...options, [exports.Kind]: 'TemplateLiteral', type: 'string', pattern });
2221
- }
2222
- /** `[Json]` Creates a Transform type */
2223
- Transform(schema) {
2224
- return new TransformDecodeBuilder(schema);
2225
- }
2226
- /** `[Json]` Creates a Tuple type */
2227
- Tuple(items, options = {}) {
2228
- const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
2229
- const clonedItems = TypeClone.Rest(items);
2230
- // prettier-ignore
2231
- const schema = (items.length > 0 ?
2232
- { ...options, [exports.Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
2233
- { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
2234
- return this.Create(schema);
2235
- }
2236
- /** `[Json]` Intrinsic function to Uncapitalize LiteralString types */
2237
- Uncapitalize(schema, options = {}) {
2238
- return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uncapitalize'), ...options };
2239
- }
2240
- /** `[Json]` Creates a Union type */
2241
- Union(union, options = {}) {
2242
- // prettier-ignore
2243
- return TypeGuard.TTemplateLiteral(union)
2244
- ? TemplateLiteralResolver.Resolve(union)
2245
- : (() => {
2246
- const anyOf = union;
2247
- if (anyOf.length === 0)
2248
- return this.Never(options);
2249
- if (anyOf.length === 1)
2250
- return this.Create(TypeClone.Type(anyOf[0], options));
2251
- const clonedAnyOf = TypeClone.Rest(anyOf);
2252
- return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
2253
- })();
2254
- }
2255
- /** `[Json]` Creates an Unknown type */
2256
- Unknown(options = {}) {
2257
- return this.Create({ ...options, [exports.Kind]: 'Unknown' });
2258
- }
2259
- /** `[Json]` Creates a Unsafe type that will infers as the generic argument T */
2260
- Unsafe(options = {}) {
2261
- return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
2262
- }
2263
- /** `[Json]` Intrinsic function to Uppercase LiteralString types */
2264
- Uppercase(schema, options = {}) {
2265
- return { ...Intrinsic.Map(TypeClone.Type(schema), 'Uppercase'), ...options };
2266
- }
2267
- }
2268
- exports.JsonTypeBuilder = JsonTypeBuilder;
2269
- // --------------------------------------------------------------------------
2270
- // JavaScriptTypeBuilder
2271
- // --------------------------------------------------------------------------
2272
- class JavaScriptTypeBuilder extends JsonTypeBuilder {
2273
- /** `[JavaScript]` Creates a AsyncIterator type */
2274
- AsyncIterator(items, options = {}) {
2275
- return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Type(items) });
2276
- }
2277
- /** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
2278
- Awaited(schema, options = {}) {
2279
- // prettier-ignore
2280
- const Unwrap = (rest) => rest.length > 0 ? (() => {
2281
- const [L, ...R] = rest;
2282
- return [this.Awaited(L), ...Unwrap(R)];
2283
- })() : rest;
2284
- // prettier-ignore
2285
- return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(Unwrap(schema.allOf)) :
2286
- TypeGuard.TUnion(schema) ? exports.Type.Union(Unwrap(schema.anyOf)) :
2287
- TypeGuard.TPromise(schema) ? this.Awaited(schema.item) :
2288
- TypeClone.Type(schema, options));
2289
- }
2290
- /** `[JavaScript]` Creates a BigInt type */
2291
- BigInt(options = {}) {
2292
- return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
2293
- }
2294
- /** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
2295
- ConstructorParameters(schema, options = {}) {
2296
- return this.Tuple([...schema.parameters], { ...options });
2297
- }
2298
- /** `[JavaScript]` Creates a Constructor type */
2299
- Constructor(parameters, returns, options) {
2300
- const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
2301
- return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
2302
- }
2303
- /** `[JavaScript]` Creates a Date type */
2304
- Date(options = {}) {
2305
- return this.Create({ ...options, [exports.Kind]: 'Date', type: 'Date' });
2306
- }
2307
- /** `[JavaScript]` Creates a Function type */
2308
- Function(parameters, returns, options) {
2309
- const [clonedParameters, clonedReturns] = [TypeClone.Rest(parameters), TypeClone.Type(returns)];
2310
- return this.Create({ ...options, [exports.Kind]: 'Function', type: 'Function', parameters: clonedParameters, returns: clonedReturns });
2311
- }
2312
- /** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
2313
- InstanceType(schema, options = {}) {
2314
- return TypeClone.Type(schema.returns, options);
2315
- }
2316
- /** `[JavaScript]` Creates an Iterator type */
2317
- Iterator(items, options = {}) {
2318
- return this.Create({ ...options, [exports.Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Type(items) });
2319
- }
2320
- /** `[JavaScript]` Extracts the Parameters from the given Function type */
2321
- Parameters(schema, options = {}) {
2322
- return this.Tuple(schema.parameters, { ...options });
2323
- }
2324
- /** `[JavaScript]` Creates a Promise type */
2325
- Promise(item, options = {}) {
2326
- return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Type(item) });
2327
- }
2328
- /** `[Extended]` Creates a String type */
2329
- RegExp(unresolved, options = {}) {
2330
- const pattern = ValueGuard.IsString(unresolved) ? unresolved : unresolved.source;
2331
- return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern });
2332
- }
2333
- /**
2334
- * @deprecated Use `Type.RegExp`
2335
- */
2336
- RegEx(regex, options = {}) {
2337
- return this.RegExp(regex, options);
2338
- }
2339
- /** `[JavaScript]` Extracts the ReturnType from the given Function type */
2340
- ReturnType(schema, options = {}) {
2341
- return TypeClone.Type(schema.returns, options);
2342
- }
2343
- /** `[JavaScript]` Creates a Symbol type */
2344
- Symbol(options) {
2345
- return this.Create({ ...options, [exports.Kind]: 'Symbol', type: 'symbol' });
2346
- }
2347
- /** `[JavaScript]` Creates a Undefined type */
2348
- Undefined(options = {}) {
2349
- return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'undefined' });
2350
- }
2351
- /** `[JavaScript]` Creates a Uint8Array type */
2352
- Uint8Array(options = {}) {
2353
- return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'Uint8Array' });
2354
- }
2355
- /** `[JavaScript]` Creates a Void type */
2356
- Void(options = {}) {
2357
- return this.Create({ ...options, [exports.Kind]: 'Void', type: 'void' });
2358
- }
2359
- }
2360
- exports.JavaScriptTypeBuilder = JavaScriptTypeBuilder;
2361
- /** Json Type Builder with Static Resolution for TypeScript */
2362
- exports.JsonType = new JsonTypeBuilder();
2363
- /** JavaScript Type Builder with Static Resolution for TypeScript */
2364
- exports.Type = new JavaScriptTypeBuilder();