@versionzero/schema 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (401) hide show
  1. package/LICENSE +177 -0
  2. package/README.md +246 -0
  3. package/package.json +84 -0
  4. package/src/compilation/handler-compilation.js +28 -0
  5. package/src/compilation/metadata-compilation.js +35 -0
  6. package/src/compilation/schema-compilation.js +142 -0
  7. package/src/compilation/selection-compilation.js +84 -0
  8. package/src/compilation/union-compilation.js +510 -0
  9. package/src/compilation/values-compilation.js +35 -0
  10. package/src/compiled-schema.js +1709 -0
  11. package/src/constants.js +1 -0
  12. package/src/core-library/index.js +32 -0
  13. package/src/core-library/processors/aggregation-operators.js +75 -0
  14. package/src/core-library/processors/alpha-constraint.js +20 -0
  15. package/src/core-library/processors/alphanum-constraint.js +20 -0
  16. package/src/core-library/processors/array-operator.js +51 -0
  17. package/src/core-library/processors/assert-constraint.js +75 -0
  18. package/src/core-library/processors/base64-constraint.js +26 -0
  19. package/src/core-library/processors/camel-case-operator.js +24 -0
  20. package/src/core-library/processors/capitalize-operator.js +16 -0
  21. package/src/core-library/processors/cardnum-constraint.js +193 -0
  22. package/src/core-library/processors/ceil-operator.js +44 -0
  23. package/src/core-library/processors/collapse-operator.js +29 -0
  24. package/src/core-library/processors/compact-operator.js +34 -0
  25. package/src/core-library/processors/compile-operator.js +65 -0
  26. package/src/core-library/processors/concat-operator.js +51 -0
  27. package/src/core-library/processors/conditional-operators.js +301 -0
  28. package/src/core-library/processors/constant-case-operator.js +16 -0
  29. package/src/core-library/processors/data-size-operator.js +86 -0
  30. package/src/core-library/processors/date-object-operator.js +54 -0
  31. package/src/core-library/processors/date-operator.js +67 -0
  32. package/src/core-library/processors/date-range-constraint.js +76 -0
  33. package/src/core-library/processors/defined-constraint.js +30 -0
  34. package/src/core-library/processors/each-operator.js +57 -0
  35. package/src/core-library/processors/email-constraint.js +112 -0
  36. package/src/core-library/processors/entries-operator.js +25 -0
  37. package/src/core-library/processors/eq-constraint.js +37 -0
  38. package/src/core-library/processors/filter-operator.js +74 -0
  39. package/src/core-library/processors/find-schema-operator.js +45 -0
  40. package/src/core-library/processors/flatten-operator.js +40 -0
  41. package/src/core-library/processors/floor-operator.js +47 -0
  42. package/src/core-library/processors/get-operator.js +44 -0
  43. package/src/core-library/processors/group-by-operator.js +84 -0
  44. package/src/core-library/processors/has-prefix-constraint.js +37 -0
  45. package/src/core-library/processors/has-suffix-constraint.js +35 -0
  46. package/src/core-library/processors/hex-constraint.js +20 -0
  47. package/src/core-library/processors/hostname-constraint.js +22 -0
  48. package/src/core-library/processors/http-url-constraint.js +27 -0
  49. package/src/core-library/processors/in-constraint.js +66 -0
  50. package/src/core-library/processors/index-by-operator.js +98 -0
  51. package/src/core-library/processors/index.js +131 -0
  52. package/src/core-library/processors/input-operator.js +23 -0
  53. package/src/core-library/processors/instanceof-constraint.js +38 -0
  54. package/src/core-library/processors/integer-constraint.js +22 -0
  55. package/src/core-library/processors/invoke-operator.js +33 -0
  56. package/src/core-library/processors/ipv4-constraint.js +188 -0
  57. package/src/core-library/processors/ipv6-constraint.js +205 -0
  58. package/src/core-library/processors/is-array-constraint.js +21 -0
  59. package/src/core-library/processors/is-date-constraint.js +22 -0
  60. package/src/core-library/processors/is-number-constraint.js +21 -0
  61. package/src/core-library/processors/is-object-constraint.js +21 -0
  62. package/src/core-library/processors/is-string-constraint.js +21 -0
  63. package/src/core-library/processors/join-operator.js +41 -0
  64. package/src/core-library/processors/json-constraint.js +22 -0
  65. package/src/core-library/processors/json-decode-operator.js +25 -0
  66. package/src/core-library/processors/json-encode-operator.js +35 -0
  67. package/src/core-library/processors/kebab-case-operator.js +23 -0
  68. package/src/core-library/processors/keys-operator.js +20 -0
  69. package/src/core-library/processors/length-constraint.js +85 -0
  70. package/src/core-library/processors/lookup-operator.js +84 -0
  71. package/src/core-library/processors/lowercase-operator.js +14 -0
  72. package/src/core-library/processors/map-operator.js +84 -0
  73. package/src/core-library/processors/match-operator.js +64 -0
  74. package/src/core-library/processors/matches-constraint.js +54 -0
  75. package/src/core-library/processors/math-operators.js +151 -0
  76. package/src/core-library/processors/merge-deep-operator.js +61 -0
  77. package/src/core-library/processors/merge-operator.js +54 -0
  78. package/src/core-library/processors/metadata-operator.js +100 -0
  79. package/src/core-library/processors/negative-constraint.js +23 -0
  80. package/src/core-library/processors/never-constraint.js +69 -0
  81. package/src/core-library/processors/non-empty-constraint.js +59 -0
  82. package/src/core-library/processors/not-constraint.js +71 -0
  83. package/src/core-library/processors/number-operator.js +24 -0
  84. package/src/core-library/processors/numeric-constraint.js +22 -0
  85. package/src/core-library/processors/object-operator.js +38 -0
  86. package/src/core-library/processors/omit-operator.js +57 -0
  87. package/src/core-library/processors/parallel-operator.js +64 -0
  88. package/src/core-library/processors/pascal-case-operator.js +16 -0
  89. package/src/core-library/processors/phone-constraint.js +235 -0
  90. package/src/core-library/processors/pick-operator.js +62 -0
  91. package/src/core-library/processors/pipeline-operator.js +63 -0
  92. package/src/core-library/processors/port-constraint.js +22 -0
  93. package/src/core-library/processors/positive-constraint.js +23 -0
  94. package/src/core-library/processors/process-operator.js +55 -0
  95. package/src/core-library/processors/property-operator.js +49 -0
  96. package/src/core-library/processors/range-constraint.js +72 -0
  97. package/src/core-library/processors/reference-operator.js +79 -0
  98. package/src/core-library/processors/require-constraint.js +74 -0
  99. package/src/core-library/processors/reverse-operator.js +20 -0
  100. package/src/core-library/processors/round-operator.js +53 -0
  101. package/src/core-library/processors/schema-handler-operators.js +54 -0
  102. package/src/core-library/processors/semver-constraint.js +282 -0
  103. package/src/core-library/processors/sequence-processors.js +406 -0
  104. package/src/core-library/processors/sort-operator.js +52 -0
  105. package/src/core-library/processors/split-operator.js +43 -0
  106. package/src/core-library/processors/string-extra-operators.js +141 -0
  107. package/src/core-library/processors/string-operator.js +34 -0
  108. package/src/core-library/processors/target-operator.js +30 -0
  109. package/src/core-library/processors/template-operator.js +60 -0
  110. package/src/core-library/processors/title-case-operator.js +17 -0
  111. package/src/core-library/processors/trim-operator.js +14 -0
  112. package/src/core-library/processors/truthy-constraint.js +35 -0
  113. package/src/core-library/processors/type-operator.js +24 -0
  114. package/src/core-library/processors/unique-operator.js +21 -0
  115. package/src/core-library/processors/uppercase-operator.js +14 -0
  116. package/src/core-library/processors/url-constraint.js +31 -0
  117. package/src/core-library/processors/url-decode-operator.js +50 -0
  118. package/src/core-library/processors/url-encode-operator.js +44 -0
  119. package/src/core-library/processors/uuid-constraint.js +31 -0
  120. package/src/core-library/processors/values-operator.js +20 -0
  121. package/src/core-library/schemas/any-schema.js +23 -0
  122. package/src/core-library/schemas/array-schema.js +8 -0
  123. package/src/core-library/schemas/boolean-schema.js +10 -0
  124. package/src/core-library/schemas/date-schema.js +12 -0
  125. package/src/core-library/schemas/function-schema.js +40 -0
  126. package/src/core-library/schemas/number-schema.js +9 -0
  127. package/src/core-library/schemas/object-schema.js +10 -0
  128. package/src/core-library/schemas/root-schema.js +21 -0
  129. package/src/core-library/schemas/string-schema.js +9 -0
  130. package/src/core-library-node/index.js +47 -0
  131. package/src/core-library-node/processors/base64-decode-operator.js +20 -0
  132. package/src/core-library-node/processors/base64-encode-operator.js +20 -0
  133. package/src/core-library-node/processors/buffer-operator.js +39 -0
  134. package/src/core-library-node/processors/directory-constraint.js +35 -0
  135. package/src/core-library-node/processors/executable-constraint.js +34 -0
  136. package/src/core-library-node/processors/file-constraint.js +34 -0
  137. package/src/core-library-node/processors/file-size-constraint.js +94 -0
  138. package/src/core-library-node/processors/is-buffer-constraint.js +21 -0
  139. package/src/core-library-node/processors/reachable-constraint.js +28 -0
  140. package/src/core-library-node/processors/readable-constraint.js +34 -0
  141. package/src/core-library-node/processors/writable-constraint.js +59 -0
  142. package/src/core-library-node/schemas/buffer-schema.js +10 -0
  143. package/src/errors.js +209 -0
  144. package/src/executor/array-executor.js +78 -0
  145. package/src/executor/conditional-executor.js +134 -0
  146. package/src/executor/each-executor.js +68 -0
  147. package/src/executor/executor.js +123 -0
  148. package/src/executor/object-executor.js +98 -0
  149. package/src/executor/parallel-executor.js +43 -0
  150. package/src/executor/pipeline-executor.js +65 -0
  151. package/src/executor/sequence-executor.js +206 -0
  152. package/src/executor/serial-executor.js +24 -0
  153. package/src/executor/step-executor.js +68 -0
  154. package/src/helpers/case.js +124 -0
  155. package/src/helpers/data-size.js +144 -0
  156. package/src/helpers/debug-sink.js +15 -0
  157. package/src/helpers/deep.js +280 -0
  158. package/src/helpers/format.js +121 -0
  159. package/src/helpers/has-string-properties.js +30 -0
  160. package/src/helpers/index.js +16 -0
  161. package/src/helpers/object.js +115 -0
  162. package/src/helpers/parse-date.js +75 -0
  163. package/src/helpers/path.js +28 -0
  164. package/src/helpers/regex.js +18 -0
  165. package/src/helpers/stringify.js +309 -0
  166. package/src/helpers/to-data.js +64 -0
  167. package/src/helpers/truthy.js +55 -0
  168. package/src/index.js +29 -0
  169. package/src/schema-compiler.js +531 -0
  170. package/src/schema-location.js +200 -0
  171. package/src/schema-resolver.js +546 -0
  172. package/src/schema.js +1182 -0
  173. package/src/traversal/executors/check-condition.js +42 -0
  174. package/src/traversal/executors/check-input.js +27 -0
  175. package/src/traversal/executors/check-required.js +19 -0
  176. package/src/traversal/executors/check-schema.js +45 -0
  177. package/src/traversal/executors/defaults.js +21 -0
  178. package/src/traversal/executors/enter-existing.js +25 -0
  179. package/src/traversal/executors/enter-input.js +25 -0
  180. package/src/traversal/executors/enter.js +37 -0
  181. package/src/traversal/executors/exit.js +74 -0
  182. package/src/traversal/executors/finalize.js +64 -0
  183. package/src/traversal/executors/index.js +42 -0
  184. package/src/traversal/executors/normalize.js +38 -0
  185. package/src/traversal/executors/prepare-existing.js +27 -0
  186. package/src/traversal/executors/prepare-pending.js +54 -0
  187. package/src/traversal/executors/resolve-union.js +50 -0
  188. package/src/traversal/executors/serialize.js +48 -0
  189. package/src/traversal/executors/transform-early.js +51 -0
  190. package/src/traversal/executors/transform.js +68 -0
  191. package/src/traversal/executors/traversal-state-executor.js +46 -0
  192. package/src/traversal/executors/validate.js +63 -0
  193. package/src/traversal/traversal-context.js +231 -0
  194. package/src/traversal/traversal-state.js +809 -0
  195. package/src/types.js +102 -0
  196. package/src/value-processor/composed-value-processor.js +43 -0
  197. package/src/value-processor/defined-value-processor.js +72 -0
  198. package/src/value-processor/function-value-processor.js +68 -0
  199. package/src/value-processor/parameterized-value-processor.js +45 -0
  200. package/src/value-processor/parameters-value-processor.js +178 -0
  201. package/src/value-processor/spec.js +89 -0
  202. package/src/value-processor/value-processor.js +105 -0
  203. package/types/compilation/handler-compilation.d.ts +13 -0
  204. package/types/compilation/metadata-compilation.d.ts +6 -0
  205. package/types/compilation/schema-compilation.d.ts +32 -0
  206. package/types/compilation/selection-compilation.d.ts +9 -0
  207. package/types/compilation/union-compilation.d.ts +42 -0
  208. package/types/compilation/values-compilation.d.ts +12 -0
  209. package/types/compiled-schema.d.ts +883 -0
  210. package/types/constants.d.ts +1 -0
  211. package/types/core-library/index.d.ts +7 -0
  212. package/types/core-library/processors/aggregation-operators.d.ts +24 -0
  213. package/types/core-library/processors/alpha-constraint.d.ts +9 -0
  214. package/types/core-library/processors/alphanum-constraint.d.ts +9 -0
  215. package/types/core-library/processors/array-operator.d.ts +12 -0
  216. package/types/core-library/processors/assert-constraint.d.ts +30 -0
  217. package/types/core-library/processors/base64-constraint.d.ts +11 -0
  218. package/types/core-library/processors/camel-case-operator.d.ts +17 -0
  219. package/types/core-library/processors/capitalize-operator.d.ts +11 -0
  220. package/types/core-library/processors/cardnum-constraint.d.ts +51 -0
  221. package/types/core-library/processors/ceil-operator.d.ts +30 -0
  222. package/types/core-library/processors/collapse-operator.d.ts +24 -0
  223. package/types/core-library/processors/compact-operator.d.ts +29 -0
  224. package/types/core-library/processors/compile-operator.d.ts +34 -0
  225. package/types/core-library/processors/concat-operator.d.ts +23 -0
  226. package/types/core-library/processors/conditional-operators.d.ts +219 -0
  227. package/types/core-library/processors/constant-case-operator.d.ts +9 -0
  228. package/types/core-library/processors/data-size-operator.d.ts +31 -0
  229. package/types/core-library/processors/date-object-operator.d.ts +16 -0
  230. package/types/core-library/processors/date-operator.d.ts +21 -0
  231. package/types/core-library/processors/date-range-constraint.d.ts +26 -0
  232. package/types/core-library/processors/defined-constraint.d.ts +20 -0
  233. package/types/core-library/processors/each-operator.d.ts +34 -0
  234. package/types/core-library/processors/email-constraint.d.ts +54 -0
  235. package/types/core-library/processors/entries-operator.d.ts +13 -0
  236. package/types/core-library/processors/eq-constraint.d.ts +20 -0
  237. package/types/core-library/processors/filter-operator.d.ts +35 -0
  238. package/types/core-library/processors/find-schema-operator.d.ts +28 -0
  239. package/types/core-library/processors/flatten-operator.d.ts +26 -0
  240. package/types/core-library/processors/floor-operator.d.ts +33 -0
  241. package/types/core-library/processors/get-operator.d.ts +31 -0
  242. package/types/core-library/processors/group-by-operator.d.ts +36 -0
  243. package/types/core-library/processors/has-prefix-constraint.d.ts +22 -0
  244. package/types/core-library/processors/has-suffix-constraint.d.ts +20 -0
  245. package/types/core-library/processors/hex-constraint.d.ts +9 -0
  246. package/types/core-library/processors/hostname-constraint.d.ts +11 -0
  247. package/types/core-library/processors/http-url-constraint.d.ts +9 -0
  248. package/types/core-library/processors/in-constraint.d.ts +27 -0
  249. package/types/core-library/processors/index-by-operator.d.ts +26 -0
  250. package/types/core-library/processors/index.d.ts +8 -0
  251. package/types/core-library/processors/input-operator.d.ts +20 -0
  252. package/types/core-library/processors/instanceof-constraint.d.ts +23 -0
  253. package/types/core-library/processors/integer-constraint.d.ts +9 -0
  254. package/types/core-library/processors/invoke-operator.d.ts +12 -0
  255. package/types/core-library/processors/ipv4-constraint.d.ts +37 -0
  256. package/types/core-library/processors/ipv6-constraint.d.ts +34 -0
  257. package/types/core-library/processors/is-array-constraint.d.ts +10 -0
  258. package/types/core-library/processors/is-date-constraint.d.ts +10 -0
  259. package/types/core-library/processors/is-number-constraint.d.ts +10 -0
  260. package/types/core-library/processors/is-object-constraint.d.ts +10 -0
  261. package/types/core-library/processors/is-string-constraint.d.ts +10 -0
  262. package/types/core-library/processors/join-operator.d.ts +29 -0
  263. package/types/core-library/processors/json-constraint.d.ts +10 -0
  264. package/types/core-library/processors/json-decode-operator.d.ts +9 -0
  265. package/types/core-library/processors/json-encode-operator.d.ts +27 -0
  266. package/types/core-library/processors/kebab-case-operator.d.ts +16 -0
  267. package/types/core-library/processors/keys-operator.d.ts +9 -0
  268. package/types/core-library/processors/length-constraint.d.ts +34 -0
  269. package/types/core-library/processors/lookup-operator.d.ts +36 -0
  270. package/types/core-library/processors/lowercase-operator.d.ts +9 -0
  271. package/types/core-library/processors/map-operator.d.ts +38 -0
  272. package/types/core-library/processors/match-operator.d.ts +34 -0
  273. package/types/core-library/processors/matches-constraint.d.ts +29 -0
  274. package/types/core-library/processors/math-operators.d.ts +91 -0
  275. package/types/core-library/processors/merge-deep-operator.d.ts +32 -0
  276. package/types/core-library/processors/merge-operator.d.ts +26 -0
  277. package/types/core-library/processors/metadata-operator.d.ts +56 -0
  278. package/types/core-library/processors/negative-constraint.d.ts +13 -0
  279. package/types/core-library/processors/never-constraint.d.ts +26 -0
  280. package/types/core-library/processors/non-empty-constraint.d.ts +28 -0
  281. package/types/core-library/processors/not-constraint.d.ts +28 -0
  282. package/types/core-library/processors/number-operator.d.ts +9 -0
  283. package/types/core-library/processors/numeric-constraint.d.ts +10 -0
  284. package/types/core-library/processors/object-operator.d.ts +10 -0
  285. package/types/core-library/processors/omit-operator.d.ts +24 -0
  286. package/types/core-library/processors/parallel-operator.d.ts +41 -0
  287. package/types/core-library/processors/pascal-case-operator.d.ts +9 -0
  288. package/types/core-library/processors/phone-constraint.d.ts +65 -0
  289. package/types/core-library/processors/pick-operator.d.ts +27 -0
  290. package/types/core-library/processors/pipeline-operator.d.ts +40 -0
  291. package/types/core-library/processors/port-constraint.d.ts +11 -0
  292. package/types/core-library/processors/positive-constraint.d.ts +13 -0
  293. package/types/core-library/processors/process-operator.d.ts +37 -0
  294. package/types/core-library/processors/property-operator.d.ts +34 -0
  295. package/types/core-library/processors/range-constraint.d.ts +30 -0
  296. package/types/core-library/processors/reference-operator.d.ts +38 -0
  297. package/types/core-library/processors/require-constraint.d.ts +29 -0
  298. package/types/core-library/processors/reverse-operator.d.ts +9 -0
  299. package/types/core-library/processors/round-operator.d.ts +34 -0
  300. package/types/core-library/processors/schema-handler-operators.d.ts +28 -0
  301. package/types/core-library/processors/semver-constraint.d.ts +43 -0
  302. package/types/core-library/processors/sequence-processors.d.ts +213 -0
  303. package/types/core-library/processors/sort-operator.d.ts +31 -0
  304. package/types/core-library/processors/split-operator.d.ts +33 -0
  305. package/types/core-library/processors/string-extra-operators.d.ts +83 -0
  306. package/types/core-library/processors/string-operator.d.ts +10 -0
  307. package/types/core-library/processors/target-operator.d.ts +27 -0
  308. package/types/core-library/processors/template-operator.d.ts +31 -0
  309. package/types/core-library/processors/title-case-operator.d.ts +12 -0
  310. package/types/core-library/processors/trim-operator.d.ts +9 -0
  311. package/types/core-library/processors/truthy-constraint.d.ts +23 -0
  312. package/types/core-library/processors/type-operator.d.ts +11 -0
  313. package/types/core-library/processors/unique-operator.d.ts +10 -0
  314. package/types/core-library/processors/uppercase-operator.d.ts +9 -0
  315. package/types/core-library/processors/url-constraint.d.ts +20 -0
  316. package/types/core-library/processors/url-decode-operator.d.ts +31 -0
  317. package/types/core-library/processors/url-encode-operator.d.ts +36 -0
  318. package/types/core-library/processors/uuid-constraint.d.ts +20 -0
  319. package/types/core-library/processors/values-operator.d.ts +9 -0
  320. package/types/core-library/schemas/any-schema.d.ts +2 -0
  321. package/types/core-library/schemas/array-schema.d.ts +2 -0
  322. package/types/core-library/schemas/boolean-schema.d.ts +2 -0
  323. package/types/core-library/schemas/date-schema.d.ts +2 -0
  324. package/types/core-library/schemas/function-schema.d.ts +2 -0
  325. package/types/core-library/schemas/number-schema.d.ts +2 -0
  326. package/types/core-library/schemas/object-schema.d.ts +2 -0
  327. package/types/core-library/schemas/root-schema.d.ts +2 -0
  328. package/types/core-library/schemas/string-schema.d.ts +2 -0
  329. package/types/core-library-node/index.d.ts +12 -0
  330. package/types/core-library-node/processors/base64-decode-operator.d.ts +9 -0
  331. package/types/core-library-node/processors/base64-encode-operator.d.ts +9 -0
  332. package/types/core-library-node/processors/buffer-operator.d.ts +15 -0
  333. package/types/core-library-node/processors/directory-constraint.d.ts +14 -0
  334. package/types/core-library-node/processors/executable-constraint.d.ts +17 -0
  335. package/types/core-library-node/processors/file-constraint.d.ts +13 -0
  336. package/types/core-library-node/processors/file-size-constraint.d.ts +43 -0
  337. package/types/core-library-node/processors/is-buffer-constraint.d.ts +10 -0
  338. package/types/core-library-node/processors/reachable-constraint.d.ts +13 -0
  339. package/types/core-library-node/processors/readable-constraint.d.ts +17 -0
  340. package/types/core-library-node/processors/writable-constraint.d.ts +18 -0
  341. package/types/core-library-node/schemas/buffer-schema.d.ts +2 -0
  342. package/types/errors.d.ts +58 -0
  343. package/types/executor/array-executor.d.ts +17 -0
  344. package/types/executor/conditional-executor.d.ts +45 -0
  345. package/types/executor/each-executor.d.ts +15 -0
  346. package/types/executor/executor.d.ts +84 -0
  347. package/types/executor/object-executor.d.ts +14 -0
  348. package/types/executor/parallel-executor.d.ts +27 -0
  349. package/types/executor/pipeline-executor.d.ts +11 -0
  350. package/types/executor/sequence-executor.d.ts +32 -0
  351. package/types/executor/serial-executor.d.ts +16 -0
  352. package/types/executor/step-executor.d.ts +14 -0
  353. package/types/helpers/case.d.ts +30 -0
  354. package/types/helpers/data-size.d.ts +25 -0
  355. package/types/helpers/debug-sink.d.ts +9 -0
  356. package/types/helpers/deep.d.ts +33 -0
  357. package/types/helpers/format.d.ts +14 -0
  358. package/types/helpers/has-string-properties.d.ts +5 -0
  359. package/types/helpers/index.d.ts +13 -0
  360. package/types/helpers/object.d.ts +46 -0
  361. package/types/helpers/parse-date.d.ts +6 -0
  362. package/types/helpers/path.d.ts +13 -0
  363. package/types/helpers/regex.d.ts +7 -0
  364. package/types/helpers/stringify.d.ts +33 -0
  365. package/types/helpers/to-data.d.ts +13 -0
  366. package/types/helpers/truthy.d.ts +26 -0
  367. package/types/index.d.ts +6 -0
  368. package/types/schema-compiler.d.ts +49 -0
  369. package/types/schema-location.d.ts +64 -0
  370. package/types/schema-resolver.d.ts +145 -0
  371. package/types/schema.d.ts +586 -0
  372. package/types/traversal/executors/check-condition.d.ts +8 -0
  373. package/types/traversal/executors/check-input.d.ts +6 -0
  374. package/types/traversal/executors/check-required.d.ts +6 -0
  375. package/types/traversal/executors/check-schema.d.ts +7 -0
  376. package/types/traversal/executors/defaults.d.ts +8 -0
  377. package/types/traversal/executors/enter-existing.d.ts +6 -0
  378. package/types/traversal/executors/enter-input.d.ts +8 -0
  379. package/types/traversal/executors/enter.d.ts +7 -0
  380. package/types/traversal/executors/exit.d.ts +6 -0
  381. package/types/traversal/executors/finalize.d.ts +6 -0
  382. package/types/traversal/executors/index.d.ts +15 -0
  383. package/types/traversal/executors/normalize.d.ts +7 -0
  384. package/types/traversal/executors/prepare-existing.d.ts +6 -0
  385. package/types/traversal/executors/prepare-pending.d.ts +6 -0
  386. package/types/traversal/executors/resolve-union.d.ts +6 -0
  387. package/types/traversal/executors/serialize.d.ts +11 -0
  388. package/types/traversal/executors/transform-early.d.ts +6 -0
  389. package/types/traversal/executors/transform.d.ts +6 -0
  390. package/types/traversal/executors/traversal-state-executor.d.ts +19 -0
  391. package/types/traversal/executors/validate.d.ts +6 -0
  392. package/types/traversal/traversal-context.d.ts +67 -0
  393. package/types/traversal/traversal-state.d.ts +97 -0
  394. package/types/types.d.ts +218 -0
  395. package/types/value-processor/composed-value-processor.d.ts +17 -0
  396. package/types/value-processor/defined-value-processor.d.ts +16 -0
  397. package/types/value-processor/function-value-processor.d.ts +15 -0
  398. package/types/value-processor/parameterized-value-processor.d.ts +14 -0
  399. package/types/value-processor/parameters-value-processor.d.ts +28 -0
  400. package/types/value-processor/spec.d.ts +22 -0
  401. package/types/value-processor/value-processor.d.ts +92 -0
