@vielzeug/spell 26.11.9 → 26.11.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/README.md +1 -1
  2. package/dist/_dev.cjs.map +1 -1
  3. package/dist/_dev.js.map +1 -1
  4. package/dist/_object.cjs +2 -0
  5. package/dist/_object.cjs.map +1 -0
  6. package/dist/_object.d.ts +9 -0
  7. package/dist/_object.d.ts.map +1 -0
  8. package/dist/_object.js +2 -0
  9. package/dist/_object.js.map +1 -0
  10. package/dist/_parse.cjs +2 -0
  11. package/dist/_parse.cjs.map +1 -0
  12. package/dist/_parse.d.ts +38 -0
  13. package/dist/_parse.d.ts.map +1 -0
  14. package/dist/_parse.js +2 -0
  15. package/dist/_parse.js.map +1 -0
  16. package/dist/errors.cjs +2 -2
  17. package/dist/errors.cjs.map +1 -1
  18. package/dist/errors.d.ts +15 -26
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +2 -2
  21. package/dist/errors.js.map +1 -1
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.ts +4 -4
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +1 -1
  26. package/dist/json.cjs +2 -1
  27. package/dist/json.cjs.map +1 -0
  28. package/dist/json.d.ts +8 -1
  29. package/dist/json.d.ts.map +1 -1
  30. package/dist/json.js +2 -1
  31. package/dist/json.js.map +1 -0
  32. package/dist/messages.cjs +1 -1
  33. package/dist/messages.cjs.map +1 -1
  34. package/dist/messages.d.ts +7 -6
  35. package/dist/messages.d.ts.map +1 -1
  36. package/dist/messages.js +1 -1
  37. package/dist/messages.js.map +1 -1
  38. package/dist/predicates.cjs +2 -1
  39. package/dist/predicates.cjs.map +1 -0
  40. package/dist/predicates.d.ts +41 -2
  41. package/dist/predicates.d.ts.map +1 -1
  42. package/dist/predicates.js +2 -1
  43. package/dist/predicates.js.map +1 -0
  44. package/dist/s.cjs +1 -1
  45. package/dist/s.cjs.map +1 -1
  46. package/dist/s.d.ts +21 -22
  47. package/dist/s.d.ts.map +1 -1
  48. package/dist/s.js +1 -1
  49. package/dist/s.js.map +1 -1
  50. package/dist/schema.cjs +2 -0
  51. package/dist/schema.cjs.map +1 -0
  52. package/dist/schema.d.ts +93 -0
  53. package/dist/schema.d.ts.map +1 -0
  54. package/dist/schema.js +2 -0
  55. package/dist/schema.js.map +1 -0
  56. package/dist/schemas/array.cjs +1 -1
  57. package/dist/schemas/array.cjs.map +1 -1
  58. package/dist/schemas/array.d.ts +22 -32
  59. package/dist/schemas/array.d.ts.map +1 -1
  60. package/dist/schemas/array.js +1 -1
  61. package/dist/schemas/array.js.map +1 -1
  62. package/dist/schemas/bigint.cjs +1 -1
  63. package/dist/schemas/bigint.cjs.map +1 -1
  64. package/dist/schemas/bigint.d.ts +14 -18
  65. package/dist/schemas/bigint.d.ts.map +1 -1
  66. package/dist/schemas/bigint.js +1 -1
  67. package/dist/schemas/bigint.js.map +1 -1
  68. package/dist/schemas/date.cjs +1 -1
  69. package/dist/schemas/date.cjs.map +1 -1
  70. package/dist/schemas/date.d.ts +7 -6
  71. package/dist/schemas/date.d.ts.map +1 -1
  72. package/dist/schemas/date.js +1 -1
  73. package/dist/schemas/date.js.map +1 -1
  74. package/dist/schemas/discriminated-union.cjs +2 -0
  75. package/dist/schemas/discriminated-union.cjs.map +1 -0
  76. package/dist/schemas/discriminated-union.d.ts +39 -0
  77. package/dist/schemas/discriminated-union.d.ts.map +1 -0
  78. package/dist/schemas/discriminated-union.js +2 -0
  79. package/dist/schemas/discriminated-union.js.map +1 -0
  80. package/dist/schemas/intersect.cjs +1 -1
  81. package/dist/schemas/intersect.cjs.map +1 -1
  82. package/dist/schemas/intersect.d.ts +10 -9
  83. package/dist/schemas/intersect.d.ts.map +1 -1
  84. package/dist/schemas/intersect.js +1 -1
  85. package/dist/schemas/intersect.js.map +1 -1
  86. package/dist/schemas/lazy.cjs +1 -1
  87. package/dist/schemas/lazy.cjs.map +1 -1
  88. package/dist/schemas/lazy.d.ts +10 -10
  89. package/dist/schemas/lazy.d.ts.map +1 -1
  90. package/dist/schemas/lazy.js +1 -1
  91. package/dist/schemas/lazy.js.map +1 -1
  92. package/dist/schemas/map.cjs +1 -1
  93. package/dist/schemas/map.cjs.map +1 -1
  94. package/dist/schemas/map.d.ts +8 -7
  95. package/dist/schemas/map.d.ts.map +1 -1
  96. package/dist/schemas/map.js +1 -1
  97. package/dist/schemas/map.js.map +1 -1
  98. package/dist/schemas/number.cjs +1 -1
  99. package/dist/schemas/number.cjs.map +1 -1
  100. package/dist/schemas/number.d.ts +20 -27
  101. package/dist/schemas/number.d.ts.map +1 -1
  102. package/dist/schemas/number.js +1 -1
  103. package/dist/schemas/number.js.map +1 -1
  104. package/dist/schemas/object.cjs +1 -1
  105. package/dist/schemas/object.cjs.map +1 -1
  106. package/dist/schemas/object.d.ts +40 -88
  107. package/dist/schemas/object.d.ts.map +1 -1
  108. package/dist/schemas/object.js +1 -1
  109. package/dist/schemas/object.js.map +1 -1
  110. package/dist/schemas/primitives.cjs +2 -0
  111. package/dist/schemas/primitives.cjs.map +1 -0
  112. package/dist/schemas/primitives.d.ts +38 -0
  113. package/dist/schemas/primitives.d.ts.map +1 -0
  114. package/dist/schemas/primitives.js +2 -0
  115. package/dist/schemas/primitives.js.map +1 -0
  116. package/dist/schemas/record.cjs +1 -1
  117. package/dist/schemas/record.cjs.map +1 -1
  118. package/dist/schemas/record.d.ts +7 -8
  119. package/dist/schemas/record.d.ts.map +1 -1
  120. package/dist/schemas/record.js +1 -1
  121. package/dist/schemas/record.js.map +1 -1
  122. package/dist/schemas/set.cjs +1 -1
  123. package/dist/schemas/set.cjs.map +1 -1
  124. package/dist/schemas/set.d.ts +8 -7
  125. package/dist/schemas/set.d.ts.map +1 -1
  126. package/dist/schemas/set.js +1 -1
  127. package/dist/schemas/set.js.map +1 -1
  128. package/dist/schemas/string.cjs +1 -1
  129. package/dist/schemas/string.cjs.map +1 -1
  130. package/dist/schemas/string.d.ts +42 -98
  131. package/dist/schemas/string.d.ts.map +1 -1
  132. package/dist/schemas/string.js +1 -1
  133. package/dist/schemas/string.js.map +1 -1
  134. package/dist/schemas/tuple.cjs +1 -1
  135. package/dist/schemas/tuple.cjs.map +1 -1
  136. package/dist/schemas/tuple.d.ts +9 -9
  137. package/dist/schemas/tuple.d.ts.map +1 -1
  138. package/dist/schemas/tuple.js +1 -1
  139. package/dist/schemas/tuple.js.map +1 -1
  140. package/dist/schemas/union.cjs +1 -1
  141. package/dist/schemas/union.cjs.map +1 -1
  142. package/dist/schemas/union.d.ts +9 -7
  143. package/dist/schemas/union.d.ts.map +1 -1
  144. package/dist/schemas/union.js +1 -1
  145. package/dist/schemas/union.js.map +1 -1
  146. package/dist/spell.cjs +2 -2
  147. package/dist/spell.cjs.map +1 -1
  148. package/dist/spell.iife.js +2 -2
  149. package/dist/spell.iife.js.map +1 -1
  150. package/dist/spell.js +842 -1601
  151. package/dist/spell.js.map +1 -1
  152. package/dist/types.cjs +1 -1
  153. package/dist/types.cjs.map +1 -1
  154. package/dist/types.d.ts +151 -265
  155. package/dist/types.d.ts.map +1 -1
  156. package/dist/types.js +1 -1
  157. package/dist/types.js.map +1 -1
  158. package/package.json +2 -2
  159. package/dist/core.cjs +0 -2
  160. package/dist/core.cjs.map +0 -1
  161. package/dist/core.d.ts +0 -101
  162. package/dist/core.d.ts.map +0 -1
  163. package/dist/core.js +0 -2
  164. package/dist/core.js.map +0 -1
  165. package/dist/formats.cjs +0 -2
  166. package/dist/formats.cjs.map +0 -1
  167. package/dist/formats.d.ts +0 -41
  168. package/dist/formats.d.ts.map +0 -1
  169. package/dist/formats.js +0 -2
  170. package/dist/formats.js.map +0 -1
  171. package/dist/json-schema.cjs +0 -2
  172. package/dist/json-schema.cjs.map +0 -1
  173. package/dist/json-schema.d.ts +0 -5
  174. package/dist/json-schema.d.ts.map +0 -1
  175. package/dist/json-schema.js +0 -2
  176. package/dist/json-schema.js.map +0 -1
  177. package/dist/safe-object.cjs +0 -2
  178. package/dist/safe-object.cjs.map +0 -1
  179. package/dist/safe-object.d.ts +0 -6
  180. package/dist/safe-object.d.ts.map +0 -1
  181. package/dist/safe-object.js +0 -2
  182. package/dist/safe-object.js.map +0 -1
  183. package/dist/schemas/boolean.cjs +0 -2
  184. package/dist/schemas/boolean.cjs.map +0 -1
  185. package/dist/schemas/boolean.d.ts +0 -9
  186. package/dist/schemas/boolean.d.ts.map +0 -1
  187. package/dist/schemas/boolean.js +0 -2
  188. package/dist/schemas/boolean.js.map +0 -1
  189. package/dist/schemas/enum.cjs +0 -2
  190. package/dist/schemas/enum.cjs.map +0 -1
  191. package/dist/schemas/enum.d.ts +0 -12
  192. package/dist/schemas/enum.d.ts.map +0 -1
  193. package/dist/schemas/enum.js +0 -2
  194. package/dist/schemas/enum.js.map +0 -1
  195. package/dist/schemas/instanceof.cjs +0 -2
  196. package/dist/schemas/instanceof.cjs.map +0 -1
  197. package/dist/schemas/instanceof.d.ts +0 -9
  198. package/dist/schemas/instanceof.d.ts.map +0 -1
  199. package/dist/schemas/instanceof.js +0 -2
  200. package/dist/schemas/instanceof.js.map +0 -1
  201. package/dist/schemas/literal.cjs +0 -2
  202. package/dist/schemas/literal.cjs.map +0 -1
  203. package/dist/schemas/literal.d.ts +0 -9
  204. package/dist/schemas/literal.d.ts.map +0 -1
  205. package/dist/schemas/literal.js +0 -2
  206. package/dist/schemas/literal.js.map +0 -1
  207. package/dist/schemas/never.cjs +0 -2
  208. package/dist/schemas/never.cjs.map +0 -1
  209. package/dist/schemas/never.d.ts +0 -8
  210. package/dist/schemas/never.d.ts.map +0 -1
  211. package/dist/schemas/never.js +0 -2
  212. package/dist/schemas/never.js.map +0 -1
  213. package/dist/schemas/raw-or.cjs +0 -2
  214. package/dist/schemas/raw-or.cjs.map +0 -1
  215. package/dist/schemas/raw-or.d.ts +0 -17
  216. package/dist/schemas/raw-or.d.ts.map +0 -1
  217. package/dist/schemas/raw-or.js +0 -2
  218. package/dist/schemas/raw-or.js.map +0 -1
  219. package/dist/schemas/variant.cjs +0 -2
  220. package/dist/schemas/variant.cjs.map +0 -1
  221. package/dist/schemas/variant.d.ts +0 -37
  222. package/dist/schemas/variant.d.ts.map +0 -1
  223. package/dist/schemas/variant.js +0 -2
  224. package/dist/schemas/variant.js.map +0 -1
  225. package/dist/tolerate.cjs +0 -2
  226. package/dist/tolerate.cjs.map +0 -1
  227. package/dist/tolerate.d.ts +0 -10
  228. package/dist/tolerate.d.ts.map +0 -1
  229. package/dist/tolerate.js +0 -2
  230. package/dist/tolerate.js.map +0 -1
  231. package/dist/validators.cjs +0 -2
  232. package/dist/validators.cjs.map +0 -1
  233. package/dist/validators.d.ts +0 -20
  234. package/dist/validators.d.ts.map +0 -1
  235. package/dist/validators.js +0 -2
  236. package/dist/validators.js.map +0 -1
package/dist/spell.js CHANGED
@@ -1,50 +1,37 @@
1
1
  import { isPlainObject as e } from "@vielzeug/arsenal";
2
2
  // src/errors.ts
3
- function fail(e, t, n) {
4
- return [{
5
- code: e,
6
- message: t,
7
- params: n,
8
- path: []
9
- }];
10
- }
11
- function resolveMessage(e, t) {
12
- return typeof e == "function" ? e(t) : e;
13
- }
14
- function prependIssuePath(e, t) {
15
- return e.map((e) => ({
16
- ...e,
17
- path: [t, ...e.path]
18
- }));
19
- }
20
- function joinIssuePath(e, t = ".") {
21
- return !e || e.length === 0 ? "" : e.map((e) => typeof e == "object" && e && "key" in e ? String(e.key) : String(e)).join(t);
22
- }
23
3
  var SpellError = class extends Error {
24
4
  constructor(e, t) {
25
5
  super(e, t), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
26
6
  }
27
- };
28
- function formatIssues(e) {
29
- return e.map(({ code: e, message: t, path: n }) => `${n.length ? n.join(".") : "value"}: ${t} [${e}]`).join("\n");
7
+ }, SpellDefinitionError = class extends SpellError {};
8
+ function joinIssuePath(e, t = ".") {
9
+ return e ? e.map((e) => String(typeof e == "object" && e ? e.key : e)).join(t) : "";
30
10
  }
31
11
  function pathsEqual(e, t) {
32
12
  return e.length === t.length && e.every((e, n) => e === t[n]);
33
13
  }
