schema-truth 0.7.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ajv-DTP2FXRr.js → ajv-DS9gtNZC.js} +1 -1
- package/dist/ajv.d.ts +1 -1
- package/dist/ajv.js +1 -1
- package/dist/{arktype-BdvvaBQX.js → arktype-CBV4bg-0.js} +179 -75
- package/dist/arktype.d.ts +3 -3
- package/dist/arktype.js +1 -1
- package/dist/cli/ajv.js +2 -2
- package/dist/cli/arktype.js +2 -2
- package/dist/cli/effect.js +2 -2
- package/dist/cli/typebox.js +2 -2
- package/dist/cli/valibot.js +2 -2
- package/dist/cli/yup.js +2 -2
- package/dist/cli/zod.js +2 -2
- package/dist/{cli-CL2mSark.js → cli-CB9GJ7qa.js} +2 -2
- package/dist/{effect-mUknRPv_.js → effect-D9pvwi_6.js} +91 -31
- package/dist/effect.d.ts +3 -3
- package/dist/effect.js +1 -1
- package/dist/{helper-Cfvb5UDq.js → helper-S7SSsYwh.js} +777 -40
- package/dist/{index-C2L8Ty0-.d.ts → index-EK43HM_z.d.ts} +29 -25
- package/dist/{index-DbV3dEmN.d.ts → index-G7UqNQ4w.d.ts} +54 -2
- package/dist/index.d.ts +63 -25
- package/dist/index.js +9 -9
- package/dist/{typebox-BdWJv3ND.js → typebox-DGGKJLdP.js} +82 -42
- package/dist/typebox.d.ts +3 -3
- package/dist/typebox.js +1 -1
- package/dist/{utils-WNBq1HR0.js → utils-Bky98UmP.js} +160 -31
- package/dist/{valibot-CaJIynCH.js → valibot-B6y4VQFF.js} +107 -36
- package/dist/valibot.d.ts +3 -3
- package/dist/valibot.js +1 -1
- package/dist/{yup-CM2TSFig.js → yup-DuqQwHUv.js} +2 -2
- package/dist/yup.d.ts +2 -2
- package/dist/yup.js +1 -1
- package/dist/{zod-BsXLZfwK.js → zod-CXo9NFuF.js} +192 -115
- package/dist/zod.d.ts +3 -3
- package/dist/zod.js +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,381 @@
|
|
|
1
|
-
import { $ as DEFINITION_REF_PREFIXES, A as isDefinitionKeyword,
|
|
1
|
+
import { $ as DEFINITION_REF_PREFIXES, A as isDefinitionKeyword, B as isSchemaObject, C as hasNumericKeyword, Dt as STRING_KEYWORDS, E as isArktypeDefinition, I as isQuoted, L as isRecord, M as isNullable, Mt as WIRE_PRIMITIVE_ORDER, O as isDeepLocalPointer, Q as DEFINITION_POINTER_PATTERN, R as isRootRef, T as hasType, Tt as STRINGBOOL_TRUTHY_PATTERN, U as ALL_CODE_EXTENSION_KEYS, V as isShapelessMember, W as ANCHOR_REF_PATTERN, X as DEFAULT_ROOT_NAME, Y as DATE_PATTERN_SOURCE, _ as canBeNull, _t as OFFSET_PATTERN_SOURCE, c as normalizeTypes, d as toPascalCase, ht as NUMERIC_KEYWORDS, j as isNullTypeMember, k as isDefaultOnlyMember, kt as UNICODE_ESCAPE_PATTERN, l as resolveOpenAPIRef, lt as MISSING_DEFINITION_PREFIX, mt as NUMBER_WIRE_PATTERN, ot as INTEGER_WIRE_PATTERN, s as makeSafeKey, t as coerceDefault, tt as FORMAT_PATTERNS, u as toIdentifierPascalCase, v as canBeString, w as hasStringKeyword, wt as STRINGBOOL_PATTERN, y as hasCombinator, z as isSchemaList } from "./utils-Bky98UmP.js";
|
|
2
|
+
//#region src/helper/regex.ts
|
|
3
|
+
/**
|
|
4
|
+
* Build a JavaScript regular-expression literal from a JSON Schema `pattern`.
|
|
5
|
+
*
|
|
6
|
+
* Escapes unescaped `/` so the literal stays delimited, and adds the `u` flag
|
|
7
|
+
* when the pattern uses Unicode property escapes (`\p{...}`, `\P{...}`,
|
|
8
|
+
* `\u{...}`), which TypeScript requires for those constructs (TS1530). A Perl
|
|
9
|
+
* hex escape `\x{HHHH}` is rewritten to `\uHHHH` (or `\u{HHHHH}` above
|
|
10
|
+
* U+FFFF, which also sets `u`): TypeScript rejects `\x{` (TS1125) and, without
|
|
11
|
+
* `u`, V8 treats it as an identity escape. Returns the literal only (`/.../`
|
|
12
|
+
* or `/.../u`); callers append any validator-specific error argument.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* regexLiteral('^[a-z]+$') // '/^[a-z]+$/'
|
|
17
|
+
* regexLiteral('^\\p{L}+$') // '/^\\p{L}+$/u'
|
|
18
|
+
* regexLiteral('a/b') // '/a\\/b/'
|
|
19
|
+
* regexLiteral('\\x{FFFF}') // '/\\uFFFF/'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function regexLiteral(pattern) {
|
|
23
|
+
const rewritten = rewriteBracedHex(pattern);
|
|
24
|
+
return `/${rewritten.pattern.replaceAll(/(?<!\\)\//gu, "\\/")}/${rewritten.unicode || UNICODE_ESCAPE_PATTERN.test(rewritten.pattern) ? "u" : ""}`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The pattern with braced hex escapes (`\x{hex}`) rewritten to JavaScript unicode
|
|
28
|
+
* escapes. A code point above U+FFFF needs the `u` flag (`unicode`). An
|
|
29
|
+
* already-escaped backslash (`\\x{FFFF}`) is left as text.
|
|
30
|
+
*/
|
|
31
|
+
function patternSource(pattern) {
|
|
32
|
+
return rewriteBracedHex(pattern).pattern;
|
|
33
|
+
}
|
|
34
|
+
function rewriteBracedHex(pattern) {
|
|
35
|
+
let unicode = false;
|
|
36
|
+
let out = "";
|
|
37
|
+
for (let index = 0; index < pattern.length; index++) {
|
|
38
|
+
const char = pattern.charAt(index);
|
|
39
|
+
if (char === "\\" && pattern.charAt(index + 1) === "\\") {
|
|
40
|
+
out += "\\\\";
|
|
41
|
+
index += 1;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (char === "\\" && pattern.startsWith("x{", index + 1)) {
|
|
45
|
+
const close = pattern.indexOf("}", index + 3);
|
|
46
|
+
const hex = close > index + 3 ? pattern.slice(index + 3, close) : "";
|
|
47
|
+
if (/^[0-9A-Fa-f]+$/u.test(hex)) {
|
|
48
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
49
|
+
if (codePoint <= 1114111) {
|
|
50
|
+
if (codePoint > 65535) {
|
|
51
|
+
unicode = true;
|
|
52
|
+
out += `\\u{${hex}}`;
|
|
53
|
+
} else out += `\\u${hex.padStart(4, "0")}`;
|
|
54
|
+
index = close;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
out += char;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
pattern: out,
|
|
63
|
+
unicode
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/helper/all-of.ts
|
|
68
|
+
/**
|
|
69
|
+
* An `allOf` member that only carries keywords (`{ minimum: 5 }`, `{ maxLength: 10 }`).
|
|
70
|
+
*
|
|
71
|
+
* It has no type of its own. The keywords still apply to the composed value — a number
|
|
72
|
+
* must meet `minimum`, a string must meet `maxLength` — so dropping the member drops
|
|
73
|
+
* constraints that JSON Schema keeps.
|
|
74
|
+
*/
|
|
75
|
+
const KEYWORD_ONLY_KEYS = /* @__PURE__ */ new Set([
|
|
76
|
+
...NUMERIC_KEYWORDS,
|
|
77
|
+
...STRING_KEYWORDS,
|
|
78
|
+
"x-error-message",
|
|
79
|
+
"x-minimum-message",
|
|
80
|
+
"x-maximum-message",
|
|
81
|
+
"x-exclusiveMinimum-message",
|
|
82
|
+
"x-exclusiveMaximum-message",
|
|
83
|
+
"x-multipleOf-message",
|
|
84
|
+
"x-minLength-message",
|
|
85
|
+
"x-maxLength-message",
|
|
86
|
+
"x-pattern-message",
|
|
87
|
+
"title",
|
|
88
|
+
"description",
|
|
89
|
+
"$comment",
|
|
90
|
+
"examples",
|
|
91
|
+
"example",
|
|
92
|
+
"deprecated",
|
|
93
|
+
"readOnly",
|
|
94
|
+
"writeOnly"
|
|
95
|
+
]);
|
|
96
|
+
/** Whether every key of `schema` is a numeric or string keyword, its message, or an annotation. */
|
|
97
|
+
function isKeywordOnlyMember(schema) {
|
|
98
|
+
const keys = Object.keys(schema);
|
|
99
|
+
if (keys.length === 0) return false;
|
|
100
|
+
if (!keys.every((key) => KEYWORD_ONLY_KEYS.has(key))) return false;
|
|
101
|
+
return hasNumericKeyword(schema) || hasStringKeyword(schema);
|
|
102
|
+
}
|
|
103
|
+
/** The message a keyword-only member should report, keyword messages falling back together. */
|
|
104
|
+
function constraintMessage(schema) {
|
|
105
|
+
return schema["x-error-message"] ?? schema["x-minimum-message"] ?? schema["x-maximum-message"] ?? schema["x-exclusiveMinimum-message"] ?? schema["x-exclusiveMaximum-message"] ?? schema["x-multipleOf-message"] ?? schema["x-minLength-message"] ?? schema["x-maxLength-message"] ?? schema["x-pattern-message"];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The predicate that keeps a keyword-only member's constraints without forcing a type.
|
|
109
|
+
*
|
|
110
|
+
* A number that fails `minimum` is rejected; a string is not a number, so `minimum` does
|
|
111
|
+
* not apply to it. `param` is the value name the caller already uses.
|
|
112
|
+
*/
|
|
113
|
+
function constraintPredicate(schema, param) {
|
|
114
|
+
const parts = [numericGuard(schema, param), stringGuard(schema, param)].filter((part) => part !== "");
|
|
115
|
+
if (parts.length === 0) return "true";
|
|
116
|
+
return parts.join(" && ");
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Fold keyword-only `allOf` members into a sibling whose type they constrain.
|
|
120
|
+
*
|
|
121
|
+
* `{ type: 'integer' }` beside `{ minimum: 5 }` is one integer of at least 5. A keyword
|
|
122
|
+
* with no such sibling stays keyword-only, so the caller emits a type-guarded check. On
|
|
123
|
+
* the string wire a leftover is given the type its keywords imply, and one coercion
|
|
124
|
+
* reads the text.
|
|
125
|
+
*/
|
|
126
|
+
function foldAllOfMembers(members, isWire) {
|
|
127
|
+
let numericAt = -1;
|
|
128
|
+
let stringAt = -1;
|
|
129
|
+
for (const [index, member] of members.entries()) {
|
|
130
|
+
if (numericAt === -1 && isNumericHost(member)) numericAt = index;
|
|
131
|
+
if (stringAt === -1 && isStringHost(member)) stringAt = index;
|
|
132
|
+
}
|
|
133
|
+
const next = [...members];
|
|
134
|
+
const numericLeftovers = [];
|
|
135
|
+
const stringLeftovers = [];
|
|
136
|
+
const drop = /* @__PURE__ */ new Set();
|
|
137
|
+
for (const [index, member] of members.entries()) {
|
|
138
|
+
if (!isKeywordOnlyMember(member)) continue;
|
|
139
|
+
drop.add(index);
|
|
140
|
+
const numeric = numericPart(member);
|
|
141
|
+
const text = stringPart(member);
|
|
142
|
+
if (numeric !== void 0) {
|
|
143
|
+
const host = numericAt === -1 ? void 0 : next[numericAt];
|
|
144
|
+
const absorbed = host === void 0 ? void 0 : tryAbsorbNumeric(host, numeric);
|
|
145
|
+
if (host !== void 0 && absorbed !== void 0) next[numericAt] = absorbed;
|
|
146
|
+
else numericLeftovers.push(numeric);
|
|
147
|
+
}
|
|
148
|
+
if (text !== void 0) {
|
|
149
|
+
const host = stringAt === -1 ? void 0 : next[stringAt];
|
|
150
|
+
const absorbed = host === void 0 ? void 0 : tryAbsorbString(host, text);
|
|
151
|
+
if (host !== void 0 && absorbed !== void 0) next[stringAt] = absorbed;
|
|
152
|
+
else stringLeftovers.push(text);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const kept = [];
|
|
156
|
+
for (const [index, member] of next.entries()) if (!drop.has(index)) kept.push(member);
|
|
157
|
+
return [
|
|
158
|
+
...kept,
|
|
159
|
+
...collapse(numericLeftovers, "number", isWire),
|
|
160
|
+
...collapse(stringLeftovers, "string", isWire)
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
function isNumericHost(schema) {
|
|
164
|
+
if (schema.type !== "number" && schema.type !== "integer") return false;
|
|
165
|
+
if (schema.$ref !== void 0 || hasCombinator(schema)) return false;
|
|
166
|
+
if (schema.properties !== void 0 || schema.enum !== void 0 || "const" in schema) return false;
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
function isStringHost(schema) {
|
|
170
|
+
if (schema.type !== "string") return false;
|
|
171
|
+
if (schema.$ref !== void 0 || hasCombinator(schema)) return false;
|
|
172
|
+
if (schema.properties !== void 0 || schema.enum !== void 0 || "const" in schema) return false;
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
function numericPart(schema) {
|
|
176
|
+
if (!hasNumericKeyword(schema)) return void 0;
|
|
177
|
+
return {
|
|
178
|
+
...schema.minimum !== void 0 ? { minimum: schema.minimum } : {},
|
|
179
|
+
...schema.maximum !== void 0 ? { maximum: schema.maximum } : {},
|
|
180
|
+
...schema.exclusiveMinimum !== void 0 ? { exclusiveMinimum: schema.exclusiveMinimum } : {},
|
|
181
|
+
...schema.exclusiveMaximum !== void 0 ? { exclusiveMaximum: schema.exclusiveMaximum } : {},
|
|
182
|
+
...schema.multipleOf !== void 0 ? { multipleOf: schema.multipleOf } : {},
|
|
183
|
+
...schema["x-minimum-message"] !== void 0 ? { "x-minimum-message": schema["x-minimum-message"] } : {},
|
|
184
|
+
...schema["x-maximum-message"] !== void 0 ? { "x-maximum-message": schema["x-maximum-message"] } : {},
|
|
185
|
+
...schema["x-exclusiveMinimum-message"] !== void 0 ? { "x-exclusiveMinimum-message": schema["x-exclusiveMinimum-message"] } : {},
|
|
186
|
+
...schema["x-exclusiveMaximum-message"] !== void 0 ? { "x-exclusiveMaximum-message": schema["x-exclusiveMaximum-message"] } : {},
|
|
187
|
+
...schema["x-multipleOf-message"] !== void 0 ? { "x-multipleOf-message": schema["x-multipleOf-message"] } : {}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function stringPart(schema) {
|
|
191
|
+
if (!hasStringKeyword(schema)) return void 0;
|
|
192
|
+
return {
|
|
193
|
+
...schema.minLength !== void 0 ? { minLength: schema.minLength } : {},
|
|
194
|
+
...schema.maxLength !== void 0 ? { maxLength: schema.maxLength } : {},
|
|
195
|
+
...schema.pattern !== void 0 ? { pattern: schema.pattern } : {},
|
|
196
|
+
...schema["x-minLength-message"] !== void 0 ? { "x-minLength-message": schema["x-minLength-message"] } : {},
|
|
197
|
+
...schema["x-maxLength-message"] !== void 0 ? { "x-maxLength-message": schema["x-maxLength-message"] } : {},
|
|
198
|
+
...schema["x-pattern-message"] !== void 0 ? { "x-pattern-message": schema["x-pattern-message"] } : {}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function tryAbsorbNumeric(host, extra) {
|
|
202
|
+
if (!canAbsorbMultipleOf(host, extra)) return void 0;
|
|
203
|
+
return absorbMultipleOf(absorbUpper(absorbLower(host, extra), extra), extra);
|
|
204
|
+
}
|
|
205
|
+
function tryAbsorbString(host, extra) {
|
|
206
|
+
if (typeof extra.pattern === "string" && typeof host.pattern === "string" && extra.pattern !== host.pattern) return;
|
|
207
|
+
return absorbString(host, extra);
|
|
208
|
+
}
|
|
209
|
+
function canAbsorbMultipleOf(host, extra) {
|
|
210
|
+
if (typeof extra.multipleOf !== "number" || typeof host.multipleOf !== "number") return true;
|
|
211
|
+
if (host.multipleOf === extra.multipleOf) return true;
|
|
212
|
+
return leastCommonMultiple(host.multipleOf, extra.multipleOf) !== void 0;
|
|
213
|
+
}
|
|
214
|
+
function absorbLower(host, extra) {
|
|
215
|
+
const extraBound = lowerBound(extra);
|
|
216
|
+
if (extraBound === void 0) return host;
|
|
217
|
+
const hostBound = lowerBound(host);
|
|
218
|
+
if (hostBound !== void 0 && !isStricter(extraBound, hostBound, "lower")) return host;
|
|
219
|
+
return withLower(host, extraBound, boundMessage(extra, extraBound, "lower"));
|
|
220
|
+
}
|
|
221
|
+
function absorbUpper(host, extra) {
|
|
222
|
+
const extraBound = upperBound(extra);
|
|
223
|
+
if (extraBound === void 0) return host;
|
|
224
|
+
const hostBound = upperBound(host);
|
|
225
|
+
if (hostBound !== void 0 && !isStricter(extraBound, hostBound, "upper")) return host;
|
|
226
|
+
return withUpper(host, extraBound, boundMessage(extra, extraBound, "upper"));
|
|
227
|
+
}
|
|
228
|
+
function absorbMultipleOf(host, extra) {
|
|
229
|
+
if (typeof extra.multipleOf !== "number") return host;
|
|
230
|
+
if (typeof host.multipleOf !== "number") return {
|
|
231
|
+
...host,
|
|
232
|
+
multipleOf: extra.multipleOf,
|
|
233
|
+
...extra["x-multipleOf-message"] !== void 0 ? { "x-multipleOf-message": extra["x-multipleOf-message"] } : {}
|
|
234
|
+
};
|
|
235
|
+
if (host.multipleOf === extra.multipleOf) return host;
|
|
236
|
+
const multiple = leastCommonMultiple(host.multipleOf, extra.multipleOf);
|
|
237
|
+
if (multiple === void 0) return host;
|
|
238
|
+
const { "x-multipleOf-message": message, ...rest } = host;
|
|
239
|
+
return {
|
|
240
|
+
...rest,
|
|
241
|
+
multipleOf: multiple
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function absorbString(host, extra) {
|
|
245
|
+
let next = host;
|
|
246
|
+
if (typeof extra.minLength === "number" && (next.minLength === void 0 || extra.minLength > next.minLength)) next = {
|
|
247
|
+
...next,
|
|
248
|
+
minLength: extra.minLength,
|
|
249
|
+
...extra["x-minLength-message"] !== void 0 ? { "x-minLength-message": extra["x-minLength-message"] } : {}
|
|
250
|
+
};
|
|
251
|
+
if (typeof extra.maxLength === "number" && (next.maxLength === void 0 || extra.maxLength < next.maxLength)) next = {
|
|
252
|
+
...next,
|
|
253
|
+
maxLength: extra.maxLength,
|
|
254
|
+
...extra["x-maxLength-message"] !== void 0 ? { "x-maxLength-message": extra["x-maxLength-message"] } : {}
|
|
255
|
+
};
|
|
256
|
+
if (typeof extra.pattern === "string" && next.pattern === void 0) next = {
|
|
257
|
+
...next,
|
|
258
|
+
pattern: extra.pattern,
|
|
259
|
+
...extra["x-pattern-message"] !== void 0 ? { "x-pattern-message": extra["x-pattern-message"] } : {}
|
|
260
|
+
};
|
|
261
|
+
return next;
|
|
262
|
+
}
|
|
263
|
+
function collapse(parts, kind, isWire) {
|
|
264
|
+
const groups = [];
|
|
265
|
+
for (const part of parts) {
|
|
266
|
+
const last = groups.at(-1);
|
|
267
|
+
const absorbed = last === void 0 ? void 0 : kind === "number" ? tryAbsorbNumeric(last, part) : tryAbsorbString(last, part);
|
|
268
|
+
if (last === void 0 || absorbed === void 0) {
|
|
269
|
+
groups.push(isWire ? {
|
|
270
|
+
...part,
|
|
271
|
+
type: kind
|
|
272
|
+
} : part);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
groups[groups.length - 1] = absorbed;
|
|
276
|
+
}
|
|
277
|
+
return groups;
|
|
278
|
+
}
|
|
279
|
+
function lowerBound(schema) {
|
|
280
|
+
if (typeof schema.exclusiveMinimum === "number") return {
|
|
281
|
+
value: schema.exclusiveMinimum,
|
|
282
|
+
exclusive: true
|
|
283
|
+
};
|
|
284
|
+
if (typeof schema.minimum === "number") return {
|
|
285
|
+
value: schema.minimum,
|
|
286
|
+
exclusive: schema.exclusiveMinimum === true
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
function upperBound(schema) {
|
|
290
|
+
if (typeof schema.exclusiveMaximum === "number") return {
|
|
291
|
+
value: schema.exclusiveMaximum,
|
|
292
|
+
exclusive: true
|
|
293
|
+
};
|
|
294
|
+
if (typeof schema.maximum === "number") return {
|
|
295
|
+
value: schema.maximum,
|
|
296
|
+
exclusive: schema.exclusiveMaximum === true
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function isStricter(next, current, direction) {
|
|
300
|
+
if (next.value !== current.value) return direction === "lower" ? next.value > current.value : next.value < current.value;
|
|
301
|
+
return next.exclusive && !current.exclusive;
|
|
302
|
+
}
|
|
303
|
+
function boundMessage(schema, bound, direction) {
|
|
304
|
+
if (direction === "lower") return bound.exclusive ? schema["x-exclusiveMinimum-message"] : schema["x-minimum-message"];
|
|
305
|
+
return bound.exclusive ? schema["x-exclusiveMaximum-message"] : schema["x-maximum-message"];
|
|
306
|
+
}
|
|
307
|
+
function dropLower(schema) {
|
|
308
|
+
const { minimum, exclusiveMinimum, "x-minimum-message": minimumMessage, "x-exclusiveMinimum-message": exclusiveMinimumMessage, ...rest } = schema;
|
|
309
|
+
return rest;
|
|
310
|
+
}
|
|
311
|
+
function dropUpper(schema) {
|
|
312
|
+
const { maximum, exclusiveMaximum, "x-maximum-message": maximumMessage, "x-exclusiveMaximum-message": exclusiveMaximumMessage, ...rest } = schema;
|
|
313
|
+
return rest;
|
|
314
|
+
}
|
|
315
|
+
function withLower(schema, bound, message) {
|
|
316
|
+
const base = dropLower(schema);
|
|
317
|
+
if (bound.exclusive) return {
|
|
318
|
+
...base,
|
|
319
|
+
exclusiveMinimum: bound.value,
|
|
320
|
+
...message !== void 0 ? { "x-exclusiveMinimum-message": message } : {}
|
|
321
|
+
};
|
|
322
|
+
return {
|
|
323
|
+
...base,
|
|
324
|
+
minimum: bound.value,
|
|
325
|
+
...message !== void 0 ? { "x-minimum-message": message } : {}
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
function withUpper(schema, bound, message) {
|
|
329
|
+
const base = dropUpper(schema);
|
|
330
|
+
if (bound.exclusive) return {
|
|
331
|
+
...base,
|
|
332
|
+
exclusiveMaximum: bound.value,
|
|
333
|
+
...message !== void 0 ? { "x-exclusiveMaximum-message": message } : {}
|
|
334
|
+
};
|
|
335
|
+
return {
|
|
336
|
+
...base,
|
|
337
|
+
maximum: bound.value,
|
|
338
|
+
...message !== void 0 ? { "x-maximum-message": message } : {}
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function leastCommonMultiple(left, right) {
|
|
342
|
+
if (!Number.isSafeInteger(left) || !Number.isSafeInteger(right) || left === 0 || right === 0) return;
|
|
343
|
+
const divisor = greatestCommonDivisor(Math.abs(left), Math.abs(right));
|
|
344
|
+
const multiple = Math.abs(left) / divisor * Math.abs(right);
|
|
345
|
+
if (!Number.isSafeInteger(multiple)) return void 0;
|
|
346
|
+
return multiple;
|
|
347
|
+
}
|
|
348
|
+
function greatestCommonDivisor(left, right) {
|
|
349
|
+
let x = left;
|
|
350
|
+
let y = right;
|
|
351
|
+
while (y !== 0) {
|
|
352
|
+
const next = x % y;
|
|
353
|
+
x = y;
|
|
354
|
+
y = next;
|
|
355
|
+
}
|
|
356
|
+
return x;
|
|
357
|
+
}
|
|
358
|
+
function numericGuard(schema, param) {
|
|
359
|
+
const checks = [];
|
|
360
|
+
if (typeof schema.minimum === "number" && schema.exclusiveMinimum !== true) checks.push(`${param} >= ${schema.minimum}`);
|
|
361
|
+
if (schema.exclusiveMinimum === true && typeof schema.minimum === "number") checks.push(`${param} > ${schema.minimum}`);
|
|
362
|
+
if (typeof schema.exclusiveMinimum === "number") checks.push(`${param} > ${schema.exclusiveMinimum}`);
|
|
363
|
+
if (typeof schema.maximum === "number" && schema.exclusiveMaximum !== true) checks.push(`${param} <= ${schema.maximum}`);
|
|
364
|
+
if (schema.exclusiveMaximum === true && typeof schema.maximum === "number") checks.push(`${param} < ${schema.maximum}`);
|
|
365
|
+
if (typeof schema.exclusiveMaximum === "number") checks.push(`${param} < ${schema.exclusiveMaximum}`);
|
|
366
|
+
if (typeof schema.multipleOf === "number") checks.push(`${param} % ${schema.multipleOf} === 0`);
|
|
367
|
+
if (checks.length === 0) return "";
|
|
368
|
+
return `(typeof ${param} !== 'number' || (${checks.join(" && ")}))`;
|
|
369
|
+
}
|
|
370
|
+
function stringGuard(schema, param) {
|
|
371
|
+
const checks = [];
|
|
372
|
+
if (typeof schema.minLength === "number") checks.push(`${param}.length >= ${schema.minLength}`);
|
|
373
|
+
if (typeof schema.maxLength === "number") checks.push(`${param}.length <= ${schema.maxLength}`);
|
|
374
|
+
if (typeof schema.pattern === "string") checks.push(`${regexLiteral(schema.pattern)}.test(${param})`);
|
|
375
|
+
if (checks.length === 0) return "";
|
|
376
|
+
return `(typeof ${param} !== 'string' || (${checks.join(" && ")}))`;
|
|
377
|
+
}
|
|
378
|
+
//#endregion
|
|
2
379
|
//#region src/helper/walk.ts
|
|
3
380
|
/**
|
|
4
381
|
* Every object node under `root`, depth first, the root first.
|
|
@@ -107,6 +484,24 @@ function readCodeExtension(schema, key, options) {
|
|
|
107
484
|
//#endregion
|
|
108
485
|
//#region src/helper/arktype-wrap.ts
|
|
109
486
|
/**
|
|
487
|
+
* A definition a callback checks values against at validation time. Inside
|
|
488
|
+
* `scope({...})` it goes through the scope (bound to `$`), which resolves the aliases
|
|
489
|
+
* it may name; the global `type(...)` cannot. An expression that already evaluates to a
|
|
490
|
+
* `Type` is used as it is.
|
|
491
|
+
*/
|
|
492
|
+
function callbackType(source, scopeMode) {
|
|
493
|
+
if (scopeMode) return isArktypeDefinition(source) ? `$.type(${source})` : source;
|
|
494
|
+
return isQuoted(source) ? `type(${source})` : source;
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* A callback that reaches the scope through `$`: its return type is spelled out, since
|
|
498
|
+
* `$` is still being declared where the callback is written and TypeScript would
|
|
499
|
+
* otherwise infer the scope's type from itself.
|
|
500
|
+
*/
|
|
501
|
+
function callbackReturn(params, body) {
|
|
502
|
+
return `${params}${body.includes("$.type(") ? ": boolean" : ""} => ${body}`;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
110
505
|
* `inner` read after `readers`, each a `(data) => ...` arrow over the raw value: a morph
|
|
111
506
|
* from `input` that pipes into `inner`, with `.to()` for a bare definition.
|
|
112
507
|
*/
|
|
@@ -178,16 +573,16 @@ function examplesOf(schema) {
|
|
|
178
573
|
* Whether a node carries a `default` the generators keep, so a property holding it may be
|
|
179
574
|
* left out of the input.
|
|
180
575
|
*/
|
|
181
|
-
function hasKeptDefault(schema) {
|
|
182
|
-
return schema.default !== void 0 && coerceDefault(schema, schema.default).keep;
|
|
576
|
+
function hasKeptDefault(schema, definitions) {
|
|
577
|
+
return schema.default !== void 0 && coerceDefault(schema, schema.default, definitions).keep;
|
|
183
578
|
}
|
|
184
579
|
/**
|
|
185
580
|
* Whether a node's own emitter already makes it optional with a default: a kept `default`,
|
|
186
581
|
* or an `allOf` member that only carries one. An object wrapping such a property again
|
|
187
582
|
* would hide the default behind the outer wrapper.
|
|
188
583
|
*/
|
|
189
|
-
function isOptionalByDefault(schema) {
|
|
190
|
-
return hasKeptDefault(schema) || schema.allOf?.some(isDefaultOnlyMember) === true;
|
|
584
|
+
function isOptionalByDefault(schema, definitions) {
|
|
585
|
+
return hasKeptDefault(schema, definitions) || schema.allOf?.some(isDefaultOnlyMember) === true;
|
|
191
586
|
}
|
|
192
587
|
//#endregion
|
|
193
588
|
//#region src/helper/cyclic.ts
|
|
@@ -282,6 +677,42 @@ function cyclicDefinitionNames(schema) {
|
|
|
282
677
|
return cyclic;
|
|
283
678
|
}
|
|
284
679
|
//#endregion
|
|
680
|
+
//#region src/helper/discriminator.ts
|
|
681
|
+
function definitionName(ref) {
|
|
682
|
+
for (const prefix of DEFINITION_REF_PREFIXES) {
|
|
683
|
+
if (!ref.startsWith(prefix)) continue;
|
|
684
|
+
const rest = ref.slice(prefix.length);
|
|
685
|
+
if (rest === "" || rest.includes("/")) return void 0;
|
|
686
|
+
return decodeURIComponent(rest);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* The discriminator values that select each `oneOf` / `anyOf` member (OpenAPI 3.x
|
|
691
|
+
* Discriminator Object): the `mapping` keys whose target names the member's `$ref`, or,
|
|
692
|
+
* when the mapping names it nowhere, the definition name itself (the implicit mapping).
|
|
693
|
+
* `undefined` when the node has no discriminator property.
|
|
694
|
+
*/
|
|
695
|
+
function discriminatedMembers(schema, members) {
|
|
696
|
+
const propertyName = schema.discriminator?.propertyName;
|
|
697
|
+
if (propertyName === void 0) return void 0;
|
|
698
|
+
const mapping = Object.entries(schema.discriminator?.mapping ?? {});
|
|
699
|
+
return {
|
|
700
|
+
propertyName,
|
|
701
|
+
members: members.map((member) => {
|
|
702
|
+
const name = member.$ref === void 0 ? void 0 : definitionName(member.$ref);
|
|
703
|
+
if (name === void 0) return {
|
|
704
|
+
member,
|
|
705
|
+
values: void 0
|
|
706
|
+
};
|
|
707
|
+
const mapped = mapping.filter(([, target]) => target === member.$ref || definitionName(target) === name).map(([value]) => value);
|
|
708
|
+
return {
|
|
709
|
+
member,
|
|
710
|
+
values: mapped.length > 0 ? mapped : [name]
|
|
711
|
+
};
|
|
712
|
+
})
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
//#endregion
|
|
285
716
|
//#region src/helper/resolve-schema-dependencies-from-schema.ts
|
|
286
717
|
/** The definition name the fragment of an external `$ref` points at, if it names one. */
|
|
287
718
|
function fragmentDefinitionName(fragment) {
|
|
@@ -356,7 +787,20 @@ function emitOptionsOf(options) {
|
|
|
356
787
|
...options?.style !== void 0 && { style: options.style },
|
|
357
788
|
...options?.explode !== void 0 && { explode: options.explode },
|
|
358
789
|
...options?.allowEmptyValue !== void 0 && { allowEmptyValue: options.allowEmptyValue },
|
|
359
|
-
...options?.contentType !== void 0 && { contentType: options.contentType }
|
|
790
|
+
...options?.contentType !== void 0 && { contentType: options.contentType },
|
|
791
|
+
...options?.properties !== void 0 && { properties: options.properties },
|
|
792
|
+
...options?.definitions !== void 0 && { definitions: options.definitions }
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* `options.definitions` plus the document's own `$defs` / `definitions`. The document
|
|
797
|
+
* wins on a shared name. `undefined` when neither holds a schema.
|
|
798
|
+
*/
|
|
799
|
+
function definitionsOf(options, document) {
|
|
800
|
+
if (Object.keys(document).length === 0 && options?.definitions === void 0) return void 0;
|
|
801
|
+
return {
|
|
802
|
+
...options?.definitions,
|
|
803
|
+
...document
|
|
360
804
|
};
|
|
361
805
|
}
|
|
362
806
|
/**
|
|
@@ -442,9 +886,9 @@ function typeboxMetaOpts(schema, ref) {
|
|
|
442
886
|
* `stringWire` (for `query`/`path` coercion) emits the default in its string-wire
|
|
443
887
|
* form (`'1'` / `'true'`), since the coerced `Type.String(...)` input is a string.
|
|
444
888
|
*/
|
|
445
|
-
function typeboxDefaultOpt(schema, stringWire = false) {
|
|
889
|
+
function typeboxDefaultOpt(schema, stringWire = false, definitions) {
|
|
446
890
|
if (schema.default === void 0) return [];
|
|
447
|
-
const result = coerceDefault(schema, schema.default);
|
|
891
|
+
const result = coerceDefault(schema, schema.default, definitions);
|
|
448
892
|
if (!result.keep) return [];
|
|
449
893
|
const value = stringWire ? String(result.value) : result.value;
|
|
450
894
|
return [`default:${typeof value === "boolean" || typeof value === "number" ? `${value}` : JSON.stringify(value)}`];
|
|
@@ -614,6 +1058,27 @@ function wireNullText(param) {
|
|
|
614
1058
|
function ownProperties(param) {
|
|
615
1059
|
return `(${param})=>(typeof ${param}==="object"&&${param}!==null&&!Array.isArray(${param})?Object.assign(Object.create(null),${param}):${param})`;
|
|
616
1060
|
}
|
|
1061
|
+
/** The regex literal of integer text on the wire. */
|
|
1062
|
+
function integerWirePattern() {
|
|
1063
|
+
return regexSource(INTEGER_WIRE_PATTERN);
|
|
1064
|
+
}
|
|
1065
|
+
/** The regex literal of JSON number text on the wire. */
|
|
1066
|
+
function numberWirePattern() {
|
|
1067
|
+
return regexSource(NUMBER_WIRE_PATTERN);
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Whether wire text for these literals is an integer or a number. An integer rejects
|
|
1071
|
+
* exponents and fractions; a number follows the JSON number grammar.
|
|
1072
|
+
*/
|
|
1073
|
+
function wireNumericKind(schema, values) {
|
|
1074
|
+
const types = schema.type;
|
|
1075
|
+
if (types === "number") return "number";
|
|
1076
|
+
if (types === "integer") return "integer";
|
|
1077
|
+
if (Array.isArray(types) && types.includes("number")) return "number";
|
|
1078
|
+
if (Array.isArray(types) && types.includes("integer")) return "integer";
|
|
1079
|
+
if (values.every((value) => typeof value === "number" && Number.isInteger(value))) return "integer";
|
|
1080
|
+
return "number";
|
|
1081
|
+
}
|
|
617
1082
|
/** The regex literal of every spelling of a boolean on the wire. */
|
|
618
1083
|
function stringboolPattern() {
|
|
619
1084
|
return regexSource(STRINGBOOL_PATTERN);
|
|
@@ -675,7 +1140,7 @@ function effectWrap(effectStr, schema, options) {
|
|
|
675
1140
|
readCodeExtension(schema, "x-pipe", options)
|
|
676
1141
|
].filter((v) => v !== void 0);
|
|
677
1142
|
const withCodeExts = codeChain.length === 0 ? withBrand : `${withBrand}${codeChain.join("")}`;
|
|
678
|
-
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default) : void 0;
|
|
1143
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
679
1144
|
const decodingDefault = options?.stringWire === true ? "Schema.withDecodingDefaultType" : "Schema.withDecodingDefault";
|
|
680
1145
|
const withDefault = defaultResult?.keep ? `${withCodeExts}.pipe(${decodingDefault}(Effect.succeed(${defaultLiteral$1(schema, defaultResult.value)})))` : withCodeExts;
|
|
681
1146
|
const examples = examplesOf(schema);
|
|
@@ -690,7 +1155,124 @@ function effectWrap(effectStr, schema, options) {
|
|
|
690
1155
|
return `${withDefault}.annotate(${serializeJSValue(ann)})`;
|
|
691
1156
|
}
|
|
692
1157
|
//#endregion
|
|
1158
|
+
//#region src/helper/hoist-refs.ts
|
|
1159
|
+
/** Keywords whose values are data, not schemas: a `$ref` string there is not a reference. */
|
|
1160
|
+
const DATA_KEYWORDS = /* @__PURE__ */ new Set([
|
|
1161
|
+
"const",
|
|
1162
|
+
"default",
|
|
1163
|
+
"enum",
|
|
1164
|
+
"example",
|
|
1165
|
+
"examples"
|
|
1166
|
+
]);
|
|
1167
|
+
/** Whether a `$ref` already names a definition directly (`#/$defs/X`). */
|
|
1168
|
+
function isDefinitionRef(ref) {
|
|
1169
|
+
return DEFINITION_REF_PREFIXES.some((prefix) => {
|
|
1170
|
+
if (!ref.startsWith(prefix)) return false;
|
|
1171
|
+
const rest = ref.slice(prefix.length);
|
|
1172
|
+
return rest !== "" && !rest.includes("/");
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
/** A local JSON Pointer `$ref` deeper than a definition: `#/$defs/A/properties/b`. */
|
|
1176
|
+
function isNestedPointer(ref) {
|
|
1177
|
+
return ref.startsWith("#/") && !isDefinitionRef(ref);
|
|
1178
|
+
}
|
|
1179
|
+
function decodeToken(token) {
|
|
1180
|
+
return decodeURIComponent(token).replaceAll("~1", "/").replaceAll("~0", "~");
|
|
1181
|
+
}
|
|
1182
|
+
function resolvePointer(document, ref) {
|
|
1183
|
+
let node = document;
|
|
1184
|
+
for (const token of ref.slice(2).split("/").map(decodeToken)) {
|
|
1185
|
+
if (typeof node !== "object" || node === null || !Object.hasOwn(node, token)) return void 0;
|
|
1186
|
+
node = Reflect.get(node, token);
|
|
1187
|
+
}
|
|
1188
|
+
return node;
|
|
1189
|
+
}
|
|
1190
|
+
function collectNestedPointers(node, found) {
|
|
1191
|
+
if (Array.isArray(node)) {
|
|
1192
|
+
for (const item of node) collectNestedPointers(item, found);
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
if (typeof node !== "object" || node === null) return;
|
|
1196
|
+
for (const [key, value] of Object.entries(node)) if (key === "$ref" && typeof value === "string" && isNestedPointer(value)) found.add(value);
|
|
1197
|
+
else if (!DATA_KEYWORDS.has(key)) collectNestedPointers(value, found);
|
|
1198
|
+
}
|
|
1199
|
+
/** A JSON object read as a schema node: what a pointer reaches or a rewrite returns. */
|
|
1200
|
+
function isSchemaNode(value) {
|
|
1201
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1202
|
+
}
|
|
1203
|
+
function rewriteRefs(node, refs) {
|
|
1204
|
+
if (Array.isArray(node)) return node.map((item) => rewriteRefs(item, refs));
|
|
1205
|
+
if (typeof node !== "object" || node === null) return node;
|
|
1206
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => {
|
|
1207
|
+
if (key === "$ref" && typeof value === "string") return [key, refs.get(value) ?? value];
|
|
1208
|
+
return [key, DATA_KEYWORDS.has(key) ? value : rewriteRefs(value, refs)];
|
|
1209
|
+
}));
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Hoist every schema a `$ref` reaches through a nested JSON Pointer into a definition of
|
|
1213
|
+
* its own, and point the references at it.
|
|
1214
|
+
*
|
|
1215
|
+
* Bundling an external file leaves its `definitions` where the file was referenced, so a
|
|
1216
|
+
* reference inside it reads `#/$defs/Root/properties/root/definitions/Node`. The
|
|
1217
|
+
* generators name and declare top-level definitions only; a recursive schema reached that
|
|
1218
|
+
* way would otherwise be referenced by a name nothing declares. A hoisted definition is
|
|
1219
|
+
* named after the pointer's last token, numbered when the name is taken.
|
|
1220
|
+
*
|
|
1221
|
+
* Returns the document as given when it has no such reference.
|
|
1222
|
+
*/
|
|
1223
|
+
function hoistNestedRefs(schema) {
|
|
1224
|
+
const pointers = /* @__PURE__ */ new Set();
|
|
1225
|
+
collectNestedPointers(schema, pointers);
|
|
1226
|
+
if (pointers.size === 0) return schema;
|
|
1227
|
+
const container = schema.$defs === void 0 && schema.definitions !== void 0 ? "definitions" : "$defs";
|
|
1228
|
+
const taken = /* @__PURE__ */ new Set([...Object.keys(schema.$defs ?? {}), ...Object.keys(schema.definitions ?? {})]);
|
|
1229
|
+
const refs = /* @__PURE__ */ new Map();
|
|
1230
|
+
const hoisted = [];
|
|
1231
|
+
for (const pointer of pointers) {
|
|
1232
|
+
const target = resolvePointer(schema, pointer);
|
|
1233
|
+
if (!isSchemaNode(target)) continue;
|
|
1234
|
+
const base = pointer.slice(2).split("/").map(decodeToken).at(-1) ?? "Definition";
|
|
1235
|
+
let name = base;
|
|
1236
|
+
for (let index = 2; taken.has(name); index += 1) name = `${base}${index}`;
|
|
1237
|
+
taken.add(name);
|
|
1238
|
+
refs.set(pointer, `#/${container}/${encodeURIComponent(name)}`);
|
|
1239
|
+
hoisted.push([name, target]);
|
|
1240
|
+
}
|
|
1241
|
+
const rewritten = rewriteRefs(schema, refs);
|
|
1242
|
+
if (refs.size === 0 || !isSchemaNode(rewritten)) return schema;
|
|
1243
|
+
const added = {};
|
|
1244
|
+
for (const [name, target] of hoisted) {
|
|
1245
|
+
const node = rewriteRefs(target, refs);
|
|
1246
|
+
if (isSchemaNode(node)) added[name] = node;
|
|
1247
|
+
}
|
|
1248
|
+
return {
|
|
1249
|
+
...rewritten,
|
|
1250
|
+
[container]: {
|
|
1251
|
+
...rewritten[container],
|
|
1252
|
+
...added
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
//#endregion
|
|
693
1257
|
//#region src/helper/not.ts
|
|
1258
|
+
/** Whether the schema's values are bigints (`int64`, `uint64`, `bigint`). */
|
|
1259
|
+
function isBigintFormat(schema) {
|
|
1260
|
+
return schema.format === "int64" || schema.format === "uint64" || schema.format === "bigint";
|
|
1261
|
+
}
|
|
1262
|
+
/** A value as a TypeScript literal. Numbers of a bigint schema are `0n`, not `0`. */
|
|
1263
|
+
function literal(value, bigint) {
|
|
1264
|
+
if (bigint && typeof value === "number" && Number.isFinite(value)) return `${value}n`;
|
|
1265
|
+
return JSON.stringify(value);
|
|
1266
|
+
}
|
|
1267
|
+
/**
|
|
1268
|
+
* An `enum` as an array literal. A bigint schema's numbers are bigint literals, so
|
|
1269
|
+
* `[0n].includes(val)` type-checks and rejects `0n`; `[0].includes(0n)` is both a
|
|
1270
|
+
* type error and always false.
|
|
1271
|
+
*/
|
|
1272
|
+
function valueListLiteral(values, bigint) {
|
|
1273
|
+
if (!bigint) return JSON.stringify(values);
|
|
1274
|
+
return `[${values.map((value) => literal(value, true)).join(",")}]`;
|
|
1275
|
+
}
|
|
694
1276
|
/** The `not` predicate bodies by `type`, over a value bound to `param`. */
|
|
695
1277
|
function notTypeBody(type, param) {
|
|
696
1278
|
switch (type) {
|
|
@@ -708,11 +1290,12 @@ function notTypeBody(type, param) {
|
|
|
708
1290
|
* The predicate refusing what a `not` schema describes, as the source of an arrow over
|
|
709
1291
|
* `param` (declared as `parameter`, which may carry a type annotation): a `const` is
|
|
710
1292
|
* refused by value, a `type` by its runtime check, a list of types by every check at
|
|
711
|
-
* once. `
|
|
712
|
-
* combinator, which each generator
|
|
1293
|
+
* once. `bigint` spells a numeric `const` as a bigint literal. `undefined` for a `not`
|
|
1294
|
+
* these shared cases do not cover — an `enum` or a combinator, which each generator
|
|
1295
|
+
* spells with its own parser.
|
|
713
1296
|
*/
|
|
714
|
-
function notPredicate(inner, param, parameter = param) {
|
|
715
|
-
if ("const" in inner) return `(${parameter}) => ${param} !== ${
|
|
1297
|
+
function notPredicate(inner, param, parameter = param, bigint = false) {
|
|
1298
|
+
if ("const" in inner) return `(${parameter}) => ${param} !== ${literal(inner.const, bigint)}`;
|
|
716
1299
|
if (typeof inner.type === "string") {
|
|
717
1300
|
const body = notTypeBody(inner.type, param);
|
|
718
1301
|
return body === void 0 ? void 0 : `(${parameter}) => ${body}`;
|
|
@@ -752,7 +1335,33 @@ function withoutParameterSerialization(options) {
|
|
|
752
1335
|
...options?.openapi !== void 0 && { openapi: options.openapi },
|
|
753
1336
|
...options?.readonly !== void 0 && { readonly: options.readonly },
|
|
754
1337
|
...options?.unsafeCodeExtensions !== void 0 && { unsafeCodeExtensions: options.unsafeCodeExtensions },
|
|
755
|
-
...options?.paramIn !== void 0 && !isJson && { paramIn: options.paramIn }
|
|
1338
|
+
...options?.paramIn !== void 0 && !isJson && { paramIn: options.paramIn },
|
|
1339
|
+
...options?.definitions !== void 0 && { definitions: options.definitions },
|
|
1340
|
+
...options?.properties !== void 0 && { properties: options.properties }
|
|
1341
|
+
};
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* The emit options of one property, when that property carries its own serialization
|
|
1345
|
+
* (`x-style` / `x-explode` / `x-allowEmptyValue`, or `options.properties`). `undefined`
|
|
1346
|
+
* when it does not, so the caller keeps the object's options. Requires `paramIn`: the
|
|
1347
|
+
* keywords describe wire text.
|
|
1348
|
+
*/
|
|
1349
|
+
function propertyEmitOptions(key, property, options) {
|
|
1350
|
+
if (options?.paramIn === void 0) return void 0;
|
|
1351
|
+
const named = options.properties?.[key];
|
|
1352
|
+
const style = named?.style ?? property["x-style"];
|
|
1353
|
+
const explode = named?.explode ?? property["x-explode"];
|
|
1354
|
+
const allowEmptyValue = named?.allowEmptyValue === true || property["x-allowEmptyValue"] === true;
|
|
1355
|
+
if (style === void 0 && explode === void 0 && !allowEmptyValue) return void 0;
|
|
1356
|
+
return {
|
|
1357
|
+
...options.openapi !== void 0 && { openapi: options.openapi },
|
|
1358
|
+
...options.readonly !== void 0 && { readonly: options.readonly },
|
|
1359
|
+
...options.unsafeCodeExtensions !== void 0 && { unsafeCodeExtensions: options.unsafeCodeExtensions },
|
|
1360
|
+
...options.definitions !== void 0 && { definitions: options.definitions },
|
|
1361
|
+
paramIn: options.paramIn,
|
|
1362
|
+
...style !== void 0 && { style },
|
|
1363
|
+
...explode !== void 0 && { explode },
|
|
1364
|
+
...allowEmptyValue && { allowEmptyValue: true }
|
|
756
1365
|
};
|
|
757
1366
|
}
|
|
758
1367
|
function isJsonMediaType(contentType) {
|
|
@@ -829,6 +1438,91 @@ function parameterReaders(schema, options, param) {
|
|
|
829
1438
|
return [options?.allowEmptyValue === true ? emptyValueReader(param) : void 0, reader].filter((step) => step !== void 0);
|
|
830
1439
|
}
|
|
831
1440
|
//#endregion
|
|
1441
|
+
//#region src/helper/polymorphic-base.ts
|
|
1442
|
+
/** The definition name a `$ref` names and the prefix it is written with. */
|
|
1443
|
+
function refParts(ref) {
|
|
1444
|
+
for (const prefix of DEFINITION_REF_PREFIXES) {
|
|
1445
|
+
if (!ref.startsWith(prefix)) continue;
|
|
1446
|
+
const rest = ref.slice(prefix.length);
|
|
1447
|
+
if (rest === "" || rest.includes("/")) return void 0;
|
|
1448
|
+
return {
|
|
1449
|
+
prefix,
|
|
1450
|
+
name: decodeURIComponent(rest)
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
/** A definition's own shape: the definition without the union that lists its subtypes. */
|
|
1455
|
+
function baseOf(schema) {
|
|
1456
|
+
const { oneOf: _oneOf, anyOf: _anyOf, discriminator: _discriminator, ...base } = schema;
|
|
1457
|
+
return base;
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* Break the cycle of a polymorphic base: a definition whose `oneOf` / `anyOf` lists its
|
|
1461
|
+
* subtypes while each subtype extends it through `allOf: [{ $ref: base }, ...]` (the
|
|
1462
|
+
* OpenAPI discriminator inheritance pattern). Read literally the base is a union of its
|
|
1463
|
+
* subtypes and each subtype contains the base, which loops forever.
|
|
1464
|
+
*
|
|
1465
|
+
* In a subtype's `allOf` the base stands for its own shape only, so that member is
|
|
1466
|
+
* pointed at a new `<Base>Base` definition holding the base without its union. The base
|
|
1467
|
+
* itself stays the union of its subtypes.
|
|
1468
|
+
*
|
|
1469
|
+
* Returns `definitions` unchanged when no definition has that shape.
|
|
1470
|
+
*/
|
|
1471
|
+
function separatePolymorphicBases(definitions) {
|
|
1472
|
+
const result = { ...definitions };
|
|
1473
|
+
let changed = false;
|
|
1474
|
+
for (const [name, schema] of Object.entries(definitions)) {
|
|
1475
|
+
const members = schema.oneOf ?? schema.anyOf;
|
|
1476
|
+
if (members === void 0 || members.length === 0) continue;
|
|
1477
|
+
const extending = members.flatMap((member) => {
|
|
1478
|
+
const parts = member.$ref === void 0 ? void 0 : refParts(member.$ref);
|
|
1479
|
+
return parts === void 0 ? [] : [parts.name];
|
|
1480
|
+
}).filter((subtype) => definitions[subtype]?.allOf?.some((member) => member.$ref !== void 0 && refParts(member.$ref)?.name === name));
|
|
1481
|
+
if (extending.length === 0) continue;
|
|
1482
|
+
changed = true;
|
|
1483
|
+
const baseName = unusedName(`${name}Base`, result);
|
|
1484
|
+
result[baseName] = baseOf(schema);
|
|
1485
|
+
for (const subtype of extending) {
|
|
1486
|
+
const subtypeSchema = result[subtype];
|
|
1487
|
+
if (subtypeSchema?.allOf === void 0) continue;
|
|
1488
|
+
result[subtype] = {
|
|
1489
|
+
...subtypeSchema,
|
|
1490
|
+
allOf: subtypeSchema.allOf.map((member) => {
|
|
1491
|
+
const parts = member.$ref === void 0 ? void 0 : refParts(member.$ref);
|
|
1492
|
+
return parts?.name === name ? withRef(member, `${parts.prefix}${encodeURIComponent(baseName)}`) : member;
|
|
1493
|
+
})
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return changed ? result : definitions;
|
|
1498
|
+
}
|
|
1499
|
+
function withRef(schema, $ref) {
|
|
1500
|
+
return {
|
|
1501
|
+
...schema,
|
|
1502
|
+
$ref
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
function unusedName(name, taken) {
|
|
1506
|
+
if (!Object.hasOwn(taken, name)) return name;
|
|
1507
|
+
let index = 2;
|
|
1508
|
+
while (Object.hasOwn(taken, `${name}${index}`)) index += 1;
|
|
1509
|
+
return `${name}${index}`;
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* A document with {@link separatePolymorphicBases} applied to its `$defs` and
|
|
1513
|
+
* `definitions`; the document itself when neither changes.
|
|
1514
|
+
*/
|
|
1515
|
+
function withPolymorphicBases(schema) {
|
|
1516
|
+
const $defs = schema.$defs === void 0 ? void 0 : separatePolymorphicBases(schema.$defs);
|
|
1517
|
+
const definitions = schema.definitions === void 0 ? void 0 : separatePolymorphicBases(schema.definitions);
|
|
1518
|
+
if ($defs === schema.$defs && definitions === schema.definitions) return schema;
|
|
1519
|
+
return {
|
|
1520
|
+
...schema,
|
|
1521
|
+
...$defs !== void 0 && { $defs },
|
|
1522
|
+
...definitions !== void 0 && { definitions }
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
//#endregion
|
|
832
1526
|
//#region src/helper/ref.ts
|
|
833
1527
|
/**
|
|
834
1528
|
* Resolves a `$ref` for the Zod, Valibot and Effect generators, which name declarations
|
|
@@ -885,27 +1579,6 @@ function refTarget(ref, rootName, isDocument, openapi) {
|
|
|
885
1579
|
return { kind: "any" };
|
|
886
1580
|
}
|
|
887
1581
|
//#endregion
|
|
888
|
-
//#region src/helper/regex.ts
|
|
889
|
-
/**
|
|
890
|
-
* Build a JavaScript regular-expression literal from a JSON Schema `pattern`.
|
|
891
|
-
*
|
|
892
|
-
* Escapes unescaped `/` so the literal stays delimited, and adds the `u` flag
|
|
893
|
-
* when the pattern uses Unicode property escapes (`\p{...}`, `\P{...}`,
|
|
894
|
-
* `\u{...}`), which TypeScript requires for those constructs (TS1530). Returns
|
|
895
|
-
* the literal only (`/.../` or `/.../u`); callers append any validator-specific
|
|
896
|
-
* error argument.
|
|
897
|
-
*
|
|
898
|
-
* @example
|
|
899
|
-
* ```ts
|
|
900
|
-
* regexLiteral('^[a-z]+$') // '/^[a-z]+$/'
|
|
901
|
-
* regexLiteral('^\\p{L}+$') // '/^\\p{L}+$/u'
|
|
902
|
-
* regexLiteral('a/b') // '/a\\/b/'
|
|
903
|
-
* ```
|
|
904
|
-
*/
|
|
905
|
-
function regexLiteral(pattern) {
|
|
906
|
-
return `/${pattern.replaceAll(/(?<!\\)\//gu, "\\/")}/${UNICODE_ESCAPE_PATTERN.test(pattern) ? "u" : ""}`;
|
|
907
|
-
}
|
|
908
|
-
//#endregion
|
|
909
1582
|
//#region src/helper/type.ts
|
|
910
1583
|
/** Whether a node is a 64-bit integer, whose number is a bigint. */
|
|
911
1584
|
function isInt64(schema) {
|
|
@@ -919,6 +1592,11 @@ function isInt64(schema) {
|
|
|
919
1592
|
*/
|
|
920
1593
|
function typeDeclaration(schema, flavour, rootName = DEFAULT_ROOT_NAME) {
|
|
921
1594
|
if (schema === void 0) return "";
|
|
1595
|
+
const base = nonNullType(schema, flavour, rootName);
|
|
1596
|
+
if (!(isNullable(schema) || schema.allOf?.some(isNullTypeMember) === true) || base === "null" || base === "unknown") return base;
|
|
1597
|
+
return `(${base} | null)`;
|
|
1598
|
+
}
|
|
1599
|
+
function nonNullType(schema, flavour, rootName) {
|
|
922
1600
|
const type = (s) => typeDeclaration(s, flavour, rootName);
|
|
923
1601
|
if (schema.$ref) {
|
|
924
1602
|
if (isRootRef(schema.$ref)) return flavour.inferRoot(rootName);
|
|
@@ -994,10 +1672,69 @@ function typeDeclaration(schema, flavour, rootName = DEFAULT_ROOT_NAME) {
|
|
|
994
1672
|
* time, because TypeBox stores metadata on the schema object during
|
|
995
1673
|
* construction and exposes no post-construction `.meta()` method.
|
|
996
1674
|
*/
|
|
997
|
-
function typeboxWrap(typeboxStr, schema) {
|
|
1675
|
+
function typeboxWrap(typeboxStr, schema, definitions) {
|
|
998
1676
|
const withNullable = isNullable(schema) ? `Type.Union([${typeboxStr},Type.Null()])` : typeboxStr;
|
|
999
1677
|
const withReadonly = schema["x-readonly"] === true ? `Type.Readonly(${withNullable})` : withNullable;
|
|
1000
|
-
return hasKeptDefault(schema) ? `Type.Optional(${withReadonly})` : withReadonly;
|
|
1678
|
+
return hasKeptDefault(schema, definitions) ? `Type.Optional(${withReadonly})` : withReadonly;
|
|
1679
|
+
}
|
|
1680
|
+
//#endregion
|
|
1681
|
+
//#region src/helper/unevaluated.ts
|
|
1682
|
+
function definitionOf(ref, definitions) {
|
|
1683
|
+
if (definitions === void 0) return void 0;
|
|
1684
|
+
for (const prefix of DEFINITION_REF_PREFIXES) {
|
|
1685
|
+
if (!ref.startsWith(prefix)) continue;
|
|
1686
|
+
const rest = ref.slice(prefix.length);
|
|
1687
|
+
if (rest === "" || rest.includes("/")) return void 0;
|
|
1688
|
+
const name = decodeURIComponent(rest);
|
|
1689
|
+
return Object.hasOwn(definitions, name) ? definitions[name] : void 0;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
/**
|
|
1693
|
+
* The properties a node evaluates for `unevaluatedProperties` (JSON Schema 2020-12 §11.3):
|
|
1694
|
+
* its own `properties` and `patternProperties`, and those of every `allOf` / `oneOf` /
|
|
1695
|
+
* `anyOf` / `then` / `else` / `dependentSchemas` member, a `$ref` read as the
|
|
1696
|
+
* definition it names. A branch that may not apply counts as evaluated, so a valid value
|
|
1697
|
+
* is never refused.
|
|
1698
|
+
*
|
|
1699
|
+
* `undefined` when every property is evaluated (an `additionalProperties` anywhere) or
|
|
1700
|
+
* when that cannot be told (a `$ref` `definitions` does not hold).
|
|
1701
|
+
*/
|
|
1702
|
+
function evaluatedProperties(schema, definitions, reading = /* @__PURE__ */ new Set()) {
|
|
1703
|
+
if (schema.additionalProperties !== void 0) return void 0;
|
|
1704
|
+
const members = [
|
|
1705
|
+
...schema.allOf ?? [],
|
|
1706
|
+
...schema.oneOf ?? [],
|
|
1707
|
+
...schema.anyOf ?? [],
|
|
1708
|
+
...schema.then === void 0 ? [] : [schema.then],
|
|
1709
|
+
...schema.else === void 0 ? [] : [schema.else],
|
|
1710
|
+
...Object.values(schema.dependentSchemas ?? {})
|
|
1711
|
+
];
|
|
1712
|
+
const names = new Set(Object.keys(schema.properties ?? {}));
|
|
1713
|
+
const patterns = new Set(Object.keys(schema.patternProperties ?? {}));
|
|
1714
|
+
if (schema.$ref !== void 0 && !reading.has(schema.$ref)) {
|
|
1715
|
+
const target = definitionOf(schema.$ref, definitions);
|
|
1716
|
+
if (target === void 0) return void 0;
|
|
1717
|
+
members.push(target);
|
|
1718
|
+
}
|
|
1719
|
+
const next = schema.$ref === void 0 ? reading : /* @__PURE__ */ new Set([...reading, schema.$ref]);
|
|
1720
|
+
for (const member of members) {
|
|
1721
|
+
if (typeof member !== "object" || member === null) continue;
|
|
1722
|
+
const evaluated = evaluatedProperties(member, definitions, next);
|
|
1723
|
+
if (evaluated === void 0) return void 0;
|
|
1724
|
+
for (const name of evaluated.names) names.add(name);
|
|
1725
|
+
for (const pattern of evaluated.patterns) patterns.add(pattern);
|
|
1726
|
+
}
|
|
1727
|
+
return {
|
|
1728
|
+
names: [...names],
|
|
1729
|
+
patterns: [...patterns]
|
|
1730
|
+
};
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* The emitted expression that tells whether `key` is evaluated: a lookup in the names
|
|
1734
|
+
* plus one `RegExp` test per pattern.
|
|
1735
|
+
*/
|
|
1736
|
+
function isEvaluatedKeyExpression(evaluated, key) {
|
|
1737
|
+
return [`${JSON.stringify(evaluated.names)}.includes(${key})`, ...evaluated.patterns.map((pattern) => `new RegExp(${JSON.stringify(pattern)}).test(${key})`)].join("||");
|
|
1001
1738
|
}
|
|
1002
1739
|
//#endregion
|
|
1003
1740
|
//#region src/helper/valibot-wrap.ts
|
|
@@ -1032,7 +1769,7 @@ function valibotWrap(valibotStr, schema, options) {
|
|
|
1032
1769
|
const nullText = options?.nullText === true;
|
|
1033
1770
|
const withNullable = isNullable(schema) || nullText && !isNullMatchedFirst(schema) ? `v.nullable(${valibotStr})` : valibotStr;
|
|
1034
1771
|
const withNullText = nullText ? `v.pipe(v.unknown(),v.transform(${wireNullText("input")}),${withNullable})` : withNullable;
|
|
1035
|
-
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default) : void 0;
|
|
1772
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
1036
1773
|
const withDefault = defaultResult?.keep ? `v.optional(${withNullText},${defaultLiteral(schema, defaultResult.value, options?.stringWire === true)})` : withNullText;
|
|
1037
1774
|
const examples = examplesOf(schema);
|
|
1038
1775
|
const actions = [];
|
|
@@ -1082,7 +1819,7 @@ function zodWrap(zodStr, schema, options) {
|
|
|
1082
1819
|
if (schema.type === "date" && typeof value === "string") return `new Date(${JSON.stringify(value)})`;
|
|
1083
1820
|
return JSON.stringify(value);
|
|
1084
1821
|
};
|
|
1085
|
-
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default) : void 0;
|
|
1822
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
1086
1823
|
const withNullable = isNullable(schema) ? `${zodStr}.nullable()` : zodStr;
|
|
1087
1824
|
const withDefault = defaultResult?.keep ? `${withNullable}.default(${formatLiteral(defaultResult.value)})` : withNullable;
|
|
1088
1825
|
const withReadonly = schema["x-readonly"] === true ? `${withDefault}.readonly()` : withDefault;
|
|
@@ -1112,4 +1849,4 @@ function zodWrap(zodStr, schema, options) {
|
|
|
1112
1849
|
return withMeta;
|
|
1113
1850
|
}
|
|
1114
1851
|
//#endregion
|
|
1115
|
-
export {
|
|
1852
|
+
export { callbackReturn as $, wireNullText as A, emitOptionsOf as B, isPrimitiveUnion as C, stringboolTruthyPattern as D, stringboolPattern as E, jsLiteral as F, schemaNamer as G, planDocument as H, typeboxDefaultOpt as I, hasRootSelfReference as J, discriminatedMembers as K, typeboxMetaOpts as L, formatPattern as M, regexSource as N, unionPrimitiveTypes as O, timePattern as P, arktypeWrap as Q, typeboxRefOpt as R, isNullTextRead as S, ownProperties as T, renderDefinitions as U, missingDefinitionComment as V, rootNameOf as W, itemsSchemaOf as X, isOptionalByDefault as Y, arktypePreprocess as Z, effectPreprocess as _, typeboxWrap as a, foldAllOfMembers as at, integerWirePattern as b, withPolymorphicBases as c, regexLiteral as ct, propertyEmitOptions as d, callbackType as et, withoutParameterSerialization as f, hoistNestedRefs as g, valueListLiteral as h, isEvaluatedKeyExpression as i, constraintPredicate as it, wireNumericKind as j, wireArrayOnce as k, hasParameterSerialization as l, notPredicate as m, valibotWrap as n, hasNotKeyword as nt, typeDeclaration as o, isKeywordOnlyMember as ot, isBigintFormat as p, cyclicDefinitionNames as q, evaluatedProperties as r, constraintMessage as rt, refTarget as s, patternSource as st, zodWrap as t, findCodeExtensionKeysInSchema as tt, parameterReaders as u, effectWrap as v, numberWirePattern as w, isNullMatchedFirst as x, impliedWireType as y, definitionsOf as z };
|