@@ -0,0 +1,65 @@
1
+ import { ParametersValueProcessor } from '../../value-processor/parameters-value-processor.js';
2
+ import { ParameterizedValueProcessor } from '../../value-processor/parameterized-value-processor.js';
3
+ import { FunctionValueProcessor } from '../../value-processor/function-value-processor.js';
4
+ import { SchemaError } from '../../errors.js';
5
+ import { formatValue } from '../../helpers/format.js';
6
+
7
+ /**
8
+ * ## $compile
9
+ *
10
+ * Compiles a `Schema` (or `CompiledSchema`) into a `CompiledSchema` ready for use
11
+ * in value processing. If a schema argument is provided, that schema is compiled and
12
+ * the input value is ignored. If no argument is given, the input value itself is treated
13
+ * as the schema to compile — useful when a schema is being passed through as data.
14
+ *
15
+ * Typically used together with `$process` to dynamically select and apply a schema.
16
+ *
17
+ * ### Parameters
18
+ * - `schema` (Schema|CompiledSchema, optional): The schema to compile. If omitted, the input
19
+ * value is compiled instead.
20
+ *
21
+ * ### Example
22
+ * ```js
23
+ * import { Schema } from '@versionzero/schema';
24
+ *
25
+ * // Compile a fixed schema and feed it into $process
26
+ * const portSchema = new Schema('number').validator({$range: {min: 1, max: 65535}});
27
+ *
28
+ * new Schema('object', {
29
+ * port: new Schema('any')
30
+ * .normalizer({$compile: portSchema})
31
+ * .transformer('$process'),
32
+ * })
33
+ *
34
+ * // Compile the input value itself when schemas arrive as data
35
+ * new Schema('any').transformer(['$compile', '$process'])
36
+ * ```
37
+ *
38
+ * @type {import('../../value-processor/value-processor.js').ValueProcessorDefinition}
39
+ */
40
+ export const COMPILE_OPERATOR = {
41
+ keyword: 'compile',
42
+ parameters: [{parameter: 'schema', required: false}, {parameter: 'compiler', required: false}],
43
+ build: (args, options) => {
44
+
45
+ const compiler = options.compiler;
46
+
47
+ const paramsProcessor = new ParametersValueProcessor(COMPILE_OPERATOR.parameters, args);
48
+
49
+ return new ParameterizedValueProcessor(
50
+ new FunctionValueProcessor((schema, _target, location, options) => {
51
+ const c = options.args.compiler ?? compiler;
52
+ if (typeof c?.compile !== 'function') {
53
+ throw new SchemaError(`Invalid compiler provided to $compile ${formatValue(c)}`, {location})
54
+ }
55
+ if (options.args.schema !== undefined) {
56
+ schema = options.args.schema;
57
+ }
58
+ return c.compile(schema)
59
+ }),
60
+ paramsProcessor,
61
+ {$compile: paramsProcessor.spec}
62
+ );
63
+ }
64
+ };
65
+
@@ -0,0 +1,51 @@
1
+
2
+ import { ComposedValueProcessor } from '../../value-processor/composed-value-processor.js';
3
+ import { FunctionValueProcessor } from '../../value-processor/function-value-processor.js';
4
+ import { ArrayExecutor } from '../../executor/array-executor.js';
5
+ import { map } from '../../helpers/object.js';
6
+ import { ConstraintError, SchemaError } from '../../errors.js';
7
+
8
+ /**
9
+ * ## $concat
10
+ *
11
+ * Returns a new array with the specified values appended to the input array.
12
+ * Throws `SchemaError` at compile time if no arguments are provided.
13
+ * Throws `ConstraintError` at runtime if the input is not an array.
14
+ *
15
+ * ### Parameters
16
+ * - Array of values to append (required, at least one).
17
+ *
18
+ * ### Example
19
+ * ```js
20
+ * // Append default scopes to a user-supplied scopes array
21
+ * new Schema('array').transformer({$concat: ['read:profile', 'read:email']})
22
+ * // ['custom:scope'] → ['custom:scope', 'read:profile', 'read:email']
23
+ *
24
+ * // Always include a sentinel value at the end of an array
25
+ * new Schema('array').transformer({$concat: [null]})
26
+ * ```
27
+ *
28
+ * @type {import('../../value-processor/value-processor.js').ValueProcessorDefinition}
29
+ */
30
+ export const CONCAT_OPERATOR = {
31
+ keyword: 'concat',
32
+
33
+ build: (args) => {
34
+ if (!Array.isArray(args) || args.length === 0) {
35
+ throw new SchemaError('$concat requires a non-empty array of values to append');
36
+ }
37
+
38
+ const argsSpec = map(args, v => v.spec);
39
+
40
+ return new FunctionValueProcessor(
41
+ (value, _target, location, options) => {
42
+ const items = Array.isArray(options.args) ? options.args : [];
43
+ if (!Array.isArray(value)) {
44
+ throw new ConstraintError(`$concat requires an array input, got ${value}`, {location});
45
+ }
46
+ return [...value, ...items];
47
+ },
48
+ new ComposedValueProcessor(new ArrayExecutor(args), argsSpec)
49
+ );
50
+ }
51
+ };
@@ -0,0 +1,301 @@
1
+ import { Executor, UNDEFINED_EXECUTOR } from "../../executor/executor.js";
2
+ import { ConditionalExecutor } from '../../executor/conditional-executor.js';
3
+ import { ValueProcessor } from '../../value-processor/value-processor.js';
4
+ import { ComposedValueProcessor } from '../../value-processor/composed-value-processor.js';
5
+ import { ResolverError } from '../../errors.js';
6
+
7
+ /**
8
+ * @import {ValueProcessorDefinition} from '../../value-processor/value-processor.js'
9
+ */
10
+
11
+ /**
12
+ * @param {string} keyword
13
+ * @param {symbol[]} [flags]
14
+ * @returns {function(any): ValueProcessor}
15
+ * @package
16
+ */
17
+ function generateBuilderFunction(keyword, flags) {
18
+
19
+ return (args) => {
20
+
21
+ /** @type {ValueProcessor} */
22
+ let predicate;
23
+ /** @type {ValueProcessor} */
24
+ let success;
25
+ /** @type {ValueProcessor} */
26
+ let failure;
27
+
28
+ if (Array.isArray(args)) {
29
+ if (args.length > 3) {
30
+ throw new ResolverError(`${keyword} array requires 0-3 processors [predicate, success, failure]`);
31
+ }
32
+ predicate = args[0];
33
+ success = args[1];
34
+ failure = args[2];
35
+ }
36
+ else if (typeof args === 'object' && args !== null) {
37
+ predicate = args.predicate ?? args.condition ?? args.cond;
38
+ success = args.success;
39
+ failure = args.failure;
40
+ }
41
+ else if (args !== undefined) {
42
+ throw new ResolverError(`${keyword} requires predicate/success/failure processors specified as an array or object`);
43
+ }
44
+
45
+ predicate ??= new ComposedValueProcessor(new Executor(), []);
46
+ success ??= new ComposedValueProcessor(new Executor(), []);
47
+ failure ??= new ComposedValueProcessor(UNDEFINED_EXECUTOR, '$undefined');
48
+
49
+ const spec = {[`${keyword}`]: [predicate.spec, success?.spec, failure?.spec]}
50
+
51
+ let description = `(${predicate.description ?? ''})?`;
52
+ if (success.description) {
53
+ description += `(${success.description})`;
54
+ }
55
+ if (failure.description) {
56
+ description += `:(${failure.description})`
57
+ }
58
+
59
+ return new ComposedValueProcessor(
60
+ new ConditionalExecutor(predicate, {success, failure}, flags),
61
+ spec, description
62
+ );
63
+ }
64
+ }
65
+
66
+ /**
67
+ * ## $if
68
+ *
69
+ * This is a conditional operator that evaluates a predicate for truthiness.
70
+ *
71
+ * If the predicate returns a truthy value, $if will invoke any provided success action
72
+ * with the original input. If not provided, the default success action will simply return the original input.
73
+ *
74
+ * If the predicate returns a falsey value or rejects/throws, $if will return the result of invoking the failure
75
+ * action on the original input. The default failure action will return undefined.
76
+ *
77
+ * This processor can act as a constraint if the success or failure actions throw when triggered.
78
+ *
79
+ * To pass the predicate results to the actions instead of the input, see $check.
80
+ * To test for success rather than truthiness, see $gate.
81
+ *
82
+ * ### Parameters
83
+ * - `predicate` (processor, optional): Evaluated for truthiness. If omitted, the input itself is used as the predicate.
84
+ * - `success` (processor, optional): Invoked with the original input on truthy predicate result. Default: returns input as-is.
85
+ * - `failure` (processor, optional): Invoked with the original input on falsey predicate result or throw. Default: returns undefined.
86
+ *
87
+ * Array form: `{$if: [predicate, success, failure]}`
88
+ * Object form: `{$if: {predicate: ..., success: ..., failure: ...}}`
89
+ * Aliases: `predicate` may also be written as `condition` or `cond`.
90
+ *
91
+ * ### Example
92
+ * ```js
93
+ * // Normalize an optional port — keep it if numeric, otherwise drop it
94
+ * new Schema('object', {
95
+ * port: new Schema('number').normalizer({
96
+ * $if: ['$numeric', '$number', null]
97
+ * }),
98
+ * })
99
+ *
100
+ * // Conditionally require an API key when a flag is set
101
+ * new Schema('object', {
102
+ * useApiKey: new Schema('boolean'),
103
+ * apiKey: new Schema('string').validator({
104
+ * $if: [
105
+ * {$reference: 'useApiKey'},
106
+ * '$non-empty',
107
+ * ]
108
+ * }),
109
+ * })
110
+ * ```
111
+ *
112
+ * @type {ValueProcessorDefinition}
113
+ */
114
+ export const IF_OPERATOR = {
115
+ keyword: 'if',
116
+ build: generateBuilderFunction('$if', [ConditionalExecutor.CHECK_TRUTHY])
117
+ };
118
+ /**
119
+ * ## $gate
120
+ *
121
+ * This is a conditional operator that evaluates a predicate for whether it returns a defined value.
122
+ *
123
+ * If the predicate returns a defined value, $gate will return the result of invoking any provided success action
124
+ * with the original input. If not provided, the default success action will simply return the original input.
125
+ *
126
+ * If the predicate returns undefined or rejects/throws, $gate will return the result of invoking the failure
127
+ * action on the original input. The default failure action will return undefined.
128
+ *
129
+ * This processor can act as a constraint if the success or failure actions throw when triggered.
130
+ *
131
+ * To pass the original input to the actions instead of the predicate results, see $if.
132
+ *
133
+ * ### Parameters
134
+ * - `predicate` (processor, optional): Evaluated for defined-ness. If omitted, the input itself is tested.
135
+ * - `success` (processor, optional): Invoked with the original input when predicate returns a defined value. Default: returns input as-is.
136
+ * - `failure` (processor, optional): Invoked with the original input when predicate returns undefined or throws. Default: returns undefined.
137
+ *
138
+ * Array form: `{$gate: [predicate, success, failure]}`
139
+ * Object form: `{$gate: {predicate: ..., success: ..., failure: ...}}`
140
+ *
141
+ * ### Example
142
+ * ```js
143
+ * // Only process the value through $uppercase if it was provided
144
+ * new Schema('string').normalizer({
145
+ * $gate: ['$defined', '$uppercase']
146
+ * })
147
+ *
148
+ * // Use the first resolvable property as a value
149
+ * new Schema('object', {
150
+ * host: new Schema('string'),
151
+ * fallbackHost: new Schema('string'),
152
+ * }).transformer({
153
+ * $gate: [{$property: 'host'}, {$property: 'host'}, {$property: 'fallbackHost'}]
154
+ * })
155
+ * ```
156
+ *
157
+ * @type {ValueProcessorDefinition}
158
+ */
159
+ export const GATE_OPERATOR = {
160
+ keyword: 'gate',
161
+ build: generateBuilderFunction('$gate',[ConditionalExecutor.CHECK_DEFINED])
162
+ };
163
+ /**
164
+ * ## $check
165
+ *
166
+ * This is a conditional operator that evaluates a predicate for truthiness.
167
+ *
168
+ * If the predicate returns a truthy value, $check will return the result of invoking any provided success action
169
+ * with the predicate result. If not provided, the default success action will simply return the predicate result.
170
+ *
171
+ * If the predicate returns a falsey value or rejects/throws, $check will return the result of invoking the failure
172
+ * action on the predicate result. The default failure action will return undefined. Since the predicate result
173
+ * is already falsey, the failure action is best used to either return a default value or throw an error.
174
+ *
175
+ * This processor can act as a constraint if the success or failure actions throw when triggered.
176
+ *
177
+ * ### Parameters
178
+ * - `predicate` (processor, optional): Evaluated for truthiness. The predicate result (not the original input) is passed to success/failure.
179
+ * - `success` (processor, optional): Invoked with the predicate result on truthy outcome. Default: returns predicate result.
180
+ * - `failure` (processor, optional): Invoked with the predicate result on falsey outcome or throw. Default: returns undefined.
181
+ *
182
+ * Array form: `{$check: [predicate, success, failure]}`
183
+ * Object form: `{$check: {predicate: ..., success: ..., failure: ...}}`
184
+ *
185
+ * ### Example
186
+ * ```js
187
+ * // Extract and validate an environment key, returning the matched value or undefined
188
+ * new Schema('string').transformer({
189
+ * $check: [
190
+ * {$match: /^(production|staging|development)$/},
191
+ * ]
192
+ * })
193
+ *
194
+ * // Validate a regex match and throw on failure
195
+ * new Schema('string').validator({
196
+ * $check: [
197
+ * {$match: /^\d{4}-\d{2}-\d{2}$/},
198
+ * null,
199
+ * () => { throw new Error('Expected YYYY-MM-DD date format') }
200
+ * ]
201
+ * })
202
+ * ```
203
+ *
204
+ * @type {ValueProcessorDefinition}
205
+ */
206
+ export const CHECK_OPERATOR = {
207
+ keyword: 'check',
208
+ build: generateBuilderFunction('$check',[ConditionalExecutor.CHECK_TRUTHY, ConditionalExecutor.PASS_RESULT])
209
+ };
210
+ /**
211
+ * ## $when
212
+ *
213
+ * This is a conditional operator that evaluates a predicate for whether it returns a defined value.
214
+ *
215
+ * If the predicate returns a defined value, $when will return the result of invoking any provided success action
216
+ * with the predicate result. If not provided, the default success action will simply return the predicate result.
217
+ *
218
+ * If the predicate returns undefined or rejects/throws, $when will return the result of invoking the failure
219
+ * action on the predicate result. The default failure action will return undefined. Since the predicate result
220
+ * is undefined, the failure action is best used to either return a default value or throw an error.
221
+ *
222
+ * This processor can act as a constraint if the success or failure actions throw when triggered.
223
+ *
224
+ * To check truthiness instead of defined/undefined, see $check.
225
+ * To pass the original input to the actions instead of the predicate result, see $gate.
226
+ *
227
+ * ### Parameters
228
+ * - `predicate` (processor, optional): Evaluated for defined-ness. The predicate result (not the original input) is passed to success/failure.
229
+ * - `success` (processor, optional): Invoked with the predicate result when it is defined. Default: returns predicate result.
230
+ * - `failure` (processor, optional): Invoked with the predicate result (undefined) when predicate returns undefined or throws. Default: returns undefined.
231
+ *
232
+ * Array form: `{$when: [predicate, success, failure]}`
233
+ * Object form: `{$when: {predicate: ..., success: ..., failure: ...}}`
234
+ *
235
+ * ### Example
236
+ * ```js
237
+ * // Extract a nested value, providing a default when not found
238
+ * new Schema('object').transformer({
239
+ * $when: [
240
+ * {$get: 'config.timeout'},
241
+ * null,
242
+ * () => 5000 // default 5s when config.timeout not set
243
+ * ]
244
+ * })
245
+ *
246
+ * // Chain: extract the match groups object if the pattern matched, otherwise drop
247
+ * new Schema('string').transformer({
248
+ * $when: [{$match: /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/}]
249
+ * })
250
+ * ```
251
+ *
252
+ * @type {ValueProcessorDefinition}
253
+ */
254
+ export const WHEN_OPERATOR = {
255
+ keyword: 'when',
256
+ build: generateBuilderFunction('$when',[ConditionalExecutor.CHECK_DEFINED, ConditionalExecutor.PASS_RESULT]),
257
+ };
258
+
259
+ /**
260
+ * ## $try
261
+ *
262
+ * This is a conditional operator that evaluates a predicate for whether it throws/rejects.
263
+ *
264
+ * If the predicate does not throw, $try will return the result of invoking any provided success action
265
+ * with the predicate result. If not provided, the default success action will simply return the predicate result.
266
+ *
267
+ * If the predicate rejects/throws, $try will return the result of invoking the failure action with the error returned.
268
+ * The default failure action will return undefined. Since the predicate result is an error, the failure action is
269
+ * best used to either intercept errors and return a default value, or to propagate or wrap the error.
270
+ *
271
+ * This processor can act as a constraint if the success or failure actions throw when triggered.
272
+ *
273
+ * ### Parameters
274
+ * - `predicate` (processor, optional): Executed and observed for throw/rejection.
275
+ * - `success` (processor, optional): Invoked with the predicate result if it did not throw. Default: returns predicate result.
276
+ * - `failure` (processor, optional): Invoked with the thrown error if the predicate threw. Default: returns undefined.
277
+ *
278
+ * Array form: `{$try: [predicate, success, failure]}`
279
+ * Object form: `{$try: {predicate: ..., success: ..., failure: ...}}`
280
+ *
281
+ * ### Example
282
+ * ```js
283
+ * // Attempt JSON parsing, fall back to raw string on failure
284
+ * new Schema('string').normalizer({
285
+ * $try: ['$json-decode', null, (err) => err.input]
286
+ * })
287
+ *
288
+ * // Attempt to parse a number, silently drop value on error
289
+ * new Schema('string').normalizer({
290
+ * $try: ['$number']
291
+ * })
292
+ * ```
293
+ *
294
+ * @type {ValueProcessorDefinition}
295
+ */
296
+
297
+ export const TRY_OPERATOR = {
298
+ keyword: 'try',
299
+ build: generateBuilderFunction('$try',[ConditionalExecutor.PASS_ERROR, ConditionalExecutor.PASS_RESULT]),
300
+ };
301
+
@@ -0,0 +1,16 @@
1
+ import { toConstantCase } from '../../helpers/case.js';
2
+
3
+ /**
4
+ * ## $constant-case
5
+ *
6
+ * Converts a string to CONSTANT_CASE format (uppercase letters with underscores).
7
+ * Safe to use in normalize phase (non-throwing).
8
+ *
9
+ * @type {import("../../value-processor/value-processor.js").ValueProcessorDefinition}
10
+ */
11
+ export const CONSTANT_CASE_OPERATOR = {
12
+ keyword: 'constant-case',
13
+ process: (value) => {
14
+ return toConstantCase(String(value));
15
+ }
16
+ };
@@ -0,0 +1,86 @@
1
+ import { ConstraintError, ResolverError } from '../../errors.js';
2
+ import { parseDataSize, formatDataSize } from '../../helpers/data-size.js';
3
+
4
+ const VALID_STANDARDS = new Set(['iec', 'si', 'any']);
5
+ const VALID_FORMATS = new Set(['iec', 'si']);
6
+
7
+ /**
8
+ * ## $data-size
9
+ *
10
+ * Parses data-size strings (e.g. `'20KiB'`, `'1.5 MB'`, `'512'`) into byte
11
+ * counts. Bare numbers and numeric strings without a suffix are treated as bytes.
12
+ *
13
+ * ### Parameters
14
+ * - `standard` (string, optional): Which unit standard to accept when parsing.
15
+ * `'iec'` (binary: KiB, MiB, ...), `'si'` (decimal: KB, MB, ...), or
16
+ * `'any'` (both). Default: `'any'`.
17
+ * - `format` (string, optional): When set, returns a formatted string instead
18
+ * of a byte count. Accepts `'iec'` or `'si'` to control the output standard.
19
+ *
20
+ * ### Example
21
+ * ```js
22
+ * // Parse to bytes (normalizer)
23
+ * new Schema().normalizer('$data-size')
24
+ *
25
+ * // Strict IEC only
26
+ * new Schema().normalizer({'$data-size': {standard: 'iec'}})
27
+ *
28
+ * // Parse and format back as SI string
29
+ * new Schema().normalizer({'$data-size': {format: 'si'}})
30
+ *
31
+ * // Use as normalizer ahead of number validation
32
+ * new Schema().normalizer(['$data-size']).validator('$is-number')
33
+ * ```
34
+ *
35
+ * @type {import("../../value-processor/value-processor.js").ValueProcessorDefinition}
36
+ */
37
+ export const DATA_SIZE_OPERATOR = {
38
+ keyword: 'data-size',
39
+
40
+ parameters: [
41
+ { parameter: 'standard', default: 'any', type: 'string' },
42
+ { parameter: 'format', default: undefined, type: 'string' },
43
+ ],
44
+
45
+ process: (value, _target, _location, options) => {
46
+ const { standard, format } = options?.args ?? {};
47
+
48
+ if (standard !== undefined && !VALID_STANDARDS.has(standard)) {
49
+ throw new ResolverError(`$data-size: unknown standard "${standard}" (expected iec, si, or any)`);
50
+ }
51
+ if (format !== undefined && !VALID_FORMATS.has(format)) {
52
+ throw new ResolverError(`$data-size: unknown format "${format}" (expected iec or si)`);
53
+ }
54
+
55
+ // passthrough numbers when no format is requested
56
+ if (typeof value === 'number' && format === undefined) {
57
+ if (!Number.isFinite(value) || value < 0) {
58
+ throw new ConstraintError(`Invalid data size: ${value}`);
59
+ }
60
+ return value;
61
+ }
62
+
63
+ const bytes = parseDataSize(value, standard === 'any' ? undefined : standard);
64
+
65
+ if (format !== undefined) {
66
+ return formatDataSize(bytes, format);
67
+ }
68
+
69
+ return bytes;
70
+ },
71
+
72
+ describe: (args) => {
73
+ if (!args) return undefined;
74
+
75
+ const standardProcessor = (Array.isArray(args) ? args[0] : args.standard);
76
+ const formatProcessor = (Array.isArray(args) ? args[1] : args.format);
77
+
78
+ const standard = standardProcessor?.description;
79
+ const format = formatProcessor?.description;
80
+
81
+ const parts = [];
82
+ if (standard !== undefined && standard !== 'any') parts.push(standard.toUpperCase());
83
+ if (format !== undefined) parts.push(`\u2192 ${format.toUpperCase()}`);
84
+ return parts.length ? parts.join(', ') : undefined;
85
+ }
86
+ };
@@ -0,0 +1,54 @@
1
+ import { ConstraintError } from '../../errors.js';
2
+ import { formatValue } from '../../helpers/format.js';
3
+
4
+ /**
5
+ * ## $date-object
6
+ *
7
+ * Expands a Date into a plain object with named calendar fields.
8
+ *
9
+ * - `'$date-object'` or `{'$date-object': 'utc'}` — UTC fields (default)
10
+ * - `{'$date-object': 'local'}` — local-timezone fields
11
+ *
12
+ * Output shape: `{year, month, day, hour, minute, second, ms, zone}`
13
+ * where `month` is 1–12 (not 0-based), and `zone` is `'utc'` or `'local'`.
14
+ *
15
+ * The output is accepted by `$date` for round-trip reassembly.
16
+ *
17
+ * @type {import('../../value-processor/value-processor.js').ValueProcessorDefinition}
18
+ */
19
+ export const DATE_OBJECT_OPERATOR = {
20
+ keyword: 'date-object',
21
+ parameters: [{parameter: 'zone'}],
22
+
23
+ process: (value, _target, location, options) => {
24
+ if (!(value instanceof Date)) {
25
+ throw new ConstraintError(`$date-object requires a Date, got ${formatValue(value)}`, {location});
26
+ }
27
+ const zone = options.args['zone'] ?? 'utc';
28
+ if (zone !== 'utc' && zone !== 'local') {
29
+ throw new ConstraintError(`$date-object zone must be 'utc' or 'local', got ${formatValue(zone)}`, {location});
30
+ }
31
+ if (zone === 'local') {
32
+ return {
33
+ year: value.getFullYear(),
34
+ month: value.getMonth() + 1,
35
+ day: value.getDate(),
36
+ hour: value.getHours(),
37
+ minute: value.getMinutes(),
38
+ second: value.getSeconds(),
39
+ ms: value.getMilliseconds(),
40
+ zone: 'local',
41
+ };
42
+ }
43
+ return {
44
+ year: value.getUTCFullYear(),
45
+ month: value.getUTCMonth() + 1,
46
+ day: value.getUTCDate(),
47
+ hour: value.getUTCHours(),
48
+ minute: value.getUTCMinutes(),
49
+ second: value.getUTCSeconds(),
50
+ ms: value.getUTCMilliseconds(),
51
+ zone: 'utc',
52
+ };
53
+ }
54
+ };
@@ -0,0 +1,67 @@
1
+ import { parseDate } from '../../helpers/parse-date.js';
2
+ import { ConstraintError } from '../../errors.js';
3
+
4
+ /**
5
+ * Reassemble a date from a `$date-object` field object.
6
+ * @param {{year:number, month:number, day:number, hour?:number, minute?:number, second?:number, ms?:number, zone?:string}} obj
7
+ * @returns {Date}
8
+ */
9
+ function fromDateObject(obj) {
10
+ const {year, month, day, hour = 0, minute = 0, second = 0, ms = 0, zone = 'utc'} = obj;
11
+ if (zone === 'local') {
12
+ return new Date(year, month - 1, day, hour, minute, second, ms);
13
+ }
14
+ return new Date(Date.UTC(year, month - 1, day, hour, minute, second, ms));
15
+ }
16
+
17
+ /**
18
+ * ## $date
19
+ *
20
+ * Normalize the input value as a Date. Accepts strings, numbers, Date instances, and
21
+ * date-field objects produced by `$date-object`.
22
+ *
23
+ * - `'now'` — returns the current timestamp
24
+ * - `'+1h'`, `'-30m'`, etc. — relative offsets from now
25
+ * - ISO strings and numeric epoch values are parsed automatically
26
+ * - `{year, month, day, ...zone}` — reassembled from a `$date-object` output
27
+ *
28
+ * Output type is inferred from the schema's `type` option:
29
+ * - `'string'` → ISO string
30
+ * - `'number'` → epoch milliseconds
31
+ * - otherwise → `Date` instance
32
+ *
33
+ * See `$is-date` for strict Date validation.
34
+ *
35
+ * @type {import("../../value-processor/value-processor.js").ValueProcessorDefinition}
36
+ */
37
+ export const DATE_OPERATOR = {
38
+ keyword: 'date',
39
+
40
+ process: (value, _target, location) => {
41
+ // Accept date-field objects produced by $date-object
42
+ if (typeof value === 'object' && value !== null && !(value instanceof Date)
43
+ && Number.isInteger(value.year) && Number.isInteger(value.month)) {
44
+ const date = fromDateObject(value);
45
+ if (isNaN(date.getTime())) {
46
+ throw new ConstraintError(`Invalid date object: ${JSON.stringify(value)}`);
47
+ }
48
+ if (location.schema.options.type === 'string') return date.toISOString();
49
+ if (location.schema.options.type === 'number') return date.getTime();
50
+ return date;
51
+ }
52
+ if (typeof value !== 'string' && typeof value !== 'number' && !(value instanceof Date)) {
53
+ throw new ConstraintError(`Invalid input for date: ${value}`);
54
+ }
55
+ const date = parseDate(value);
56
+
57
+ if (location.schema.options.type === 'string') {
58
+ return date.toISOString();
59
+ }
60
+ else if (location.schema.options.type === 'number') {
61
+ return date.getTime();
62
+ }
63
+ else {
64
+ return date;
65
+ }
66
+ }
67
+ };