34
- var SpellDefinitionError = class extends SpellError {}, SpellValidationError = class extends SpellError {
14
+ var SpellValidationError = class extends SpellError {
35
15
  issues;
36
16
  constructor(e, t) {
37
- super(formatIssues(e), { cause: t }), this.issues = Object.freeze([...e]);
17
+ super(e.map(({ code: e, message: t, path: n }) => `${joinIssuePath(n) || "value"}: ${t} [${e}]`).join("\n"), t), this.issues = Object.freeze([...e]);
38
18
  }
39
19
  flatten() {
40
20
  let e = [], t = [], n = /* @__PURE__ */ new Map();
41
- for (let r of this.issues) if (r.path.length === 0) t.push(r.message);
42
- else {
43
- let t = JSON.stringify(r.path), i = n.get(t);
44
- i === void 0 ? (n.set(t, e.length), e.push({
45
- messages: [r.message],
46
- path: [...r.path]
47
- })) : e[i]?.messages.push(r.message);
21
+ for (let r of this.issues) {
22
+ if (r.path.length === 0) {
23
+ t.push(r.message);
24
+ continue;
25
+ }
26
+ let i = JSON.stringify(r.path), a = n.get(i);
27
+ if (a) a.messages.push(r.message);
28
+ else {
29
+ let t = {
30
+ messages: [r.message],
31
+ path: [...r.path]
32
+ };
33
+ n.set(i, t), e.push(t);
34
+ }
48
35
  }
49
36
  return {
50
37
  fieldErrors: e,
@@ -54,9 +41,9 @@ var SpellDefinitionError = class extends SpellError {}, SpellValidationError = c
54
41
  flattenFirst() {
55
42
  let { fieldErrors: e, formErrors: t } = this.flatten();
56
43
  return {
57
- fieldErrors: e.map((e) => ({
58
- message: e.messages[0],
59
- path: e.path
44
+ fieldErrors: e.map(({ messages: e, path: t }) => ({
45
+ message: e[0],
46
+ path: t
60
47
  })),
61
48
  formErrors: t
62
49
  };
@@ -85,8 +72,13 @@ function objectFromEntries(e) {
85
72
  for (let [n, r] of e) defineOwnProperty(t, n, r);
86
73
  return t;
87
74
  }
88
- function cloneRecord(e) {
89
- return objectFromEntries(Object.entries(e));
75
+ function materialize(t) {
76
+ return Array.isArray(t) ? t.map(materialize) : t instanceof Date ? new Date(t.getTime()) : t instanceof Map ? new Map([...t].map(([e, t]) => [materialize(e), materialize(t)])) : t instanceof Set ? new Set([...t].map(materialize)) : e(t) ? objectFromEntries(Object.entries(t).map(([e, t]) => [e, materialize(t)])) : t;
77
+ }
78
+ function deepFreeze(e) {
79
+ if (typeof e != "object" || !e || Object.isFrozen(e)) return e;
80
+ for (let t of Object.values(e)) deepFreeze(t);
81
+ return Object.freeze(e);
90
82
  }
91
83
  // src/messages.ts
92
84
  var n = {
@@ -115,6 +107,10 @@ var n = {
115
107
  min: ({ min: e }) => `Must be after ${e.toISOString()}`,
116
108
  type: () => "Expected valid date"
117
109
  },
110
+ discriminatedUnion: {
111
+ invalidDiscriminator: ({ discriminator: e, expected: t }) => `Invalid discriminator value at "${e}": expected ${t.map((e) => JSON.stringify(e)).join(" | ")}`,
112
+ type: () => "Expected object"
113
+ },
118
114
  enum: { invalid: ({ values: e }) => `Expected one of: ${e.map((e) => JSON.stringify(e)).join(", ")}` },
119
115
  instanceof: { type: ({ className: e }) => `Expected instance of ${e}` },
120
116
  literal: { expected: ({ expected: e }) => `Expected ${JSON.stringify(e)}` },
@@ -155,8 +151,6 @@ var n = {
155
151
  base64url: () => "Invalid base64url",
156
152
  cuid: () => "Invalid CUID",
157
153
  cuid2: () => "Invalid CUID2",
158
- date: () => "Invalid date",
159
- dateTime: () => "Invalid datetime",
160
154
  duration: () => "Invalid ISO 8601 duration",
161
155
  email: () => "Invalid email address",
162
156
  emoji: () => "Invalid emoji sequence",
@@ -165,6 +159,8 @@ var n = {
165
159
  hexColor: () => "Invalid hex color",
166
160
  includes: ({ substr: e }) => `Must include "${e}"`,
167
161
  ip: () => "Invalid IP address",
162
+ isoDate: () => "Invalid date",
163
+ isoDateTime: () => "Invalid datetime",
168
164
  jwt: () => "Invalid JWT",
169
165
  length: ({ exact: e }) => `Must be exactly ${e} characters`,
170
166
  max: ({ max: e }) => `Must be at most ${e} characters`,
@@ -187,230 +183,208 @@ var n = {
187
183
  min: ({ min: e }) => `Expected tuple with at least ${e} items`,
188
184
  type: () => "Expected array"
189
185
  },
190
- union: { invalid: () => "Does not match any of the expected types" },
191
- variant: {
192
- invalidDiscriminator: ({ discriminator: e, expected: t }) => `Invalid discriminator value at "${e}": expected ${t.map((e) => JSON.stringify(e)).join(" | ")}`,
193
- type: () => "Expected object"
194
- }
186
+ union: { invalid: () => "Does not match any of the expected types" }
195
187
  };
196
- function isRecord(e) {
197
- return typeof e == "object" && !!e && !Array.isArray(e);
198
- }
199
- function mergeMessages(e, t) {
200
- let n = cloneRecord(e);
201
- for (let [e, r] of Object.entries(t)) {
202
- if (r === void 0) continue;
203
- if (isUnsafeObjectKey(e)) {
204
- `${e}`;
188
+ function mergeMessages(t, n) {
189
+ let r = objectFromEntries(Object.entries(t));
190
+ for (let [t, i] of Object.entries(n)) {
191
+ if (i === void 0) continue;
192
+ if (isUnsafeObjectKey(t)) {
193
+ `${t}`;
205
194
  continue;
206
195
  }
207
- let t = n[e];
208
- isRecord(t) && isRecord(r) ? defineOwnProperty(n, e, mergeMessages(t, r)) : defineOwnProperty(n, e, r);
196
+ let n = r[t];
197
+ defineOwnProperty(r, t, e(n) && e(i) ? mergeMessages(n, i) : i);
209
198
  }
210
- return n;
199
+ return r;
211
200
  }
212
201
  function createParseContext(e = {}) {
213
- return { messages: mergeMessages(n, e) };
202
+ return deepFreeze({ messages: mergeMessages(n, e) });
214
203
  }
215
- // src/types.ts
216
- var r = {
204
+ var r = createParseContext();
205
+ // src/_parse.ts
206
+ function chain(e, t) {
207
+ return e instanceof Promise ? e.then(t) : t(e);
208
+ }
209
+ function all(e) {
210
+ return e.some((e) => e instanceof Promise) ? Promise.all(e) : e;
211
+ }
212
+ function issue(e, t, ...[n]) {
213
+ return n === void 0 ? {
214
+ code: e,
215
+ message: t,
216
+ path: []
217
+ } : {
218
+ code: e,
219
+ message: t,
220
+ params: n,
221
+ path: []
222
+ };
223
+ }
224
+ function resolveMessage(e, t) {
225
+ return typeof e == "function" ? e(t) : e;
226
+ }
227
+ function prefixIssues(e, t) {
228
+ return e.map((e) => ({
229
+ ...e,
230
+ path: [t, ...e.path]
231
+ }));
232
+ }
233
+ var atLeast = (e, t) => e === void 0 || t > e ? t : e, atMost = (e, t) => e === void 0 || t < e ? t : e, ok = (e) => ({
234
+ issues: [],
235
+ typeOk: !0,
236
+ value: e
237
+ }), invalid = (e, ...t) => ({
238
+ issues: t,
239
+ typeOk: !1,
240
+ value: e
241
+ }), i = {
217
242
  custom: "custom",
218
- invalid_base64: "invalid_base64",
219
243
  invalid_date: "invalid_date",
220
- invalid_duration: "invalid_duration",
244
+ invalid_discriminator: "invalid_discriminator",
221
245
  invalid_enum: "invalid_enum",
222
246
  invalid_finite: "invalid_finite",
247
+ invalid_format: "invalid_format",
223
248
  invalid_integer: "invalid_integer",
224
249
  invalid_keys: "invalid_keys",
225
250
  invalid_length: "invalid_length",
226
251
  invalid_literal: "invalid_literal",
227
252
  invalid_multiple_of: "invalid_multiple_of",
228
253
  invalid_safe: "invalid_safe",
229
- invalid_string: "invalid_string",
230
254
  invalid_type: "invalid_type",
231
255
  invalid_union: "invalid_union",
232
256
  invalid_unique: "invalid_unique",
233
- invalid_url: "invalid_url",
234
- invalid_variant: "invalid_variant",
235
257
  too_big: "too_big",
236
258
  too_small: "too_small"
237
- };
238
- // src/core.ts
239
- function deepFreeze(e) {
240
- if (typeof e != "object" || !e || Object.isFrozen(e)) return e;
241
- for (let t of Object.values(e)) deepFreeze(t);
242
- return Object.freeze(e);
243
- }
244
- function materializeValue(t) {
245
- if (typeof t != "object" || !t) return t;
246
- if (Array.isArray(t)) return t.map((e) => materializeValue(e));
247
- if (t instanceof Date) return new Date(t.getTime());
248
- if (t instanceof Map) {
249
- let e = /* @__PURE__ */ new Map();
250
- for (let [n, r] of t.entries()) e.set(materializeValue(n), materializeValue(r));
251
- return e;
252
- }
253
- if (t instanceof Set) return new Set([...t.values()].map((e) => materializeValue(e)));
254
- if (e(t)) {
255
- let e = {};
256
- for (let [n, r] of Object.entries(t)) defineOwnProperty(e, n, materializeValue(r));
257
- return e;
258
- }
259
- return t;
260
- }
261
- function defaultState() {
262
- return {
263
- hasAsyncChecks: !1,
264
- hasRuntimeChecks: !1,
265
- isNullable: !1,
266
- isOptional: !1,
267
- postprocessors: [],
268
- preprocessors: [],
269
- validators: []
270
- };
271
- }
272
- function cloneState(e) {
273
- return {
274
- catch: e.catch,
275
- defaultValue: e.defaultValue,
276
- description: e.description,
277
- hasAsyncChecks: e.hasAsyncChecks,
278
- hasRuntimeChecks: e.hasRuntimeChecks,
279
- isNullable: e.isNullable,
280
- isOptional: e.isOptional,
281
- postprocessors: [...e.postprocessors],
282
- preprocessors: [...e.preprocessors],
283
- validators: [...e.validators]
259
+ }, a = {
260
+ meta: {},
261
+ nullable: !1,
262
+ optional: !1,
263
+ portable: !0,
264
+ preprocess: [],
265
+ steps: []
266
+ }, asyncMisuse = () => new SpellError("Schema contains async checks; use parseAsync() or safeParseAsync().");
267
+ function toResult({ issues: e, value: t }) {
268
+ return e.length === 0 ? {
269
+ data: t,
270
+ success: !0
271
+ } : {
272
+ error: new SpellValidationError(e),
273
+ success: !1
284
274
  };
285
275
  }
286
- function _makeCtx(e) {
287
- return e ? { messages: e } : createParseContext();
288
- }
289
- function normalizeValidateResult(e, t, n) {
290
- let i = [...t];
291
- return typeof e == "string" ? i.push({
292
- code: r.custom,
293
- message: e,
294
- path: []
295
- }) : e === !1 && i.length === 0 && i.push({
296
- code: r.custom,
297
- message: n.messages.check.default(),
298
- path: []
299
- }), i.length ? i : null;
276
+ function checkIssues(e, t, n) {
277
+ return typeof e == "string" ? t.push(issue(i.custom, e)) : e === !1 && t.length === 0 && t.push(issue(i.custom, n.check.default())), t.length > 0 ? t : null;
300
278
  }
301
279
  var Schema = class {
302
- state;
303
- _annotations = {};
304
- _typeValidator = null;
280
+ state = a;
281
+ get kind() {
282
+ return "unknown";
283
+ }
284
+ get description() {
285
+ return this.state.description;
286
+ }
287
+ get isOptional() {
288
+ return this.state.optional || this.state.default !== void 0 || this.state.catch !== void 0;
289
+ }
290
+ get isNullable() {
291
+ return this.state.nullable;
292
+ }
305
293
  get "~standard"() {
306
294
  return {
307
- validate: async (e) => {
308
- let t = await this.safeParseAsync(e);
309
- return t.success ? { value: t.data } : { issues: t.error.issues.map(({ message: e, path: t }) => ({
310
- message: e,
311
- path: t
312
- })) };
313
- },
295
+ validate: (e) => chain(this._run(e, {
296
+ async: !0,
297
+ messages: r.messages
298
+ }), ({ issues: e, value: t }) => e.length === 0 ? { value: t } : { issues: e.map(({ message: e, path: t }) => ({
299
+ message: e,
300
+ path: t
301
+ })) }),
314
302
  vendor: "vielzeug",
315
303
  version: 1
316
304
  };
317
305
  }
318
- constructor(e) {
319
- this.state = defaultState(), this._typeValidator = e ?? null;
320
- }
321
306
  parse(e, t) {
322
- if (this.state.hasAsyncChecks) throw new SpellValidationError([{
323
- code: r.custom,
324
- message: "parse() cannot evaluate async checks. Use parseAsync().",
325
- path: []
326
- }]);
327
- let n = t ?? _makeCtx();
328
- return this._withCatch(() => {
329
- let t = this._prepareInput(e);
330
- if (t.skip) return t.value;
331
- let r = this._parse(t.value, n), i = r.typeOk ? this._runValidatorsSync(r.data, n) : [], a = [...r.issues, ...i];
332
- if (a.length) throw new SpellValidationError(a);
333
- return this._runPostprocessors(r.data);
307
+ let n = this.safeParse(e, t);
308
+ if (n.success) return n.data;
309
+ throw n.error;
310
+ }
311
+ safeParse(e, t = r) {
312
+ let n = this._run(e, {
313
+ async: !1,
314
+ messages: t.messages
334
315
  });
335
- }
336
- safeParse(e, t) {
337
- try {
338
- return {
339
- data: this.parse(e, t),
340
- success: !0
341
- };
342
- } catch (e) {
343
- if (e instanceof SpellValidationError) return {
344
- error: e,
345
- success: !1
346
- };
347
- throw e;
348
- }
316
+ if (n instanceof Promise) throw asyncMisuse();
317
+ return toResult(n);
349
318
  }
350
319
  async parseAsync(e, t) {
351
- let n = t ?? _makeCtx();
352
- return this._withCatchAsync(async () => {
353
- let t = this._prepareInput(e);
354
- if (t.skip) return t.value;
355
- let r = await this._parseAsync(t.value, n), i = r.typeOk ? await this._runValidatorsAsync(r.data, n) : [], a = [...r.issues, ...i];
356
- if (a.length) throw new SpellValidationError(a);
357
- return this._runPostprocessors(r.data);
358
- });
320
+ let n = await this.safeParseAsync(e, t);
321
+ if (n.success) return n.data;
322
+ throw n.error;
323
+ }
324
+ async safeParseAsync(e, t = r) {
325
+ return toResult(await this._run(e, {
326
+ async: !0,
327
+ messages: t.messages
328
+ }));
359
329
  }
360
- async safeParseAsync(e, t) {
361
- try {
362
- return {
363
- data: await this.parseAsync(e, t),
364
- success: !0
330
+ is(e) {
331
+ return this.safeParse(e).success;
332
+ }
333
+ assert(e, t) {
334
+ let n = this.safeParse(e);
335
+ if (!n.success) throw t ? new SpellValidationError(n.error.issues.map((e) => e.path.length === 0 ? {
336
+ ...e,
337
+ message: `${t}: ${e.message}`
338
+ } : e)) : n.error;
339
+ }
340
+ _run(e, t) {
341
+ let { catch: n, default: r, nullable: i, optional: a, preprocess: o } = this.state, s = e;
342
+ for (let e of o) s = e(s);
343
+ if (s === void 0) {
344
+ if (r) return {
345
+ issues: [],
346
+ value: r()
365
347
  };
366
- } catch (e) {
367
- if (e instanceof SpellValidationError) return {
368
- error: e,
369
- success: !1
348
+ if (a) return {
349
+ issues: [],
350
+ value: s
370
351
  };
371
- throw e;
372
352
  }
373
- }
374
- _parseFullSync(e, t) {
375
- if (this.state.hasAsyncChecks) throw new SpellValidationError([{
376
- code: r.custom,
377
- message: "Sync parsing cannot evaluate async checks. Use parseAsync().",
378
- path: []
379
- }]);
380
- let n = t ?? _makeCtx(), i = this._prepareInput(e);
381
- if (i.skip) return {
382
- data: i.value,
383
- issues: []
353
+ if (s === null && i) return {
354
+ issues: [],
355
+ value: s
384
356
  };
385
- let a = this._parse(i.value, n), o = a.typeOk ? this._runValidatorsSync(a.data, n) : [], s = [...a.issues, ...o];
386
- return s.length > 0 ? this.state.catch ? {
387
- data: this.state.catch(),
388
- issues: []
389
- } : {
390
- data: a.data,
391
- issues: s
392
- } : {
393
- data: this._runPostprocessors(a.data),
394
- issues: []
357
+ let c = chain(this._parse(s, t), (e) => e.typeOk ? this._runSteps(e.value, e.issues, t, 0) : e);
358
+ return n ? chain(c, (e) => e.issues.length === 0 ? e : {
359
+ issues: [],
360
+ value: n()
361
+ }) : c;
362
+ }
363
+ _runSteps(e, t, n, r) {
364
+ let { steps: i } = this.state, a = e;
365
+ for (let e = r; e < i.length; e++) {
366
+ let r = i[e];
367
+ if (r.kind === "transform") {
368
+ if (t.length > 0) break;
369
+ a = r.run(a);
370
+ continue;
371
+ }
372
+ if (r.async && !n.async) throw asyncMisuse();
373
+ let o = r.run(a, n);
374
+ if (o instanceof Promise) {
375
+ if (!n.async) throw o.catch(() => {}), asyncMisuse();
376
+ let r = a;
377
+ return o.then((i) => this._runSteps(r, i ? [...t, ...i] : t, n, e + 1));
378
+ }
379
+ o && (t = [...t, ...o]);
380
+ }
381
+ return {
382
+ issues: t,
383
+ value: a
395
384
  };
396
385
  }
397
- async _parseFullAsync(e, t) {
398
- let n = t ?? _makeCtx(), r = this._prepareInput(e);
399
- if (r.skip) return {
400
- data: r.value,
401
- issues: []
402
- };
403
- let i = await this._parseAsync(r.value, n), a = i.typeOk ? await this._runValidatorsAsync(i.data, n) : [], o = [...i.issues, ...a];
404
- return o.length > 0 ? this.state.catch ? {
405
- data: this.state.catch(),
406
- issues: []
407
- } : {
408
- data: i.data,
409
- issues: o
410
- } : {
411
- data: this._runPostprocessors(i.data),
412
- issues: []
413
- };
386
+ _parse(e, t) {
387
+ return ok(e);
414
388
  }
415
389
  check(e) {
416
390
  return this._addCheck(e, !1);
@@ -419,238 +393,127 @@ var Schema = class {
419
393
  return this._addCheck(e, !0);
420
394
  }
421
395
  _addCheck(e, t) {
422
- let validator = (t, n) => {
423
- let r = [], i = e(t, { addIssue: (e) => r.push({
424
- ...e,
425
- path: e.path ?? []
426
- }) });
427
- return i instanceof Promise ? i.then((e) => normalizeValidateResult(e, r, n)) : normalizeValidateResult(i, r, n);
428
- }, n = this._addConstraint(validator);
429
- return n.state.hasRuntimeChecks = !0, t && (n.state.hasAsyncChecks = !0), n;
396
+ let run = (t, n) => {
397
+ let r = [];
398
+ return chain(e(t, { addIssue: ({ path: e = [], ...t }) => r.push({
399
+ ...t,
400
+ path: e
401
+ }) }), (e) => checkIssues(e, r, n.messages));
402
+ };
403
+ return this._with({
404
+ portable: !1,
405
+ steps: [...this.state.steps, {
406
+ async: t,
407
+ kind: "check",
408
+ run
409
+ }]
410
+ });
411
+ }
412
+ _refine(e, t, n) {
413
+ let run = (n, r) => e(n) ? null : [t(n, r.messages)];
414
+ return this._with({
415
+ meta: n ? {
416
+ ...this.state.meta,
417
+ ...n(this.state.meta)
418
+ } : this.state.meta,
419
+ steps: [...this.state.steps, {
420
+ async: !1,
421
+ kind: "check",
422
+ run
423
+ }]
424
+ });
430
425
  }
431
426
  optional() {
432
- let e = this._clone();
433
- return e.state.isOptional = !0, e;
427
+ return this._with({ optional: !0 });
434
428
  }
435
429
  nullable() {
436
- let e = this._clone();
437
- return e.state.isNullable = !0, e;
430
+ return this._with({ nullable: !0 });
438
431
  }
439
432
  nullish() {
440
- let e = this._clone();
441
- return e.state.isOptional = !0, e.state.isNullable = !0, e;
433
+ return this._with({
434
+ nullable: !0,
435
+ optional: !0
436
+ });
442
437
  }
443
438
  required() {
444
- let e = this._clone();
445
- return e.state.isOptional = !1, e;
439
+ return this._with({
440
+ default: void 0,
441
+ optional: !1
442
+ });
446
443
  }
447
444
  default(e) {
448
- let t = this._clone();
449
- return t.state.defaultValue = typeof e == "function" ? e : () => materializeValue(e), t;
445
+ let t = typeof e == "function" ? e : () => materialize(e);
446
+ return this._with({
447
+ default: t,
448
+ portable: !1
449
+ });
450
450
  }
451
451
  catch(e) {
452
- let t = this._clone();
453
- return t.state.catch = typeof e == "function" ? e : () => materializeValue(e), t;
452
+ let t = typeof e == "function" ? e : () => materialize(e);
453
+ return this._with({
454
+ catch: t,
455
+ portable: !1
456
+ });
454
457
  }
455
458
  transform(e) {
456
- let t = this._clone();
457
- return t.state.postprocessors.push(e), t;
459
+ let t = {
460
+ kind: "transform",
461
+ run: e
462
+ };
463
+ return this._with({
464
+ portable: !1,
465
+ steps: [...this.state.steps, t]
466
+ });
458
467
  }
459
468
  preprocess(e) {
460
- let t = this._clone();
461
- return t.state.preprocessors.push(e), t;
469
+ return this._with({
470
+ portable: !1,
471
+ preprocess: [...this.state.preprocess, e]
472
+ });
462
473
  }
463
474
  pipe(e) {
464
475
  return new PipeSchema(this, e);
465
476
  }
466
- label(e) {
467
- let t = this._clone();
468
- return t.state.description = e, t;
477
+ describe(e) {
478
+ return this._with({ description: e });
469
479
  }
470
480
  definition() {
471
- if (this.state.catch || this.state.defaultValue || this.state.postprocessors.length > 0 || this.state.preprocessors.length > 0 || this.state.hasRuntimeChecks) throw new SpellDefinitionError("Schemas with checks, transforms, defaults, catches, or preprocessors have no declarative definition.");
472
- return deepFreeze(this._toDescriptorImpl());
473
- }
474
- get description() {
475
- return this.state.description;
476
- }
477
- get isOptional() {
478
- return this.state.isOptional;
479
- }
480
- get isNullable() {
481
- return this.state.isNullable;
482
- }
483
- is(e) {
484
- return this.safeParse(e).success;
485
- }
486
- assert(e, t) {
487
- let n = this._parseFullSync(e);
488
- if (n.issues.length !== 0) throw new SpellValidationError(t ? n.issues.map((e) => ({
489
- ...e,
490
- message: e.path.length === 0 ? `${t}: ${e.message}` : e.message
491
- })) : n.issues);
492
- }
493
- get kind() {
494
- return this._kind;
495
- }
496
- get _kind() {
497
- return "unknown";
498
- }
499
- _describeBase() {
500
- return {
501
- ...this.state.description === void 0 ? {} : { description: this.state.description },
502
- ...this.state.isNullable ? { isNullable: !0 } : {},
503
- ...this.state.isOptional ? { isOptional: !0 } : {}
504
- };
505
- }
506
- _addConstraint(e, t) {
507
- let n = this._clone();
508
- return n.state.validators.push(e), t && (n._annotations = t({ ...n._annotations })), n;
509
- }
510
- _construct(e) {
511
- let t = Object.assign(Object.create(Object.getPrototypeOf(this)), this, { state: e });
512
- return t._annotations = { ...this._annotations }, t;
481
+ if (!this.state.portable) throw new SpellDefinitionError("Schemas with custom checks, transforms, preprocessors, defaults, or catches have no declarative definition.");
482
+ return deepFreeze(this._describe());
513
483
  }
514
- _clone() {
515
- return this._construct(cloneState(this.state));
516
- }
517
- _copyStateTo(e) {
518
- return e.state = cloneState(this.state), e;
519
- }
520
- _toDescriptorImpl() {
484
+ _describe() {
521
485
  return {
522
486
  ...this._describeBase(),
523
487
  kind: "unknown"
524
488
  };
525
489
  }
526
- _withCatch(e) {
527
- if (!this.state.catch) return e();
528
- try {
529
- return e();
530
- } catch (e) {
531
- if (e instanceof SpellValidationError) return this.state.catch();
532
- throw e;
533
- }
534
- }
535
- async _withCatchAsync(e) {
536
- if (!this.state.catch) return e();
537
- try {
538
- return await e();
539
- } catch (e) {
540
- if (e instanceof SpellValidationError) return this.state.catch();
541
- throw e;
542
- }
543
- }
544
- _prepareInput(e) {
545
- let t = this._runPreprocessors(e), n = t === void 0 && this.state.defaultValue ? this.state.defaultValue() : t;
546
- return this.state.isOptional && n === void 0 || this.state.isNullable && n === null ? {
547
- skip: !0,
548
- value: n
549
- } : {
550
- skip: !1,
551
- value: n
552
- };
553
- }
554
- _runPreprocessors(e) {
555
- let t = e;
556
- for (let e of this.state.preprocessors) t = e(t);
557
- return t;
558
- }
559
- _runPostprocessors(e) {
560
- let t = e;
561
- for (let e of this.state.postprocessors) t = e(t);
562
- return t;
563
- }
564
- _runValidatorsSync(e, t) {
565
- if (this._typeValidator) {
566
- let n = this._typeValidator(e, t);
567
- if (n instanceof Promise) throw new SpellValidationError([{
568
- code: r.custom,
569
- message: "Type validator returned a Promise. Use parseAsync() for async validation.",
570
- path: []
571
- }]);
572
- if (n && n.length > 0) return n;
573
- }
574
- let n = [];
575
- for (let i of this.state.validators) {
576
- let a = i(e, t);
577
- if (a instanceof Promise) throw new SpellValidationError([{
578
- code: r.custom,
579
- message: "Sync parsing received an async check. Use checkAsync() and parseAsync().",
580
- path: []
581
- }]);
582
- a && n.push(...a);
583
- }
584
- return n;
585
- }
586
- async _runValidatorsAsync(e, t) {
587
- if (this._typeValidator) {
588
- let n = await this._typeValidator(e, t);
589
- if (n && n.length > 0) return n;
590
- }
591
- let n = [];
592
- for (let r of this.state.validators) {
593
- let i = await r(e, t);
594
- i && n.push(...i);
595
- }
596
- return n;
597
- }
598
- _parse(e, t) {
490
+ _describeBase() {
491
+ let { description: e, nullable: t, optional: n } = this.state;
599
492
  return {
600
- data: e,
601
- issues: [],
602
- typeOk: !0
493
+ ...e !== void 0 && { description: e },
494
+ ...t && { isNullable: !0 },
495
+ ...n && { isOptional: !0 }
603
496
  };
604
497
  }
605
- _parseAsync(e, t) {
606
- return Promise.resolve(this._parse(e, t));
498
+ _with(e) {
499
+ return Object.assign(Object.create(Object.getPrototypeOf(this)), this, { state: {
500
+ ...this.state,
501
+ ...e
502
+ } });
607
503
  }
608
504
  }, PipeSchema = class extends Schema {
609
505
  from;
610
506
  to;
611
- get _kind() {
612
- return "pipe";
613
- }
614
507
  constructor(e, t) {
615
508
  super(), this.from = e, this.to = t;
616
509
  }
617
- _parse(e, t) {
618
- let n = this.from._parseFullSync(e, t);
619
- if (n.issues.length > 0) return {
620
- data: e,
621
- issues: n.issues,
622
- typeOk: !1
623
- };
624
- let r = this.to._parseFullSync(n.data, t);
625
- return r.issues.length > 0 ? {
626
- data: n.data,
627
- issues: r.issues,
628
- typeOk: !1
629
- } : {
630
- data: r.data,
631
- issues: [],
632
- typeOk: !0
633
- };
510
+ get kind() {
511
+ return "pipe";
634
512
  }
635
- async _parseAsync(e, t) {
636
- let n = await this.from._parseFullAsync(e, t);
637
- if (n.issues.length > 0) return {
638
- data: e,
639
- issues: n.issues,
640
- typeOk: !1
641
- };
642
- let r = await this.to._parseFullAsync(n.data, t);
643
- return r.issues.length > 0 ? {
644
- data: n.data,
645
- issues: r.issues,
646
- typeOk: !1
647
- } : {
648
- data: r.data,
649
- issues: [],
650
- typeOk: !0
651
- };
513
+ _parse(e, t) {
514
+ return chain(this.from._run(e, t), (n) => n.issues.length > 0 ? invalid(e, ...n.issues) : chain(this.to._run(n.value, t), (e) => e.issues.length > 0 ? invalid(n.value, ...e.issues) : ok(e.value)));
652
515
  }
653
- _toDescriptorImpl() {
516
+ _describe() {
654
517
  return {
655
518
  ...this._describeBase(),
656
519
  from: this.from.definition(),
@@ -660,943 +523,535 @@ var Schema = class {
660
523
  }
661
524
  }, ArraySchema = class extends Schema {
662
525
  itemSchema;
663
- get _kind() {
664
- return "array";
665
- }
666
526
  constructor(e) {
667
527
  super(), this.itemSchema = e;
668
528
  }
669
- _parseItemsSync(e, t) {
670
- let n = [], r = [];
671
- for (let i = 0; i < e.length; i++) {
672
- let a = this.itemSchema._parseFullSync(e[i], t);
673
- a.issues.length === 0 ? r.push(a.data) : (n.push(...prependIssuePath(a.issues, i)), r.push(e[i]));
674
- }
675
- return {
676
- data: r,
677
- issues: n
678
- };
529
+ get kind() {
530
+ return "array";
679
531
  }
680
532
  _parse(e, t) {
681
- if (!Array.isArray(e)) return {
682
- data: e,
683
- issues: fail(r.invalid_type, t.messages.array.type()),
684
- typeOk: !1
685
- };
686
- let { data: n, issues: i } = this._parseItemsSync(e, t);
687
- return {
688
- data: n,
689
- issues: i,
690
- typeOk: !0
691
- };
692
- }
693
- async _parseAsync(e, t) {
694
- if (!Array.isArray(e)) return {
695
- data: e,
696
- issues: fail(r.invalid_type, t.messages.array.type()),
697
- typeOk: !1
698
- };
699
- let n = await Promise.all(e.map((e) => this.itemSchema._parseFullAsync(e, t))), i = [], a = [];
700
- for (let t = 0; t < n.length; t++) {
701
- let r = n[t];
702
- r.issues.length === 0 ? a.push(r.data) : (i.push(...prependIssuePath(r.issues, t)), a.push(e[t]));
703
- }
704
- return {
705
- data: a,
706
- issues: i,
707
- typeOk: !0
708
- };
533
+ return Array.isArray(e) ? chain(all(e.map((e) => this.itemSchema._run(e, t))), (t) => {
534
+ let n = [];
535
+ return {
536
+ issues: n,
537
+ typeOk: !0,
538
+ value: t.map((t, r) => t.issues.length === 0 ? t.value : (n.push(...prefixIssues(t.issues, r)), e[r]))
539
+ };
540
+ }) : invalid(e, issue(i.invalid_type, t.messages.array.type()));
709
541
  }
710
542
  min(e, t) {
711
- return this._addConstraint((n, i) => n.length >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.array.min, {
543
+ return this._refine((t) => t.length >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.array.min, {
712
544
  min: e,
713
545
  value: n
714
- }), { min: e }), (t) => {
715
- let n = t;
716
- return {
717
- ...n,
718
- minItems: n.minItems === void 0 ? e : Math.max(n.minItems, e)
719
- };
720
- });
546
+ }), { min: e }), (t) => ({ minItems: atLeast(t.minItems, e) }));
721
547
  }
722
548
  max(e, t) {
723
- return this._addConstraint((n, i) => n.length <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.array.max, {
549
+ return this._refine((t) => t.length <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.array.max, {
724
550
  max: e,
725
551
  value: n
726
- }), { max: e }), (t) => {
727
- let n = t;
728
- return {
729
- ...n,
730
- maxItems: n.maxItems === void 0 ? e : Math.min(n.maxItems, e)
731
- };
732
- });
552
+ }), { max: e }), (t) => ({ maxItems: atMost(t.maxItems, e) }));
733
553
  }
734
554
  length(e, t) {
735
- return this._addConstraint((n, i) => n.length === e ? null : fail(r.invalid_length, resolveMessage(t ?? i.messages.array.length, {
555
+ return this._refine((t) => t.length === e, (n, r) => issue(i.invalid_length, resolveMessage(t ?? r.array.length, {
736
556
  exact: e,
737
557
  value: n
738
558
  }), { exact: e }), (t) => ({
739
- ...t,
740
- maxItems: e,
741
- minItems: e
559
+ maxItems: atMost(t.maxItems, e),
560
+ minItems: atLeast(t.minItems, e)
742
561
  }));
743
562
  }
744
563
  nonEmpty(e) {
745
- return this._addConstraint((t, n) => {
746
- let i = t;
747
- return i.length > 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.array.nonEmpty, {
748
- min: 1,
749
- value: i
750
- }), { min: 1 });
751
- });
564
+ return this._refine((e) => e.length > 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.array.nonEmpty, { value: t }), { min: 1 }), (e) => ({ minItems: atLeast(e.minItems, 1) }));
752
565
  }
753
- unique(e, t) {
754
- let n = typeof e == "function" && e.length === 2, i = n ? e : null, a = n ? t : typeof e == "function" ? e : void 0;
755
- return this._addConstraint((e, t) => {
756
- let n = e, o;
757
- return o = i ? n.every((e, t) => n.slice(0, t).every((t) => !i(e, t))) : new Set(n).size === n.length, o ? null : fail(r.invalid_unique, resolveMessage(a ?? t.messages.array.unique, { value: n }), { unique: !0 });
758
- });
566
+ unique(e = {}) {
567
+ let { by: t, message: n } = e, r = this._refine((e) => new Set(t ? e.map(t) : e).size === e.length, (e, t) => issue(i.invalid_unique, resolveMessage(n ?? t.array.unique, { value: e })), t ? void 0 : () => ({ uniqueItems: !0 }));
568
+ return t ? r._with({ portable: !1 }) : r;
759
569
  }
760
- _toDescriptorImpl() {
761
- let e = this._annotations;
570
+ _describe() {
762
571
  return {
763
572
  ...this._describeBase(),
764
- ...e.maxItems === void 0 ? {} : { maxItems: e.maxItems },
765
- ...e.minItems === void 0 ? {} : { minItems: e.minItems },
573
+ ...this.state.meta,
766
574
  items: this.itemSchema.definition(),
767
575
  kind: "array"
768
576
  };
769
577
  }
770
- }, i = class BigIntSchema extends Schema {
771
- get _kind() {
578
+ }, BigIntSchema = class extends Schema {
579
+ get kind() {
772
580
  return "bigint";
773
581
  }
774
- constructor() {
775
- super((e, t) => typeof e == "bigint" ? null : fail(r.invalid_type, t.messages.bigint.type()));
582
+ _parse(e, t) {
583
+ return typeof e == "bigint" ? ok(e) : invalid(e, issue(i.invalid_type, t.messages.bigint.type()));
776
584
  }
777
585
  min(e, t) {
778
- return this._addConstraint((n, i) => {
779
- let a = n;
780
- return a >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.bigint.min, {
781
- min: e,
782
- value: a
783
- }), { min: e });
784
- });
586
+ return this._refine((t) => t >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.bigint.min, {
587
+ min: e,
588
+ value: n
589
+ }), { min: e }), (t) => ({ minimum: atLeast(t.minimum, e) }));
785
590
  }
786
591
  max(e, t) {
787
- return this._addConstraint((n, i) => {
788
- let a = n;
789
- return a <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.bigint.max, {
790
- max: e,
791
- value: a
792
- }), { max: e });
793
- });
592
+ return this._refine((t) => t <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.bigint.max, {
593
+ max: e,
594
+ value: n
595
+ }), { max: e }), (t) => ({ maximum: atMost(t.maximum, e) }));
794
596
  }
795
597
  positive(e) {
796
- return this._addConstraint((t, n) => {
797
- let i = t;
798
- return i > 0n ? null : fail(r.too_small, resolveMessage(e ?? n.messages.bigint.positive, { value: i }), {
799
- exclusive: !0,
800
- min: 0n
801
- });
802
- });
598
+ return this._refine((e) => e > 0n, (t, n) => issue(i.too_small, resolveMessage(e ?? n.bigint.positive, { value: t }), {
599
+ exclusive: !0,
600
+ min: 0n
601
+ }), (e) => ({ exclusiveMinimum: atLeast(e.exclusiveMinimum, 0n) }));
803
602
  }
804
603
  negative(e) {
805
- return this._addConstraint((t, n) => {
806
- let i = t;
807
- return i < 0n ? null : fail(r.too_big, resolveMessage(e ?? n.messages.bigint.negative, { value: i }), {
808
- exclusive: !0,
809
- max: 0n
810
- });
811
- });
604
+ return this._refine((e) => e < 0n, (t, n) => issue(i.too_big, resolveMessage(e ?? n.bigint.negative, { value: t }), {
605
+ exclusive: !0,
606
+ max: 0n
607
+ }), (e) => ({ exclusiveMaximum: atMost(e.exclusiveMaximum, 0n) }));
812
608
  }
813
609
  nonNegative(e) {
814
- return this._addConstraint((t, n) => {
815
- let i = t;
816
- return i >= 0n ? null : fail(r.too_small, resolveMessage(e ?? n.messages.bigint.nonNegative, { value: i }), { min: 0n });
817
- });
610
+ return this._refine((e) => e >= 0n, (t, n) => issue(i.too_small, resolveMessage(e ?? n.bigint.nonNegative, { value: t }), { min: 0n }), (e) => ({ minimum: atLeast(e.minimum, 0n) }));
818
611
  }
819
612
  nonPositive(e) {
820
- return this._addConstraint((t, n) => {
821
- let i = t;
822
- return i <= 0n ? null : fail(r.too_big, resolveMessage(e ?? n.messages.bigint.nonPositive, { value: i }), { max: 0n });
823
- });
613
+ return this._refine((e) => e <= 0n, (t, n) => issue(i.too_big, resolveMessage(e ?? n.bigint.nonPositive, { value: t }), { max: 0n }), (e) => ({ maximum: atMost(e.maximum, 0n) }));
824
614
  }
825
615
  multipleOf(e, t) {
826
- return this._addConstraint((n, i) => {
827
- let a = n;
828
- return a % e === 0n ? null : fail(r.invalid_multiple_of, resolveMessage(t ?? i.messages.bigint.multipleOf, {
829
- step: e,
830
- value: a
831
- }), { step: e });
832
- });
833
- }
834
- _toDescriptorImpl() {
835
- return this.state.validators.length, {
836
- ...this._describeBase(),
837
- kind: "bigint"
838
- };
839
- }
840
- static coerce() {
841
- return new BigIntSchema().preprocess((e) => {
842
- if (typeof e == "bigint") return e;
843
- if (typeof e == "number") {
844
- if (!Number.isFinite(e) || !Number.isInteger(e)) return e;
845
- try {
846
- return BigInt(e);
847
- } catch {
848
- return e;
849
- }
850
- }
851
- if (typeof e == "string") {
852
- let t = e.trim();
853
- if (t.length === 0 || t.length > 1e3) return e;
854
- try {
855
- return BigInt(t);
856
- } catch {
857
- return e;
858
- }
859
- }
860
- return e;
861
- });
862
- }
863
- }, a = class BooleanSchema extends Schema {
864
- get _kind() {
865
- return "boolean";
866
- }
867
- constructor() {
868
- super((e, t) => typeof e == "boolean" ? null : [{
869
- code: r.invalid_type,
870
- message: t.messages.boolean.type(),
871
- path: []
872
- }]);
616
+ return this._refine((t) => e !== 0n && t % e === 0n, (n, r) => issue(i.invalid_multiple_of, resolveMessage(t ?? r.bigint.multipleOf, {
617
+ step: e,
618
+ value: n
619
+ }), { step: e }), () => ({ multipleOf: e }));
873
620
  }
874
- _toDescriptorImpl() {
621
+ _describe() {
875
622
  return {
876
623
  ...this._describeBase(),
877
- kind: "boolean"
624
+ ...this.state.meta,
625
+ kind: "bigint"
878
626
  };
879
627
  }
880
- static coerce() {
881
- return new BooleanSchema().preprocess((e) => typeof e == "boolean" ? e : e === "true" || e === "1" || e === 1 ? !0 : e === "false" || e === "0" || e === 0 ? !1 : e);
882
- }
883
- }, o = class DateSchema extends Schema {
884
- get _kind() {
628
+ }, DateSchema = class extends Schema {
629
+ get kind() {
885
630
  return "date";
886
631
  }
887
- constructor() {
888
- super((e, t) => e instanceof Date && !Number.isNaN(e.getTime()) ? null : fail(r.invalid_date, t.messages.date.type()));
632
+ _parse(e, t) {
633
+ return e instanceof Date && !Number.isNaN(e.getTime()) ? ok(e) : invalid(e, issue(i.invalid_date, t.messages.date.type()));
889
634
  }
890
635
  min(e, t) {
891
- return this._addConstraint((n, i) => {
892
- let a = n;
893
- return a >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.date.min, {
894
- min: e,
895
- value: a
896
- }), { min: e });
897
- });
636
+ return this._refine((t) => t >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.date.min, {
637
+ min: e,
638
+ value: n
639
+ }), { min: e }), (t) => ({ minimum: t.minimum === void 0 || e > new Date(t.minimum) ? e.toISOString() : t.minimum }));
898
640
  }
899
641
  max(e, t) {
900
- return this._addConstraint((n, i) => {
901
- let a = n;
902
- return a <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.date.max, {
903
- max: e,
904
- value: a
905
- }), { max: e });
906
- });
642
+ return this._refine((t) => t <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.date.max, {
643
+ max: e,
644
+ value: n
645
+ }), { max: e }), (t) => ({ maximum: t.maximum === void 0 || e < new Date(t.maximum) ? e.toISOString() : t.maximum }));
907
646
  }
908
- _toDescriptorImpl() {
647
+ _describe() {
909
648
  return {
910
649
  ...this._describeBase(),
650
+ ...this.state.meta,
911
651
  kind: "date"
912
652
  };
913
653
  }
914
- static coerce() {
915
- return new DateSchema().preprocess((e) => e instanceof Date ? e : typeof e == "string" || typeof e == "number" ? new Date(e) : e);
916
- }
917
- };
918
- // src/schemas/enum.ts
919
- function buildEnumValidator(e) {
920
- let t = new Set(e);
921
- return (n, i) => t.has(n) ? null : [{
922
- code: r.invalid_enum,
923
- message: i.messages.enum.invalid({ values: e }),
924
- params: { values: e },
925
- path: []
926
- }];
927
- }
928
- var EnumSchema = class extends Schema {
929
- values;
930
- get _kind() {
931
- return "enum";
654
+ }, BooleanSchema = class extends Schema {
655
+ get kind() {
656
+ return "boolean";
932
657
  }
933
- constructor(e) {
934
- super(buildEnumValidator(e)), this.values = e;
658
+ _parse(e, t) {
659
+ return typeof e == "boolean" ? ok(e) : invalid(e, issue(i.invalid_type, t.messages.boolean.type()));
935
660
  }
936
- _toDescriptorImpl() {
661
+ _describe() {
937
662
  return {
938
663
  ...this._describeBase(),
939
- kind: "enum",
940
- values: this.values
664
+ kind: "boolean"
941
665
  };
942
666
  }
943
- }, InstanceOfSchema = class extends Schema {
944
- cls;
945
- get _kind() {
946
- return "instanceof";
947
- }
667
+ }, LiteralSchema = class extends Schema {
668
+ value;
948
669
  constructor(e) {
949
- super((t, n) => t instanceof e ? null : [{
950
- code: r.invalid_type,
951
- message: n.messages.instanceof.type({ className: e.name }),
952
- path: []
953
- }]), this.cls = e;
670
+ super(), this.value = e;
954
671
  }
955
- _toDescriptorImpl() {
956
- return {
957
- ...this._describeBase(),
958
- className: this.cls.name,
959
- kind: "instanceof"
960
- };
961
- }
962
- };
963
- // src/schemas/intersect.ts
964
- function deepMerge(t, n) {
965
- if (e(t) && e(n)) {
966
- let e = cloneRecord(t);
967
- for (let [r, i] of Object.entries(n)) defineOwnProperty(e, r, deepMerge(t[r], i));
968
- return e;
969
- }
970
- return n;
971
- }
972
- var IntersectSchema = class extends Schema {
973
- schemas;
974
- get _kind() {
975
- return "intersect";
976
- }
977
- constructor(e) {
978
- super(), this.schemas = Object.freeze([...e]);
672
+ get kind() {
673
+ return "literal";
979
674
  }
980
675
  _parse(e, t) {
981
- let n = {
982
- hasOutput: !1,
983
- issues: [],
984
- output: e
985
- };
986
- for (let r = 0; r < this.schemas.length; r++) {
987
- let i = this.schemas[r]._parseFullSync(e, t);
988
- i.issues.length > 0 ? n.issues.push(...i.issues) : n.hasOutput ? n.output = deepMerge(n.output, i.data) : (n.hasOutput = !0, n.output = i.data);
989
- }
990
- return {
991
- data: n.output,
992
- issues: n.issues,
993
- typeOk: n.issues.length === 0
994
- };
995
- }
996
- async _parseAsync(e, t) {
997
- let n = {
998
- hasOutput: !1,
999
- issues: [],
1000
- output: e
1001
- };
1002
- for (let r = 0; r < this.schemas.length; r++) {
1003
- let i = await this.schemas[r]._parseFullAsync(e, t);
1004
- i.issues.length > 0 ? n.issues.push(...i.issues) : n.hasOutput ? n.output = deepMerge(n.output, i.data) : (n.hasOutput = !0, n.output = i.data);
1005
- }
1006
- return {
1007
- data: n.output,
1008
- issues: n.issues,
1009
- typeOk: n.issues.length === 0
1010
- };
676
+ return e === this.value ? ok(e) : invalid(e, issue(i.invalid_literal, t.messages.literal.expected({ expected: this.value }), { expected: this.value }));
1011
677
  }
1012
- _toDescriptorImpl() {
678
+ _describe() {
1013
679
  return {
1014
680
  ...this._describeBase(),
1015
- branches: this.schemas.map((e) => e.definition()),
1016
- kind: "intersect"
681
+ kind: "literal",
682
+ value: this.value
1017
683
  };
1018
684
  }
1019
- }, s = class LazySchema extends Schema {
1020
- _getter;
1021
- _resolved;
1022
- get _kind() {
1023
- return "lazy";
1024
- }
685
+ }, EnumSchema = class extends Schema {
686
+ values;
687
+ _allowed;
1025
688
  constructor(e) {
1026
- super(), this._getter = e;
689
+ super(), this.values = Object.freeze([...e]), this._allowed = new Set(e);
1027
690
  }
1028
- _resolve() {
1029
- return this._resolved ??= this._getter();
691
+ get kind() {
692
+ return "enum";
1030
693
  }
1031
694
  _parse(e, t) {
1032
- let n = this._resolve()._parseFullSync(e, t);
1033
- return n.issues.length === 0 ? {
1034
- data: n.data,
1035
- issues: [],
1036
- typeOk: !0
1037
- } : {
1038
- data: e,
1039
- issues: n.issues,
1040
- typeOk: !0
1041
- };
695
+ return this._allowed.has(e) ? ok(e) : invalid(e, issue(i.invalid_enum, t.messages.enum.invalid({ values: this.values }), { values: this.values }));
1042
696
  }
1043
- async _parseAsync(e, t) {
1044
- let n = await this._resolve()._parseFullAsync(e, t);
1045
- return n.issues.length === 0 ? {
1046
- data: n.data,
1047
- issues: [],
1048
- typeOk: !0
1049
- } : {
1050
- data: e,
1051
- issues: n.issues,
1052
- typeOk: !0
1053
- };
1054
- }
1055
- _toDescriptorImpl() {
697
+ _describe() {
1056
698
  return {
1057
699
  ...this._describeBase(),
1058
- kind: "lazy"
700
+ kind: "enum",
701
+ values: this.values
1059
702
  };
1060
703
  }
1061
- _construct(e) {
1062
- let t = new LazySchema(this._getter);
1063
- return t.state = e, t;
1064
- }
1065
- }, LiteralSchema = class extends Schema {
1066
- value;
1067
- get _kind() {
1068
- return "literal";
1069
- }
704
+ }, InstanceOfSchema = class extends Schema {
705
+ cls;
1070
706
  constructor(e) {
1071
- super((t, n) => t === e ? null : [{
1072
- code: r.invalid_literal,
1073
- message: n.messages.literal.expected({ expected: e }),
1074
- params: { expected: e },
1075
- path: []
1076
- }]), this.value = e;
1077
- }
1078
- _toDescriptorImpl() {
1079
- return {
1080
- ...this._describeBase(),
1081
- kind: "literal",
1082
- value: this.value
1083
- };
707
+ super(), this.cls = e;
1084
708
  }
1085
- }, MapSchema = class extends Schema {
1086
- keySchema;
1087
- valueSchema;
1088
- get _kind() {
1089
- return "map";
1090
- }
1091
- constructor(e, t) {
1092
- super(), this.keySchema = e, this.valueSchema = t;
709
+ get kind() {
710
+ return "instanceof";
1093
711
  }
1094
712
  _parse(e, t) {
1095
- if (!(e instanceof Map)) return {
1096
- data: e,
1097
- issues: [{
1098
- code: r.invalid_type,
1099
- message: t.messages.map.type(),
1100
- path: []
1101
- }],
1102
- typeOk: !1
1103
- };
1104
- let n = /* @__PURE__ */ new Map(), i = [], a = 0;
1105
- for (let [r, o] of e) {
1106
- let e = this.keySchema._parseFullSync(r, t), s = this.valueSchema._parseFullSync(o, t);
1107
- e.issues.length > 0 && i.push(...prependIssuePath(e.issues, a)), s.issues.length > 0 && i.push(...prependIssuePath(s.issues, a)), e.issues.length === 0 && s.issues.length === 0 && n.set(e.data, s.data), a += 1;
1108
- }
1109
- return {
1110
- data: n,
1111
- issues: i,
1112
- typeOk: !0
1113
- };
1114
- }
1115
- async _parseAsync(e, t) {
1116
- if (!(e instanceof Map)) return {
1117
- data: e,
1118
- issues: [{
1119
- code: r.invalid_type,
1120
- message: t.messages.map.type(),
1121
- path: []
1122
- }],
1123
- typeOk: !1
1124
- };
1125
- let n = [...e], i = await Promise.all(n.map(([e, n]) => Promise.all([this.keySchema._parseFullAsync(e, t), this.valueSchema._parseFullAsync(n, t)]))), a = [], o = /* @__PURE__ */ new Map();
1126
- for (let e = 0; e < i.length; e++) {
1127
- let [t, n] = i[e];
1128
- t.issues.length > 0 && a.push(...prependIssuePath(t.issues, e)), n.issues.length > 0 && a.push(...prependIssuePath(n.issues, e)), t.issues.length === 0 && n.issues.length === 0 && o.set(t.data, n.data);
1129
- }
1130
- return {
1131
- data: o,
1132
- issues: a,
1133
- typeOk: !0
1134
- };
1135
- }
1136
- min(e, t) {
1137
- return this._addConstraint((n, i) => {
1138
- let a = n;
1139
- return a.size >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.map.min, {
1140
- min: e,
1141
- value: a
1142
- }), { min: e });
1143
- });
1144
- }
1145
- max(e, t) {
1146
- return this._addConstraint((n, i) => {
1147
- let a = n;
1148
- return a.size <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.map.max, {
1149
- max: e,
1150
- value: a
1151
- }), { max: e });
1152
- });
1153
- }
1154
- size(e, t) {
1155
- return this._addConstraint((n, i) => {
1156
- let a = n;
1157
- return a.size === e ? null : fail(r.invalid_length, resolveMessage(t ?? i.messages.map.size, {
1158
- exact: e,
1159
- value: a
1160
- }), { exact: e });
1161
- });
1162
- }
1163
- nonEmpty(e) {
1164
- return this._addConstraint((t, n) => t.size > 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.map.nonEmpty, { min: 1 }), { min: 1 }));
713
+ return e instanceof this.cls ? ok(e) : invalid(e, issue(i.invalid_type, t.messages.instanceof.type({ className: this.cls.name })));
1165
714
  }
1166
- _toDescriptorImpl() {
715
+ _describe() {
1167
716
  return {
1168
717
  ...this._describeBase(),
1169
- key: this.keySchema.definition(),
1170
- kind: "map",
1171
- value: this.valueSchema.definition()
718
+ className: this.cls.name,
719
+ kind: "instanceof"
1172
720
  };
1173
721
  }
1174
722
  }, NeverSchema = class extends Schema {
1175
- get _kind() {
723
+ get kind() {
1176
724
  return "never";
1177
725
  }
1178
- constructor() {
1179
- super((e, t) => [{
1180
- code: r.invalid_type,
1181
- message: t.messages.never.invalid(),
1182
- path: []
1183
- }]);
726
+ _parse(e, t) {
727
+ return invalid(e, issue(i.invalid_type, t.messages.never.invalid()));
1184
728
  }
1185
- _toDescriptorImpl() {
729
+ _describe() {
1186
730
  return {
1187
731
  ...this._describeBase(),
1188
732
  kind: "never"
1189
733
  };
1190
734
  }
1191
- }, c = class NumberSchema extends Schema {
1192
- get _kind() {
1193
- return "number";
735
+ }, o = class ObjectSchema extends Schema {
736
+ shape;
737
+ _unknownKeys;
738
+ _tolerated;
739
+ constructor(e, t = "strict", n = []) {
740
+ super(), this.shape = Object.freeze(objectFromEntries(Object.entries(e))), this._unknownKeys = t, this._tolerated = Object.freeze([...new Set(n)]);
1194
741
  }
1195
- constructor() {
1196
- super((e, t) => typeof e == "number" && !Number.isNaN(e) ? null : fail(r.invalid_type, t.messages.number.type()));
742
+ get kind() {
743
+ return "object";
1197
744
  }
1198
- min(e, t) {
1199
- return this._addConstraint((n, i) => n >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.number.min, {
1200
- min: e,
1201
- value: n
1202
- }), { min: e }), (t) => {
1203
- let n = t;
745
+ _parse(e, t) {
746
+ if (typeof e != "object" || !e || Array.isArray(e)) return invalid(e, issue(i.invalid_type, t.messages.object.type()));
747
+ let n = e, r = Object.keys(this.shape), a = Object.keys(n).filter((e) => !Object.hasOwn(this.shape, e) && !this._tolerated.includes(e));
748
+ return chain(all(r.map((e) => this.shape[e]._run(n[e], t))), (e) => {
749
+ let o = [], s = {};
750
+ if (a.length > 0 && this._unknownKeys === "strict" && o.push(issue(i.invalid_keys, t.messages.object.invalidKeys({ keys: a }), { keys: a })), e.forEach((e, t) => {
751
+ let i = r[t];
752
+ e.issues.length > 0 ? o.push(...prefixIssues(e.issues, i)) : (e.value !== void 0 || Object.hasOwn(n, i)) && defineOwnProperty(s, i, e.value);
753
+ }), this._unknownKeys === "relaxed") for (let e of a) isUnsafeObjectKey(e) || defineOwnProperty(s, e, n[e]);
1204
754
  return {
1205
- ...n,
1206
- minimum: n.minimum === void 0 ? e : Math.max(n.minimum, e)
755
+ issues: o,
756
+ typeOk: !0,
757
+ value: s
1207
758
  };
1208
759
  });
1209
760
  }
1210
- max(e, t) {
1211
- return this._addConstraint((n, i) => n <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.number.max, {
1212
- max: e,
1213
- value: n
1214
- }), { max: e }), (t) => {
1215
- let n = t;
1216
- return {
1217
- ...n,
1218
- maximum: n.maximum === void 0 ? e : Math.min(n.maximum, e)
1219
- };
1220
- });
761
+ relaxed() {
762
+ return this._withShape(this.shape, "relaxed");
1221
763
  }
1222
- int(e) {
1223
- return this._addConstraint((t, n) => Number.isInteger(t) ? null : fail(r.invalid_integer, resolveMessage(e ?? n.messages.number.int, { value: t })), (e) => ({
1224
- ...e,
1225
- typeHint: "integer"
1226
- }));
764
+ strict() {
765
+ return this._withShape(this.shape, "strict");
1227
766
  }
1228
- positive(e) {
1229
- return this._addConstraint((t, n) => t > 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.number.positive, { value: t }), {
1230
- exclusive: !0,
1231
- min: 0
1232
- }), (e) => {
1233
- let t = e;
1234
- return {
1235
- ...t,
1236
- exclusiveMinimum: t.exclusiveMinimum === void 0 ? 0 : Math.max(t.exclusiveMinimum, 0)
1237
- };
1238
- });
767
+ tolerate(...e) {
768
+ return this._withShape(this.shape, this._unknownKeys, [...this._tolerated, ...e]);
1239
769
  }
1240
- negative(e) {
1241
- return this._addConstraint((t, n) => t < 0 ? null : fail(r.too_big, resolveMessage(e ?? n.messages.number.negative, { value: t }), {
1242
- exclusive: !0,
1243
- max: 0
1244
- }), (e) => {
1245
- let t = e;
1246
- return {
1247
- ...t,
1248
- exclusiveMaximum: t.exclusiveMaximum === void 0 ? 0 : Math.min(t.exclusiveMaximum, 0)
1249
- };
770
+ extend(e) {
771
+ return this._derive({
772
+ ...this.shape,
773
+ ...e
1250
774
  });
1251
775
  }
1252
- nonNegative(e) {
1253
- return this._addConstraint((t, n) => t >= 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.number.nonNegative, { value: t }), { min: 0 }), (e) => {
1254
- let t = e;
1255
- return {
1256
- ...t,
1257
- minimum: t.minimum === void 0 ? 0 : Math.max(t.minimum, 0)
1258
- };
1259
- });
776
+ pick(...e) {
777
+ let t = objectFromEntries(Object.entries(this.shape).filter(([t]) => e.includes(t)));
778
+ return this._derive(t);
1260
779
  }
1261
- nonPositive(e) {
1262
- return this._addConstraint((t, n) => t <= 0 ? null : fail(r.too_big, resolveMessage(e ?? n.messages.number.nonPositive, { value: t }), { max: 0 }), (e) => {
1263
- let t = e;
1264
- return {
1265
- ...t,
1266
- maximum: t.maximum === void 0 ? 0 : Math.min(t.maximum, 0)
1267
- };
1268
- });
780
+ omit(...e) {
781
+ let t = objectFromEntries(Object.entries(this.shape).filter(([t]) => !e.includes(t)));
782
+ return this._derive(t);
1269
783
  }
1270
- multipleOf(e, t) {
1271
- return this._addConstraint((n, i) => Math.abs(Math.round(n / e) - n / e) < 1e-9 ? null : fail(r.invalid_multiple_of, resolveMessage(t ?? i.messages.number.multipleOf, {
1272
- step: e,
1273
- value: n
1274
- }), { step: e }), (t) => ({
1275
- ...t,
1276
- multipleOf: e
1277
- }));
784
+ partial(...e) {
785
+ return this._derive(objectFromEntries(Object.entries(this.shape).map(([t, n]) => [t, e.length === 0 || e.includes(t) ? n.optional() : n])));
1278
786
  }
1279
- safe(e) {
1280
- return this._addConstraint((t, n) => Number.isSafeInteger(t) ? null : fail(r.invalid_safe, resolveMessage(e ?? n.messages.number.safe, { value: t })));
787
+ keyof() {
788
+ return new EnumSchema(Object.keys(this.shape));
1281
789
  }
1282
- finite(e) {
1283
- return this._addConstraint((t, n) => Number.isFinite(t) ? null : fail(r.invalid_finite, resolveMessage(e ?? n.messages.number.finite, { value: t })));
790
+ _withShape(e, t = this._unknownKeys, n = this._tolerated) {
791
+ let r = new ObjectSchema(e, t, n);
792
+ return r.state = this.state, r;
793
+ }
794
+ _derive(e) {
795
+ let { catch: t, default: n, preprocess: r, steps: i } = this.state;
796
+ if (i.length > 0 || r.length > 0 || t || n) throw new SpellDefinitionError("Cannot derive a new shape from an object schema with checks, transforms, preprocessors, defaults, or catches. Derive the shape first, then add them.");
797
+ return this._withShape(e);
1284
798
  }
1285
- _toDescriptorImpl() {
1286
- let e = this._annotations;
799
+ _describe() {
800
+ let e = {};
801
+ for (let [t, n] of Object.entries(this.shape)) defineOwnProperty(e, t, n.definition());
1287
802
  return {
1288
803
  ...this._describeBase(),
1289
- ...e.exclusiveMaximum === void 0 ? {} : { exclusiveMaximum: e.exclusiveMaximum },
1290
- ...e.exclusiveMinimum === void 0 ? {} : { exclusiveMinimum: e.exclusiveMinimum },
1291
- ...e.maximum === void 0 ? {} : { maximum: e.maximum },
1292
- ...e.minimum === void 0 ? {} : { minimum: e.minimum },
1293
- ...e.multipleOf === void 0 ? {} : { multipleOf: e.multipleOf },
1294
- ...e.typeHint === void 0 ? {} : { typeHint: e.typeHint },
1295
- kind: "number"
804
+ fields: e,
805
+ kind: "object",
806
+ strict: this._unknownKeys === "strict",
807
+ ...this._tolerated.length > 0 && { tolerated: this._tolerated }
1296
808
  };
1297
809
  }
1298
- static coerce() {
1299
- return new NumberSchema().preprocess((e) => {
1300
- if (typeof e == "number") return e;
1301
- if (typeof e == "string" || typeof e == "boolean") {
1302
- let t = Number(e);
1303
- return Number.isNaN(t) ? e : t;
1304
- }
1305
- return e;
1306
- });
1307
- }
1308
- }, UnionSchema = class extends Schema {
1309
- schemas;
1310
- get _kind() {
1311
- return "union";
810
+ }, DiscriminatedUnionSchema = class extends Schema {
811
+ discriminator;
812
+ branches;
813
+ constructor(e, t) {
814
+ super();
815
+ let n = /* @__PURE__ */ new Map();
816
+ for (let [r, i] of Object.entries(t)) {
817
+ if (!(i instanceof o)) throw new SpellDefinitionError(`Branch "${r}" must be an object schema.`);
818
+ let t = i.shape[e];
819
+ if (t === void 0) n.set(r, i._withShape({
820
+ ...i.shape,
821
+ [e]: new LiteralSchema(r)
822
+ }));
823
+ else if (t instanceof LiteralSchema && t.value === r) n.set(r, i);
824
+ else throw new SpellDefinitionError(`Branch "${r}" declares a "${e}" field that does not match its discriminator value.`);
825
+ }
826
+ this.discriminator = e, this.branches = n;
1312
827
  }
1313
- constructor(e) {
1314
- super(), this.schemas = Object.freeze([...e]);
828
+ get kind() {
829
+ return "discriminatedUnion";
1315
830
  }
1316
831
  _parse(e, t) {
1317
- let n = [];
1318
- for (let r of this.schemas) {
1319
- let i = r._parseFullSync(e, t);
1320
- if (i.issues.length === 0) return {
1321
- data: i.data,
1322
- issues: [],
1323
- typeOk: !0
1324
- };
1325
- n.push(i.issues);
832
+ let { messages: n } = t;
833
+ if (typeof e != "object" || !e || Array.isArray(e)) return invalid(e, issue(i.invalid_type, n.discriminatedUnion.type()));
834
+ let r = e[this.discriminator], a = typeof r == "string" ? this.branches.get(r) : void 0;
835
+ if (!a) {
836
+ let t = [...this.branches.keys()];
837
+ return invalid(e, issue(i.invalid_discriminator, n.discriminatedUnion.invalidDiscriminator({
838
+ discriminator: this.discriminator,
839
+ expected: t
840
+ }), {
841
+ discriminator: this.discriminator,
842
+ expected: t
843
+ }));
1326
844
  }
1327
- return {
1328
- data: e,
1329
- issues: [{
1330
- code: r.invalid_union,
1331
- message: t.messages.union.invalid(),
1332
- params: { errors: n },
1333
- path: []
1334
- }],
1335
- typeOk: !1
1336
- };
845
+ return chain(a._run(e, t), (t) => t.issues.length === 0 ? ok(t.value) : invalid(e, ...t.issues));
1337
846
  }
1338
- async _parseAsync(e, t) {
1339
- let n = [];
1340
- for (let r of this.schemas) {
1341
- let i = await r._parseFullAsync(e, t);
1342
- if (i.issues.length === 0) return {
1343
- data: i.data,
1344
- issues: [],
1345
- typeOk: !0
1346
- };
1347
- n.push(i.issues);
1348
- }
847
+ _describe() {
848
+ let e = {};
849
+ for (let [t, n] of this.branches) defineOwnProperty(e, t, n.definition());
1349
850
  return {
1350
- data: e,
1351
- issues: [{
1352
- code: r.invalid_union,
1353
- message: t.messages.union.invalid(),
1354
- params: { errors: n },
1355
- path: []
1356
- }],
1357
- typeOk: !1
851
+ ...this._describeBase(),
852
+ branches: e,
853
+ discriminator: this.discriminator,
854
+ kind: "discriminatedUnion"
1358
855
  };
1359
856
  }
1360
- _toDescriptorImpl() {
857
+ };
858
+ // src/schemas/intersect.ts
859
+ function deepMerge(t, n) {
860
+ if (!e(t) || !e(n)) return n;
861
+ let r = objectFromEntries(Object.entries(t));
862
+ for (let [e, i] of Object.entries(n)) defineOwnProperty(r, e, deepMerge(t[e], i));
863
+ return r;
864
+ }
865
+ var IntersectSchema = class extends Schema {
866
+ schemas;
867
+ constructor(e) {
868
+ super(), this.schemas = Object.freeze([...e]);
869
+ }
870
+ get kind() {
871
+ return "intersect";
872
+ }
873
+ _parse(e, t) {
874
+ return chain(all(this.schemas.map((n) => n._run(e, t))), (t) => {
875
+ let n = t.flatMap((e) => e.issues);
876
+ return n.length > 0 ? invalid(e, ...n) : ok(t.map((e) => e.value).reduce(deepMerge));
877
+ });
878
+ }
879
+ _describe() {
1361
880
  return {
1362
881
  ...this._describeBase(),
1363
882
  branches: this.schemas.map((e) => e.definition()),
1364
- kind: "union"
883
+ kind: "intersect"
1365
884
  };
1366
885
  }
1367
- }, l = class ObjectSchema extends Schema {
1368
- shape;
1369
- _isRelaxed;
1370
- get _kind() {
1371
- return "object";
886
+ }, LazySchema = class extends Schema {
887
+ _getter;
888
+ _resolved;
889
+ constructor(e) {
890
+ super(), this._getter = e;
1372
891
  }
1373
- constructor(e, t = !1) {
1374
- super(), this.shape = Object.freeze(objectFromEntries(Object.entries(e))), this._isRelaxed = t;
1375
- }
1376
- _unknownKeys(e) {
1377
- return Object.keys(e).filter((e) => !Object.hasOwn(this.shape, e));
1378
- }
1379
- _strictUnknownKeyIssues(e, t) {
1380
- if (this._isRelaxed) return [];
1381
- let n = this._unknownKeys(e);
1382
- return n.length === 0 ? [] : [{
1383
- code: r.invalid_keys,
1384
- message: t.messages.object.invalidKeys({ keys: n }),
1385
- params: { keys: n },
1386
- path: []
1387
- }];
1388
- }
1389
- _copyRelaxedUnknownKeys(e, t) {
1390
- if (this._isRelaxed) for (let n of this._unknownKeys(e)) isUnsafeObjectKey(n) || defineOwnProperty(t, n, e[n]);
1391
- }
1392
- _guardObjectInput(e, t) {
1393
- return typeof e != "object" || !e || Array.isArray(e) ? {
1394
- issues: [{
1395
- code: r.invalid_type,
1396
- message: t.messages.object.type(),
1397
- path: []
1398
- }],
1399
- ok: !1
1400
- } : {
1401
- obj: e,
1402
- ok: !0
1403
- };
892
+ get kind() {
893
+ return "lazy";
1404
894
  }
1405
- _createObjectParseContext(e, t) {
895
+ _parse(e, t) {
896
+ return this._resolved ??= this._getter(), chain(this._resolved._run(e, t), (t) => t.issues.length === 0 ? ok(t.value) : invalid(e, ...t.issues));
897
+ }
898
+ _describe() {
1406
899
  return {
1407
- issues: [...this._strictUnknownKeyIssues(e, t)],
1408
- output: {}
900
+ ...this._describeBase(),
901
+ kind: "lazy"
1409
902
  };
1410
903
  }
1411
- _rebuildWith(e, t = this._isRelaxed) {
1412
- return this._copyStateTo(new ObjectSchema(e, t));
904
+ }, MapSchema = class extends Schema {
905
+ keySchema;
906
+ valueSchema;
907
+ constructor(e, t) {
908
+ super(), this.keySchema = e, this.valueSchema = t;
909
+ }
910
+ get kind() {
911
+ return "map";
1413
912
  }
1414
913
  _parse(e, t) {
1415
- let n = this._guardObjectInput(e, t);
1416
- if (!n.ok) return {
1417
- data: e,
1418
- issues: n.issues,
1419
- typeOk: !1
1420
- };
1421
- let { obj: r } = n, { issues: i, output: a } = this._createObjectParseContext(r, t);
1422
- for (let e of Object.keys(this.shape)) {
1423
- let n = this.shape[e]._parseFullSync(r[e], t);
1424
- n.issues.length === 0 ? defineOwnProperty(a, e, n.data) : i.push(...prependIssuePath(n.issues, e));
1425
- }
1426
- return this._copyRelaxedUnknownKeys(r, a), {
1427
- data: a,
1428
- issues: i,
1429
- typeOk: !0
1430
- };
914
+ return e instanceof Map ? chain(all([...e].map(([e, n]) => all([this.keySchema._run(e, t), this.valueSchema._run(n, t)]))), (e) => {
915
+ let t = [], n = /* @__PURE__ */ new Map();
916
+ return e.forEach(([e, r], i) => {
917
+ t.push(...prefixIssues(e.issues, i), ...prefixIssues(r.issues, i)), e.issues.length === 0 && r.issues.length === 0 && n.set(e.value, r.value);
918
+ }), {
919
+ issues: t,
920
+ typeOk: !0,
921
+ value: n
922
+ };
923
+ }) : invalid(e, issue(i.invalid_type, t.messages.map.type()));
1431
924
  }
1432
- async _parseAsync(e, t) {
1433
- let n = this._guardObjectInput(e, t);
1434
- if (!n.ok) return {
1435
- data: e,
1436
- issues: n.issues,
1437
- typeOk: !1
1438
- };
1439
- let { obj: r } = n, { issues: i, output: a } = this._createObjectParseContext(r, t), o = Object.keys(this.shape), s = await Promise.all(o.map((e) => this.shape[e]._parseFullAsync(r[e], t)));
1440
- for (let e = 0; e < o.length; e++) {
1441
- let t = o[e], n = s[e];
1442
- n.issues.length === 0 ? defineOwnProperty(a, t, n.data) : i.push(...prependIssuePath(n.issues, t));
1443
- }
1444
- return this._copyRelaxedUnknownKeys(r, a), {
1445
- data: a,
1446
- issues: i,
1447
- typeOk: !0
1448
- };
925
+ min(e, t) {
926
+ return this._refine((t) => t.size >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.map.min, {
927
+ min: e,
928
+ value: n
929
+ }), { min: e }), (t) => ({ minSize: atLeast(t.minSize, e) }));
1449
930
  }
1450
- partial(...e) {
1451
- let t = e.length > 0 ? new Set(e) : null;
1452
- return this._rebuildWith(objectFromEntries(Object.entries(this.shape).map(([e, n]) => [e, t === null || t.has(e) ? n.optional() : n])), this._isRelaxed);
931
+ max(e, t) {
932
+ return this._refine((t) => t.size <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.map.max, {
933
+ max: e,
934
+ value: n
935
+ }), { max: e }), (t) => ({ maxSize: atMost(t.maxSize, e) }));
1453
936
  }
1454
- optional() {
1455
- return super.optional();
937
+ size(e, t) {
938
+ return this._refine((t) => t.size === e, (n, r) => issue(i.invalid_length, resolveMessage(t ?? r.map.size, {
939
+ exact: e,
940
+ value: n
941
+ }), { exact: e }), (t) => ({
942
+ maxSize: atMost(t.maxSize, e),
943
+ minSize: atLeast(t.minSize, e)
944
+ }));
1456
945
  }
1457
- nullable() {
1458
- return super.nullable();
946
+ nonEmpty(e) {
947
+ return this._refine((e) => e.size > 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.map.nonEmpty, { value: t }), { min: 1 }), (e) => ({ minSize: atLeast(e.minSize, 1) }));
1459
948
  }
1460
- nullish() {
1461
- return super.nullish();
949
+ _describe() {
950
+ return {
951
+ ...this._describeBase(),
952
+ ...this.state.meta,
953
+ key: this.keySchema.definition(),
954
+ kind: "map",
955
+ value: this.valueSchema.definition()
956
+ };
1462
957
  }
1463
- required() {
1464
- return this._rebuildWith(objectFromEntries(Object.entries(this.shape).map(([e, t]) => [e, t.required()])), this._isRelaxed);
958
+ }, NumberSchema = class extends Schema {
959
+ get kind() {
960
+ return "number";
1465
961
  }
1466
- extend(e) {
1467
- return this._rebuildWith(objectFromEntries([...Object.entries(this.shape), ...Object.entries(e)]), this._isRelaxed);
962
+ _parse(e, t) {
963
+ return typeof e == "number" && !Number.isNaN(e) ? ok(e) : invalid(e, issue(i.invalid_type, t.messages.number.type()));
1468
964
  }
1469
- pick(...e) {
1470
- let t = new Set(e);
1471
- return this._rebuildWith(objectFromEntries(Object.entries(this.shape).filter(([e]) => t.has(e))));
965
+ min(e, t) {
966
+ return this._refine((t) => t >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.number.min, {
967
+ min: e,
968
+ value: n
969
+ }), { min: e }), (t) => ({ minimum: atLeast(t.minimum, e) }));
1472
970
  }
1473
- omit(...e) {
1474
- let t = new Set(e);
1475
- return this._rebuildWith(objectFromEntries(Object.entries(this.shape).filter(([e]) => !t.has(e))));
971
+ max(e, t) {
972
+ return this._refine((t) => t <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.number.max, {
973
+ max: e,
974
+ value: n
975
+ }), { max: e }), (t) => ({ maximum: atMost(t.maximum, e) }));
1476
976
  }
1477
- defaults() {
1478
- return this.parse({});
977
+ positive(e) {
978
+ return this._refine((e) => e > 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.number.positive, { value: t }), {
979
+ exclusive: !0,
980
+ min: 0
981
+ }), (e) => ({ exclusiveMinimum: atLeast(e.exclusiveMinimum, 0) }));
1479
982
  }
1480
- partialDefaults() {
1481
- let e = {};
1482
- for (let t of Object.keys(this.shape)) {
1483
- let n = this.shape[t]._parseFullSync(void 0);
1484
- n.issues.length === 0 && defineOwnProperty(e, t, n.data);
1485
- }
1486
- return e;
983
+ negative(e) {
984
+ return this._refine((e) => e < 0, (t, n) => issue(i.too_big, resolveMessage(e ?? n.number.negative, { value: t }), {
985
+ exclusive: !0,
986
+ max: 0
987
+ }), (e) => ({ exclusiveMaximum: atMost(e.exclusiveMaximum, 0) }));
1487
988
  }
1488
- requiredFields() {
1489
- return Object.keys(this.shape).filter((e) => !this.shape[e].isOptional);
989
+ nonNegative(e) {
990
+ return this._refine((e) => e >= 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.number.nonNegative, { value: t }), { min: 0 }), (e) => ({ minimum: atLeast(e.minimum, 0) }));
1490
991
  }
1491
- optionalFields() {
1492
- return Object.keys(this.shape).filter((e) => this.shape[e].isOptional);
992
+ nonPositive(e) {
993
+ return this._refine((e) => e <= 0, (t, n) => issue(i.too_big, resolveMessage(e ?? n.number.nonPositive, { value: t }), { max: 0 }), (e) => ({ maximum: atMost(e.maximum, 0) }));
1493
994
  }
1494
- relaxed() {
1495
- return this._rebuildWith(this.shape, !0);
995
+ int(e) {
996
+ return this._refine(Number.isInteger, (t, n) => issue(i.invalid_integer, resolveMessage(e ?? n.number.int, { value: t })), () => ({ integer: !0 }));
1496
997
  }
1497
- strict() {
1498
- return this._rebuildWith(this.shape, !1);
998
+ safe(e) {
999
+ return this._refine(Number.isSafeInteger, (t, n) => issue(i.invalid_safe, resolveMessage(e ?? n.number.safe, { value: t })), (e) => ({
1000
+ integer: !0,
1001
+ maximum: atMost(e.maximum, 2 ** 53 - 1),
1002
+ minimum: atLeast(e.minimum, -(2 ** 53 - 1))
1003
+ }));
1499
1004
  }
1500
- keyof() {
1501
- return new UnionSchema(Object.keys(this.shape).map((e) => new LiteralSchema(e)));
1005
+ finite(e) {
1006
+ return this._refine(Number.isFinite, (t, n) => issue(i.invalid_finite, resolveMessage(e ?? n.number.finite, { value: t })), () => ({ finite: !0 }));
1502
1007
  }
1503
- _toDescriptorImpl() {
1504
- let e = {};
1505
- for (let [t, n] of Object.entries(this.shape)) defineOwnProperty(e, t, n.definition());
1008
+ multipleOf(e, t) {
1009
+ return this._refine((t) => Math.abs(Math.round(t / e) - t / e) < 1e-9, (n, r) => issue(i.invalid_multiple_of, resolveMessage(t ?? r.number.multipleOf, {
1010
+ step: e,
1011
+ value: n
1012
+ }), { step: e }), () => ({ multipleOf: e }));
1013
+ }
1014
+ _describe() {
1506
1015
  return {
1507
1016
  ...this._describeBase(),
1508
- fields: e,
1509
- kind: "object",
1510
- strict: !this._isRelaxed
1017
+ ...this.state.meta,
1018
+ kind: "number"
1511
1019
  };
1512
1020
  }
1513
- merge(e) {
1514
- return this._rebuildWith(objectFromEntries([...Object.entries(this.shape), ...Object.entries(e.shape)]), e._isRelaxed);
1515
- }
1516
- };
1517
- // src/schemas/raw-or.ts
1518
- function normalizeToSchemas(e) {
1519
- return e.map((e) => e instanceof Schema ? e : new LiteralSchema(e));
1520
- }
1521
- // src/schemas/record.ts
1522
- var RecordSchema = class extends Schema {
1021
+ }, RecordSchema = class extends Schema {
1523
1022
  keySchema;
1524
1023
  valueSchema;
1525
- get _kind() {
1526
- return "record";
1527
- }
1528
1024
  constructor(e, t) {
1529
1025
  super(), this.keySchema = e, this.valueSchema = t;
1530
1026
  }
1531
- _guardRecordInput(e, t) {
1532
- return typeof e != "object" || !e || Array.isArray(e) ? {
1533
- issues: [{
1534
- code: r.invalid_type,
1535
- message: t.messages.object.type(),
1536
- path: []
1537
- }],
1538
- ok: !1
1539
- } : {
1540
- ok: !0,
1541
- value: e
1542
- };
1543
- }
1544
- _parseRecordEntries(e, t) {
1545
- let n = [], r = {};
1546
- for (let i of Object.keys(e)) {
1547
- let a = this.keySchema._parseFullSync(i, t);
1548
- if (a.issues.length > 0) {
1549
- n.push(...prependIssuePath(a.issues, i));
1550
- continue;
1551
- }
1552
- let o = a.data;
1553
- if (isUnsafeObjectKey(o)) continue;
1554
- let s = this.valueSchema._parseFullSync(e[i], t);
1555
- s.issues.length === 0 ? defineOwnProperty(r, o, s.data) : n.push(...prependIssuePath(s.issues, i));
1556
- }
1557
- return {
1558
- issues: n,
1559
- output: r
1560
- };
1027
+ get kind() {
1028
+ return "record";
1561
1029
  }
1562
1030
  _parse(e, t) {
1563
- let n = this._guardRecordInput(e, t);
1564
- if (!n.ok) return {
1565
- data: e,
1566
- issues: n.issues,
1567
- typeOk: !1
1568
- };
1569
- let { issues: r, output: i } = this._parseRecordEntries(n.value, t);
1570
- return {
1571
- data: i,
1572
- issues: r,
1573
- typeOk: !0
1574
- };
1575
- }
1576
- async _parseAsync(e, t) {
1577
- let n = this._guardRecordInput(e, t);
1578
- if (!n.ok) return {
1579
- data: e,
1580
- issues: n.issues,
1581
- typeOk: !1
1582
- };
1583
- let r = n.value, i = Object.keys(r), a = await Promise.all(i.map((e) => Promise.all([this.keySchema._parseFullAsync(e, t), this.valueSchema._parseFullAsync(r[e], t)]))), o = [], s = {};
1584
- for (let e = 0; e < i.length; e++) {
1585
- let t = i[e], [n, r] = a[e];
1586
- if (n.issues.length > 0) {
1587
- o.push(...prependIssuePath(n.issues, t));
1588
- continue;
1589
- }
1590
- let c = n.data;
1591
- isUnsafeObjectKey(c) || (r.issues.length === 0 ? defineOwnProperty(s, c, r.data) : o.push(...prependIssuePath(r.issues, t)));
1592
- }
1593
- return {
1594
- data: s,
1595
- issues: o,
1596
- typeOk: !0
1597
- };
1031
+ if (typeof e != "object" || !e || Array.isArray(e)) return invalid(e, issue(i.invalid_type, t.messages.object.type()));
1032
+ let n = e, r = Object.keys(n);
1033
+ return chain(all(r.map((e) => all([this.keySchema._run(e, t), this.valueSchema._run(n[e], t)]))), (e) => {
1034
+ let t = [], n = {};
1035
+ return e.forEach(([e, i], a) => {
1036
+ let o = r[a];
1037
+ if (e.issues.length > 0) {
1038
+ t.push(...prefixIssues(e.issues, o));
1039
+ return;
1040
+ }
1041
+ if (i.issues.length > 0) {
1042
+ t.push(...prefixIssues(i.issues, o));
1043
+ return;
1044
+ }
1045
+ let s = String(e.value);
1046
+ isUnsafeObjectKey(s) || defineOwnProperty(n, s, i.value);
1047
+ }), {
1048
+ issues: t,
1049
+ typeOk: !0,
1050
+ value: n
1051
+ };
1052
+ });
1598
1053
  }
1599
- _toDescriptorImpl() {
1054
+ _describe() {
1600
1055
  return {
1601
1056
  ...this._describeBase(),
1602
1057
  key: this.keySchema.definition(),
@@ -1606,105 +1061,72 @@ var RecordSchema = class extends Schema {
1606
1061
  }
1607
1062
  }, SetSchema = class extends Schema {
1608
1063
  itemSchema;
1609
- get _kind() {
1610
- return "set";
1611
- }
1612
1064
  constructor(e) {
1613
1065
  super(), this.itemSchema = e;
1614
1066
  }
1615
- _parse(e, t) {
1616
- if (!(e instanceof Set)) return {
1617
- data: e,
1618
- issues: [{
1619
- code: r.invalid_type,
1620
- message: t.messages.set.type(),
1621
- path: []
1622
- }],
1623
- typeOk: !1
1624
- };
1625
- let n = [], i = /* @__PURE__ */ new Set(), a = 0;
1626
- for (let r of e) {
1627
- let e = this.itemSchema._parseFullSync(r, t);
1628
- e.issues.length === 0 ? i.add(e.data) : n.push(...prependIssuePath(e.issues, a)), a += 1;
1629
- }
1630
- return {
1631
- data: i,
1632
- issues: n,
1633
- typeOk: !0
1634
- };
1067
+ get kind() {
1068
+ return "set";
1635
1069
  }
1636
- async _parseAsync(e, t) {
1637
- if (!(e instanceof Set)) return {
1638
- data: e,
1639
- issues: [{
1640
- code: r.invalid_type,
1641
- message: t.messages.set.type(),
1642
- path: []
1643
- }],
1644
- typeOk: !1
1645
- };
1646
- let n = [...e], i = await Promise.all(n.map((e) => this.itemSchema._parseFullAsync(e, t))), a = [], o = /* @__PURE__ */ new Set();
1647
- for (let e = 0; e < i.length; e++) {
1648
- let t = i[e];
1649
- t.issues.length === 0 ? o.add(t.data) : a.push(...prependIssuePath(t.issues, e));
1650
- }
1651
- return {
1652
- data: o,
1653
- issues: a,
1654
- typeOk: !0
1655
- };
1070
+ _parse(e, t) {
1071
+ return e instanceof Set ? chain(all([...e].map((e) => this.itemSchema._run(e, t))), (e) => {
1072
+ let t = [], n = /* @__PURE__ */ new Set();
1073
+ return e.forEach((e, r) => {
1074
+ e.issues.length === 0 ? n.add(e.value) : t.push(...prefixIssues(e.issues, r));
1075
+ }), {
1076
+ issues: t,
1077
+ typeOk: !0,
1078
+ value: n
1079
+ };
1080
+ }) : invalid(e, issue(i.invalid_type, t.messages.set.type()));
1656
1081
  }
1657
1082
  min(e, t) {
1658
- return this._addConstraint((n, i) => {
1659
- let a = n;
1660
- return a.size >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.set.min, {
1661
- min: e,
1662
- value: a
1663
- }), { min: e });
1664
- });
1083
+ return this._refine((t) => t.size >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.set.min, {
1084
+ min: e,
1085
+ value: n
1086
+ }), { min: e }), (t) => ({ minSize: atLeast(t.minSize, e) }));
1665
1087
  }
1666
1088
  max(e, t) {
1667
- return this._addConstraint((n, i) => {
1668
- let a = n;
1669
- return a.size <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.set.max, {
1670
- max: e,
1671
- value: a
1672
- }), { max: e });
1673
- });
1089
+ return this._refine((t) => t.size <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.set.max, {
1090
+ max: e,
1091
+ value: n
1092
+ }), { max: e }), (t) => ({ maxSize: atMost(t.maxSize, e) }));
1674
1093
  }
1675
1094
  size(e, t) {
1676
- return this._addConstraint((n, i) => {
1677
- let a = n;
1678
- return a.size === e ? null : fail(r.invalid_length, resolveMessage(t ?? i.messages.set.size, {
1679
- exact: e,
1680
- value: a
1681
- }), { exact: e });
1682
- });
1095
+ return this._refine((t) => t.size === e, (n, r) => issue(i.invalid_length, resolveMessage(t ?? r.set.size, {
1096
+ exact: e,
1097
+ value: n
1098
+ }), { exact: e }), (t) => ({
1099
+ maxSize: atMost(t.maxSize, e),
1100
+ minSize: atLeast(t.minSize, e)
1101
+ }));
1683
1102
  }
1684
1103
  nonEmpty(e) {
1685
- return this._addConstraint((t, n) => t.size > 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.set.nonEmpty, { min: 1 }), { min: 1 }));
1104
+ return this._refine((e) => e.size > 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.set.nonEmpty, { value: t }), { min: 1 }), (e) => ({ minSize: atLeast(e.minSize, 1) }));
1686
1105
  }
1687
- _toDescriptorImpl() {
1106
+ _describe() {
1688
1107
  return {
1689
1108
  ...this._describeBase(),
1109
+ ...this.state.meta,
1690
1110
  items: this.itemSchema.definition(),
1691
1111
  kind: "set"
1692
1112
  };
1693
1113
  }
1694
1114
  };
1695
- // src/formats.ts
1696
- function isEmail(e) {
1697
- return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(e);
1698
- }
1699
- var u = /* @__PURE__ */ new Set(["http", "https"]);
1700
- function isUrl(e, t) {
1115
+ // src/predicates.ts
1116
+ function parseUrl(e) {
1701
1117
  try {
1702
- let n = new URL(e);
1703
- return (t ? new Set(t.map((e) => e.toLowerCase())) : u).has(n.protocol.replace(":", "").toLowerCase());
1118
+ return new URL(e);
1704
1119
  } catch {
1705
- return !1;
1120
+ return null;
1706
1121
  }
1707
1122
  }
1123
+ function isEmail(e) {
1124
+ return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(e);
1125
+ }
1126
+ function isUrl(e, t = ["http", "https"]) {
1127
+ let n = parseUrl(e)?.protocol.slice(0, -1).toLowerCase();
1128
+ return n !== void 0 && t.some((e) => e.toLowerCase() === n);
1129
+ }
1708
1130
  function isUuid(e) {
1709
1131
  return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e);
1710
1132
  }
@@ -1717,15 +1139,7 @@ function isIsoDateTime(e) {
1717
1139
  return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})?$/.test(e) && !Number.isNaN(new Date(e).getTime());
1718
1140
  }
1719
1141
  function isIp(e) {
1720
- if (/^(\d{1,3}\.){3}\d{1,3}$/.test(e)) return e.split(".").every((e) => {
1721
- let t = parseInt(e, 10);
1722
- return t >= 0 && t <= 255;
1723
- });
1724
- try {
1725
- return new URL(`http://[${e}]/`), !0;
1726
- } catch {
1727
- return !1;
1728
- }
1142
+ return /^(\d{1,3}\.){3}\d{1,3}$/.test(e) ? e.split(".").every((e) => Number(e) <= 255) : /^[0-9A-Fa-f:.]+$/.test(e) && parseUrl(`http://[${e}]/`) !== null;
1729
1143
  }
1730
1144
  function isCuid(e) {
1731
1145
  return /^c[a-z0-9]{8,}$/.test(e);
@@ -1773,453 +1187,280 @@ function isNumeric(e) {
1773
1187
  return /^[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?$/i.test(e);
1774
1188
  }
1775
1189
  // src/schemas/string.ts
1776
- var d = class StringSchema extends Schema {
1777
- get _kind() {
1190
+ var escapeRegExp = (e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), StringSchema = class extends Schema {
1191
+ get kind() {
1778
1192
  return "string";
1779
1193
  }
1780
- constructor() {
1781
- super((e, t) => typeof e == "string" ? null : fail(r.invalid_type, t.messages.string.type()));
1194
+ _parse(e, t) {
1195
+ return typeof e == "string" ? ok(e) : invalid(e, issue(i.invalid_type, t.messages.string.type()));
1196
+ }
1197
+ _format(e, t, n, r) {
1198
+ return this._refine(t, (t, r) => issue(i.invalid_format, resolveMessage(n ?? r.string[e], { value: t }), { format: e }), (t) => ({
1199
+ ...r?.(t),
1200
+ formats: [...t.formats ?? [], e]
1201
+ }));
1202
+ }
1203
+ _pattern(e, t, n) {
1204
+ return this._refine(e, n, (e) => ({ patterns: [...e.patterns ?? [], t] }));
1782
1205
  }
1783
1206
  min(e, t) {
1784
- return this._addConstraint((n, i) => n.length >= e ? null : fail(r.too_small, resolveMessage(t ?? i.messages.string.min, {
1207
+ return this._refine((t) => t.length >= e, (n, r) => issue(i.too_small, resolveMessage(t ?? r.string.min, {
1785
1208
  min: e,
1786
1209
  value: n
1787
- }), { min: e }), (t) => {
1788
- let n = t;
1789
- return {
1790
- ...n,
1791
- minLength: n.minLength === void 0 ? e : Math.max(n.minLength, e)
1792
- };
1793
- });
1210
+ }), { min: e }), (t) => ({ minLength: atLeast(t.minLength, e) }));
1794
1211
  }
1795
1212
  max(e, t) {
1796
- return this._addConstraint((n, i) => n.length <= e ? null : fail(r.too_big, resolveMessage(t ?? i.messages.string.max, {
1213
+ return this._refine((t) => t.length <= e, (n, r) => issue(i.too_big, resolveMessage(t ?? r.string.max, {
1797
1214
  max: e,
1798
1215
  value: n
1799
- }), { max: e }), (t) => {
1800
- let n = t;
1801
- return {
1802
- ...n,
1803
- maxLength: n.maxLength === void 0 ? e : Math.min(n.maxLength, e)
1804
- };
1805
- });
1216
+ }), { max: e }), (t) => ({ maxLength: atMost(t.maxLength, e) }));
1806
1217
  }
1807
1218
  length(e, t) {
1808
- return this._addConstraint((n, i) => n.length === e ? null : fail(r.invalid_length, resolveMessage(t ?? i.messages.string.length, {
1219
+ return this._refine((t) => t.length === e, (n, r) => issue(i.invalid_length, resolveMessage(t ?? r.string.length, {
1809
1220
  exact: e,
1810
1221
  value: n
1811
1222
  }), { exact: e }), (t) => ({
1812
- ...t,
1813
- maxLength: e,
1814
- minLength: e
1223
+ maxLength: atMost(t.maxLength, e),
1224
+ minLength: atLeast(t.minLength, e)
1815
1225
  }));
1816
1226
  }
1817
1227
  nonEmpty(e) {
1818
- return this._addConstraint((t, n) => t.length > 0 ? null : fail(r.too_small, resolveMessage(e ?? n.messages.string.nonEmpty, {
1819
- min: 1,
1820
- value: t
1821
- }), { min: 1 }), (e) => {
1822
- let t = e;
1823
- return {
1824
- ...t,
1825
- minLength: Math.max(t.minLength ?? 0, 1)
1826
- };
1827
- });
1228
+ return this._refine((e) => e.length > 0, (t, n) => issue(i.too_small, resolveMessage(e ?? n.string.nonEmpty, { value: t }), { min: 1 }), (e) => ({ minLength: atLeast(e.minLength, 1) }));
1828
1229
  }
1829
1230
  startsWith(e, t) {
1830
- return this._addConstraint((n, i) => n.startsWith(e) ? null : fail(r.invalid_string, resolveMessage(t ?? i.messages.string.startsWith, {
1231
+ return this._pattern((t) => t.startsWith(e), {
1232
+ flags: "",
1233
+ source: `^${escapeRegExp(e)}`
1234
+ }, (n, r) => issue(i.invalid_format, resolveMessage(t ?? r.string.startsWith, {
1831
1235
  prefix: e,
1832
1236
  value: n
1833
- }), { prefix: e }));
1237
+ }), {
1238
+ format: "startsWith",
1239
+ prefix: e
1240
+ }));
1834
1241
  }
1835
1242
  endsWith(e, t) {
1836
- return this._addConstraint((n, i) => n.endsWith(e) ? null : fail(r.invalid_string, resolveMessage(t ?? i.messages.string.endsWith, {
1243
+ return this._pattern((t) => t.endsWith(e), {
1244
+ flags: "",
1245
+ source: `${escapeRegExp(e)}$`
1246
+ }, (n, r) => issue(i.invalid_format, resolveMessage(t ?? r.string.endsWith, {
1837
1247
  suffix: e,
1838
1248
  value: n
1839
- }), { suffix: e }));
1249
+ }), {
1250
+ format: "endsWith",
1251
+ suffix: e
1252
+ }));
1840
1253
  }
1841
1254
  includes(e, t) {
1842
- return this._addConstraint((n, i) => n.includes(e) ? null : fail(r.invalid_string, resolveMessage(t ?? i.messages.string.includes, {
1255
+ return this._pattern((t) => t.includes(e), {
1256
+ flags: "",
1257
+ source: escapeRegExp(e)
1258
+ }, (n, r) => issue(i.invalid_format, resolveMessage(t ?? r.string.includes, {
1843
1259
  substr: e,
1844
1260
  value: n
1845
- }), { includes: e }));
1261
+ }), {
1262
+ format: "includes",
1263
+ includes: e
1264
+ }));
1846
1265
  }
1847
1266
  regex(e, t) {
1848
1267
  let n = new RegExp(e.source, e.flags.replace(/[gy]/g, ""));
1849
- return this._addConstraint((e, i) => n.test(e) ? null : fail(r.invalid_string, resolveMessage(t ?? i.messages.string.regex, { value: e }), { pattern: n.source }), (e) => {
1850
- let t = e;
1851
- return t.pattern === null ? t : t.pattern === void 0 ? {
1852
- ...t,
1853
- pattern: n.source
1854
- } : t.pattern === n.source ? t : {
1855
- ...t,
1856
- pattern: null
1857
- };
1858
- });
1268
+ return this._pattern((e) => n.test(e), {
1269
+ flags: n.flags,
1270
+ source: n.source
1271
+ }, (e, r) => issue(i.invalid_format, resolveMessage(t ?? r.string.regex, { value: e }), {
1272
+ format: "regex",
1273
+ pattern: n.source
1274
+ }));
1859
1275
  }
1860
1276
  email(e) {
1861
- return this._addConstraint((t, n) => isEmail(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.email, { value: t }), { format: "email" }), (e) => ({
1862
- ...e,
1863
- format: "email"
1864
- }));
1277
+ return this._format("email", isEmail, e);
1865
1278
  }
1866
1279
  url(e = {}) {
1867
- let { message: t, protocols: n = ["http", "https"] } = e;
1868
- return this._addConstraint((e, i) => isUrl(e, n) ? null : fail(r.invalid_url, resolveMessage(t ?? i.messages.string.url, { value: e }), { format: "url" }), (e) => ({
1869
- ...e,
1870
- format: "uri"
1871
- }));
1280
+ let { message: t, protocols: n = ["http", "https"] } = e, r = {
1281
+ flags: "i",
1282
+ source: `^(?:${n.map(escapeRegExp).join("|")}):`
1283
+ };
1284
+ return this._format("url", (e) => isUrl(e, n), t, (e) => ({ patterns: [...e.patterns ?? [], r] }));
1872
1285
  }
1873
1286
  uuid(e) {
1874
- return this._addConstraint((t, n) => isUuid(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.uuid, { value: t }), { format: "uuid" }), (e) => ({
1875
- ...e,
1876
- format: "uuid"
1877
- }));
1287
+ return this._format("uuid", isUuid, e);
1878
1288
  }
1879
1289
  isoDate(e) {
1880
- return this._addConstraint((t, n) => isIsoDate(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.date, { value: t }), { format: "iso-date" }), (e) => ({
1881
- ...e,
1882
- format: "date"
1883
- }));
1290
+ return this._format("isoDate", isIsoDate, e);
1884
1291
  }
1885
1292
  isoDateTime(e) {
1886
- return this._addConstraint((t, n) => isIsoDateTime(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.dateTime, { value: t }), { format: "iso-datetime" }), (e) => ({
1887
- ...e,
1888
- format: "date-time"
1889
- }));
1293
+ return this._format("isoDateTime", isIsoDateTime, e);
1294
+ }
1295
+ time(e) {
1296
+ return this._format("time", isTime, e);
1297
+ }
1298
+ duration(e) {
1299
+ return this._format("duration", isDuration, e);
1890
1300
  }
1891
1301
  ip(e) {
1892
- return this._addConstraint((t, n) => isIp(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.ip, { value: t }), { format: "ip" }));
1302
+ return this._format("ip", isIp, e);
1893
1303
  }
1894
1304
  cuid(e) {
1895
- return this._addConstraint((t, n) => isCuid(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.cuid, { value: t }), { format: "cuid" }));
1305
+ return this._format("cuid", isCuid, e);
1896
1306
  }
1897
1307
  cuid2(e) {
1898
- return this._addConstraint((t, n) => isCuid2(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.cuid2, { value: t }), { format: "cuid2" }));
1308
+ return this._format("cuid2", isCuid2, e);
1899
1309
  }
1900
1310
  ulid(e) {
1901
- return this._addConstraint((t, n) => isUlid(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.ulid, { value: t }), { format: "ulid" }));
1311
+ return this._format("ulid", isUlid, e);
1902
1312
  }
1903
- nanoid(e, t) {
1904
- return this._addConstraint((n, i) => isNanoid(n, e) ? null : fail(r.invalid_string, resolveMessage(t ?? i.messages.string.nanoid, { value: n }), { format: "nanoid" }));
1313
+ nanoid(e = 21, t) {
1314
+ return this._format("nanoid", (t) => isNanoid(t, e), t, (t) => ({
1315
+ maxLength: atMost(t.maxLength, e),
1316
+ minLength: atLeast(t.minLength, e)
1317
+ }));
1905
1318
  }
1906
1319
  base64(e) {
1907
- return this._addConstraint((t, n) => isBase64(t) ? null : fail(r.invalid_base64, resolveMessage(e ?? n.messages.string.base64, { value: t }), { format: "base64" }), (e) => ({
1908
- ...e,
1909
- contentEncoding: "base64"
1910
- }));
1320
+ return this._format("base64", isBase64, e);
1911
1321
  }
1912
1322
  base64url(e) {
1913
- return this._addConstraint((t, n) => isBase64url(t) ? null : fail(r.invalid_base64, resolveMessage(e ?? n.messages.string.base64url, { value: t }), { format: "base64url" }));
1323
+ return this._format("base64url", isBase64url, e);
1914
1324
  }
1915
1325
  hex(e) {
1916
- return this._addConstraint((t, n) => isHex(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.hex, { value: t }), { format: "hex" }));
1326
+ return this._format("hex", isHex, e);
1917
1327
  }
1918
1328
  hexColor(e) {
1919
- return this._addConstraint((t, n) => isHexColor(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.hexColor, { value: t }), { format: "hex-color" }));
1329
+ return this._format("hexColor", isHexColor, e);
1920
1330
  }
1921
1331
  emoji(e) {
1922
- return this._addConstraint((t, n) => isEmoji(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.emoji, { value: t }), { format: "emoji" }));
1332
+ return this._format("emoji", isEmoji, e);
1923
1333
  }
1924
1334
  jwt(e) {
1925
- return this._addConstraint((t, n) => isJwt(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.jwt, { value: t }), { format: "jwt" }));
1926
- }
1927
- time(e) {
1928
- return this._addConstraint((t, n) => isTime(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.time, { value: t }), { format: "time" }));
1929
- }
1930
- duration(e) {
1931
- return this._addConstraint((t, n) => isDuration(t) ? null : fail(r.invalid_duration, resolveMessage(e ?? n.messages.string.duration, { value: t }), { format: "duration" }), (e) => ({
1932
- ...e,
1933
- format: "duration"
1934
- }));
1335
+ return this._format("jwt", isJwt, e);
1935
1336
  }
1936
1337
  semver(e) {
1937
- return this._addConstraint((t, n) => isSemver(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.semver, { value: t }), { format: "semver" }));
1338
+ return this._format("semver", isSemver, e);
1938
1339
  }
1939
1340
  slug(e) {
1940
- return this._addConstraint((t, n) => isSlug(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.slug, { value: t }), { format: "slug" }));
1341
+ return this._format("slug", isSlug, e);
1941
1342
  }
1942
1343
  numeric(e) {
1943
- return this._addConstraint((t, n) => isNumeric(t) ? null : fail(r.invalid_string, resolveMessage(e ?? n.messages.string.numeric, { value: t }), { format: "numeric" }));
1344
+ return this._format("numeric", isNumeric, e);
1944
1345
  }
1945
1346
  trim() {
1946
- return this.preprocess((e) => typeof e == "string" ? e.trim() : e);
1347
+ return this.transform((e) => e.trim());
1947
1348
  }
1948
1349
  lowercase() {
1949
- return this.preprocess((e) => typeof e == "string" ? e.toLowerCase() : e);
1350
+ return this.transform((e) => e.toLowerCase());
1950
1351
  }
1951
1352
  uppercase() {
1952
- return this.preprocess((e) => typeof e == "string" ? e.toUpperCase() : e);
1353
+ return this.transform((e) => e.toUpperCase());
1953
1354
  }
1954
- _toDescriptorImpl() {
1955
- let e = this._annotations;
1355
+ _describe() {
1956
1356
  return {
1957
1357
  ...this._describeBase(),
1958
- ...e.contentEncoding === void 0 ? {} : { contentEncoding: e.contentEncoding },
1959
- ...e.format === void 0 ? {} : { format: e.format },
1960
- ...e.maxLength === void 0 ? {} : { maxLength: e.maxLength },
1961
- ...e.minLength === void 0 ? {} : { minLength: e.minLength },
1962
- ...e.pattern === void 0 ? {} : { pattern: e.pattern },
1358
+ ...this.state.meta,
1963
1359
  kind: "string"
1964
1360
  };
1965
1361
  }
1966
- static coerce() {
1967
- return new StringSchema().preprocess((e) => e == null ? e : String(e));
1968
- }
1969
- }, f = class TupleSchema extends Schema {
1362
+ }, s = class TupleSchema extends Schema {
1970
1363
  items;
1971
1364
  restSchema;
1972
- get _kind() {
1973
- return "tuple";
1974
- }
1975
1365
  constructor(e, t = null) {
1976
- super(), this.items = e, this.restSchema = t;
1366
+ super(), this.items = Object.freeze([...e]), this.restSchema = t;
1367
+ }
1368
+ get kind() {
1369
+ return "tuple";
1977
1370
  }
1978
1371
  rest(e) {
1979
- return this._copyStateTo(new TupleSchema(this.items, e));
1980
- }
1981
- _guardTupleInput(e, t) {
1982
- return this.restSchema === null && e.length !== this.items.length ? {
1983
- issues: [{
1984
- code: r.invalid_length,
1985
- message: t.messages.tuple.length({ exact: this.items.length }),
1986
- params: { exact: this.items.length },
1987
- path: []
1988
- }],
1989
- ok: !1
1990
- } : this.restSchema !== null && e.length < this.items.length ? {
1991
- issues: [{
1992
- code: r.too_small,
1993
- message: t.messages.tuple.min({ min: this.items.length }),
1994
- params: { min: this.items.length },
1995
- path: []
1996
- }],
1997
- ok: !1
1998
- } : {
1999
- ok: !0,
2000
- value: e
2001
- };
1372
+ let t = new TupleSchema(this.items, e);
1373
+ return t.state = this.state, t;
2002
1374
  }
2003
1375
  _parse(e, t) {
2004
- if (!Array.isArray(e)) return {
2005
- data: e,
2006
- issues: [{
2007
- code: r.invalid_type,
2008
- message: t.messages.tuple.type(),
2009
- path: []
2010
- }],
2011
- typeOk: !1
2012
- };
2013
- let n = this._guardTupleInput(e, t);
2014
- if (!n.ok) return {
2015
- data: e,
2016
- issues: n.issues,
2017
- typeOk: !1
2018
- };
2019
- let i = [], a = [], o = n.value;
2020
- for (let e = 0; e < this.items.length; e++) {
2021
- let n = this.items[e]._parseFullSync(o[e], t);
2022
- n.issues.length === 0 ? a.push(n.data) : (i.push(...prependIssuePath(n.issues, e)), a.push(o[e]));
2023
- }
2024
- if (this.restSchema !== null) for (let e = this.items.length; e < o.length; e++) {
2025
- let n = this.restSchema._parseFullSync(o[e], t);
2026
- n.issues.length === 0 ? a.push(n.data) : (i.push(...prependIssuePath(n.issues, e)), a.push(o[e]));
2027
- }
2028
- return {
2029
- data: a,
2030
- issues: i,
2031
- typeOk: !0
2032
- };
2033
- }
2034
- async _parseAsync(e, t) {
2035
- if (!Array.isArray(e)) return {
2036
- data: e,
2037
- issues: [{
2038
- code: r.invalid_type,
2039
- message: t.messages.tuple.type(),
2040
- path: []
2041
- }],
2042
- typeOk: !1
2043
- };
2044
- let n = this._guardTupleInput(e, t);
2045
- if (!n.ok) return {
2046
- data: e,
2047
- issues: n.issues,
2048
- typeOk: !1
2049
- };
2050
- let i = n.value, a = [], o = [], s = await Promise.all(this.items.map((e, n) => e._parseFullAsync(i[n], t)));
2051
- for (let e = 0; e < s.length; e++) {
2052
- let t = s[e];
2053
- t.issues.length === 0 ? o.push(t.data) : (a.push(...prependIssuePath(t.issues, e)), o.push(i[e]));
2054
- }
2055
- let c = this.restSchema;
2056
- if (c !== null) {
2057
- let e = i.slice(this.items.length), n = await Promise.all(e.map((e) => c._parseFullAsync(e, t)));
2058
- for (let e = 0; e < n.length; e++) {
2059
- let t = n[e], r = this.items.length + e;
2060
- t.issues.length === 0 ? o.push(t.data) : (a.push(...prependIssuePath(t.issues, r)), o.push(i[r]));
2061
- }
2062
- }
2063
- return {
2064
- data: o,
2065
- issues: a,
2066
- typeOk: !0
2067
- };
1376
+ let { messages: n } = t, r = this.items.length;
1377
+ if (!Array.isArray(e)) return invalid(e, issue(i.invalid_type, n.tuple.type()));
1378
+ if (this.restSchema === null && e.length !== r) return invalid(e, issue(i.invalid_length, n.tuple.length({ exact: r }), { exact: r }));
1379
+ if (e.length < r) return invalid(e, issue(i.too_small, n.tuple.min({ min: r }), { min: r }));
1380
+ let a = this.restSchema;
1381
+ return chain(all(e.map((e, n) => (n < r ? this.items[n] : a)._run(e, t))), (t) => {
1382
+ let n = [];
1383
+ return {
1384
+ issues: n,
1385
+ typeOk: !0,
1386
+ value: t.map((t, r) => t.issues.length === 0 ? t.value : (n.push(...prefixIssues(t.issues, r)), e[r]))
1387
+ };
1388
+ });
2068
1389
  }
2069
- _toDescriptorImpl() {
1390
+ _describe() {
2070
1391
  return {
2071
1392
  ...this._describeBase(),
2072
1393
  items: this.items.map((e) => e.definition()),
2073
1394
  kind: "tuple",
2074
- rest: this.restSchema === null ? null : this.restSchema.definition()
1395
+ rest: this.restSchema?.definition() ?? null
2075
1396
  };
2076
1397
  }
2077
- }, VariantSchema = class extends Schema {
2078
- _map;
2079
- _discriminator;
2080
- get _kind() {
2081
- return "variant";
2082
- }
2083
- constructor(e, t) {
2084
- let n = /* @__PURE__ */ new Map();
2085
- for (let [r, i] of Object.entries(t)) {
2086
- let t = new LiteralSchema(r), a = i.shape[e];
2087
- if (a && (!(a instanceof LiteralSchema) || a.value !== r)) throw new SpellError(`s.discriminatedUnion(): branch "${r}" defines a conflicting discriminator schema for "${e}".`);
2088
- let o = { [e]: t };
2089
- n.set(r, a ? i : i.extend(o));
2090
- }
2091
- super(), this._discriminator = e, this._map = n;
2092
- }
2093
- get discriminator() {
2094
- return this._discriminator;
2095
- }
2096
- get variantMap() {
2097
- return new Map(this._map);
1398
+ }, UnionSchema = class extends Schema {
1399
+ schemas;
1400
+ constructor(e) {
1401
+ super(), this.schemas = Object.freeze([...e]);
2098
1402
  }
2099
- _resolveVariant(e, t) {
2100
- if (typeof e != "object" || !e || Array.isArray(e)) return { issues: [{
2101
- code: r.invalid_type,
2102
- message: t.messages.variant.type(),
2103
- path: []
2104
- }] };
2105
- let n = e, i = n[this._discriminator], a = this._map.get(i);
2106
- if (!a) {
2107
- let e = [...this._map.keys()];
2108
- return { issues: [{
2109
- code: r.invalid_variant,
2110
- message: t.messages.variant.invalidDiscriminator({
2111
- discriminator: this._discriminator,
2112
- expected: e
2113
- }),
2114
- params: {
2115
- discriminator: this._discriminator,
2116
- expected: e
2117
- },
2118
- path: []
2119
- }] };
2120
- }
2121
- return {
2122
- matched: a,
2123
- obj: n
2124
- };
1403
+ get kind() {
1404
+ return "union";
2125
1405
  }
2126
1406
  _parse(e, t) {
2127
- let n = this._resolveVariant(e, t);
2128
- if ("issues" in n) return {
2129
- data: e,
2130
- issues: n.issues,
2131
- typeOk: !1
2132
- };
2133
- let r = n.matched._parseFullSync(e, t);
2134
- return r.issues.length === 0 ? {
2135
- data: r.data,
2136
- issues: [],
2137
- typeOk: !0
2138
- } : {
2139
- data: e,
2140
- issues: r.issues,
2141
- typeOk: !0
2142
- };
2143
- }
2144
- async _parseAsync(e, t) {
2145
- let n = this._resolveVariant(e, t);
2146
- if ("issues" in n) return {
2147
- data: e,
2148
- issues: n.issues,
2149
- typeOk: !1
2150
- };
2151
- let r = await n.matched._parseFullAsync(e, t);
2152
- return r.issues.length === 0 ? {
2153
- data: r.data,
2154
- issues: [],
2155
- typeOk: !0
2156
- } : {
2157
- data: e,
2158
- issues: r.issues,
2159
- typeOk: !0
2160
- };
1407
+ let attempt = (n, r) => n === this.schemas.length ? invalid(e, issue(i.invalid_union, t.messages.union.invalid(), { errors: r })) : chain(this.schemas[n]._run(e, t), (e) => e.issues.length === 0 ? ok(e.value) : attempt(n + 1, [...r, e.issues]));
1408
+ return attempt(0, []);
2161
1409
  }
2162
- _toDescriptorImpl() {
2163
- let e = {};
2164
- for (let [t, n] of this._map.entries()) defineOwnProperty(e, t, n.definition());
1410
+ _describe() {
2165
1411
  return {
2166
1412
  ...this._describeBase(),
2167
- branches: e,
2168
- discriminator: this._discriminator,
2169
- kind: "variant"
1413
+ branches: this.schemas.map((e) => e.definition()),
1414
+ kind: "union"
2170
1415
  };
2171
1416
  }
2172
- }, sArray = (e) => new ArraySchema(e), sBigint = () => new i(), sBoolean = () => new a(), sDate = () => new o(), sEnum = (e) => new EnumSchema(e), sInstanceof = (e) => new InstanceOfSchema(e), sIntersect = (...e) => new IntersectSchema(normalizeToSchemas(e)), sLazy = (e) => new s(e), sLiteral = (e) => new LiteralSchema(e), sMap = (e, t) => new MapSchema(e, t), sNever = () => new NeverSchema(), sNull = () => new LiteralSchema(null), sNumber = () => new c(), sObject = (e) => new l(e), sRecord = (e, t) => new RecordSchema(e, t), sSet = (e) => new SetSchema(e), sString = () => new d(), sTuple = (e) => new f(e), sUndefined = () => new LiteralSchema(void 0);
2173
- function sUnion(...e) {
2174
- return new UnionSchema(normalizeToSchemas(e));
2175
- }
2176
- var sUnknown = () => new Schema(), sVariant = (e, t) => new VariantSchema(e, t), p = {
2177
- array: sArray,
2178
- bigint: sBigint,
2179
- boolean: sBoolean,
1417
+ }, coerceNumber = (e) => {
1418
+ if (typeof e != "string" && typeof e != "boolean") return e;
1419
+ let t = Number(e);
1420
+ return Number.isNaN(t) ? e : t;
1421
+ }, coerceBoolean = (e) => e === "true" || e === "1" || e === 1 ? !0 : e === "false" || e === "0" || e === 0 ? !1 : e, coerceBigInt = (e) => {
1422
+ if (typeof e == "number" && !Number.isSafeInteger(e)) return e;
1423
+ if (typeof e == "string") {
1424
+ let t = e.trim();
1425
+ if (t.length === 0 || t.length > 1e3) return e;
1426
+ try {
1427
+ return BigInt(t);
1428
+ } catch {
1429
+ return e;
1430
+ }
1431
+ }
1432
+ return typeof e == "number" ? BigInt(e) : e;
1433
+ }, coerceDate = (e) => typeof e == "string" || typeof e == "number" ? new Date(e) : e, coerceString = (e) => e == null ? e : String(e), c = {
1434
+ array: (e) => new ArraySchema(e),
1435
+ bigint: () => new BigIntSchema(),
1436
+ boolean: () => new BooleanSchema(),
2180
1437
  coerce: {
2181
- bigint: () => i.coerce(),
2182
- boolean: () => a.coerce(),
2183
- date: () => o.coerce(),
2184
- number: () => c.coerce(),
2185
- string: () => d.coerce()
1438
+ bigint: () => new BigIntSchema().preprocess(coerceBigInt),
1439
+ boolean: () => new BooleanSchema().preprocess(coerceBoolean),
1440
+ date: () => new DateSchema().preprocess(coerceDate),
1441
+ number: () => new NumberSchema().preprocess(coerceNumber),
1442
+ string: () => new StringSchema().preprocess(coerceString)
2186
1443
  },
2187
- date: sDate,
2188
- discriminatedUnion: sVariant,
2189
- enum: sEnum,
2190
- instanceof: sInstanceof,
2191
- intersect: sIntersect,
2192
- lazy: sLazy,
2193
- literal: sLiteral,
2194
- map: sMap,
2195
- never: sNever,
2196
- null: sNull,
2197
- number: sNumber,
2198
- object: sObject,
2199
- record: sRecord,
2200
- set: sSet,
2201
- string: sString,
2202
- tuple: sTuple,
2203
- undefined: sUndefined,
2204
- union: sUnion,
2205
- unknown: sUnknown
1444
+ date: () => new DateSchema(),
1445
+ discriminatedUnion: (e, t) => new DiscriminatedUnionSchema(e, t),
1446
+ enum: (e) => new EnumSchema(e),
1447
+ instanceof: (e) => new InstanceOfSchema(e),
1448
+ intersect: (...e) => new IntersectSchema(e),
1449
+ lazy: (e) => new LazySchema(e),
1450
+ literal: (e) => new LiteralSchema(e),
1451
+ map: (e, t) => new MapSchema(e, t),
1452
+ never: () => new NeverSchema(),
1453
+ null: () => new LiteralSchema(null),
1454
+ number: () => new NumberSchema(),
1455
+ object: (e) => new o(e),
1456
+ record: (e, t) => new RecordSchema(e, t),
1457
+ set: (e) => new SetSchema(e),
1458
+ string: () => new StringSchema(),
1459
+ tuple: (e) => new s(e),
1460
+ undefined: () => new LiteralSchema(void 0),
1461
+ union: (...e) => new UnionSchema(e),
1462
+ unknown: () => new Schema()
2206
1463
  };
2207
- // src/tolerate.ts
2208
- function tolerate(e, ...t) {
2209
- let n = new Set(t);
2210
- return e.preprocess((e) => {
2211
- if (typeof e != "object" || !e) return e;
2212
- let t = !1;
2213
- for (let r of Object.keys(e)) if (n.has(r)) {
2214
- t = !0;
2215
- break;
2216
- }
2217
- if (!t) return e;
2218
- let r = {};
2219
- for (let [t, i] of Object.entries(e)) n.has(t) || (r[t] = i);
2220
- return r;
2221
- });
2222
- }
2223
- export { r as ErrorCode, Schema, SpellDefinitionError, SpellError, SpellValidationError, createParseContext, joinIssuePath, p as s, tolerate };
1464
+ export { i as ErrorCode, Schema, SpellDefinitionError, SpellError, SpellValidationError, createParseContext, joinIssuePath, c as s };
2224
1465
 
2225
1466
  //# sourceMappingURL=spell.js.map