schema-truth 0.7.0 → 0.8.1
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-CRZ8vXZ5.js} +94 -37
- 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-eAY8LqwP.js} +2 -2
- package/dist/{effect-mUknRPv_.js → effect-BkWovUi3.js} +52 -19
- package/dist/effect.d.ts +3 -3
- package/dist/effect.js +1 -1
- package/dist/{helper-Cfvb5UDq.js → helper-CbeedY35.js} +721 -38
- package/dist/{index-C2L8Ty0-.d.ts → index-DYzAccyT.d.ts} +29 -25
- package/dist/{index-DbV3dEmN.d.ts → index-G7UqNQ4w.d.ts} +54 -2
- package/dist/index.d.ts +54 -25
- package/dist/index.js +9 -9
- package/dist/{typebox-BdWJv3ND.js → typebox-wYeJqy7T.js} +33 -14
- 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-DQUEDYKt.js} +64 -23
- package/dist/valibot.d.ts +3 -3
- package/dist/valibot.js +1 -1
- package/dist/{yup-CM2TSFig.js → yup-BMpa9Co1.js} +2 -2
- package/dist/yup.d.ts +2 -2
- package/dist/yup.js +1 -1
- package/dist/{zod-BsXLZfwK.js → zod-Dq9LtqPL.js} +130 -91
- 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.
|
|
@@ -178,16 +555,16 @@ function examplesOf(schema) {
|
|
|
178
555
|
* Whether a node carries a `default` the generators keep, so a property holding it may be
|
|
179
556
|
* left out of the input.
|
|
180
557
|
*/
|
|
181
|
-
function hasKeptDefault(schema) {
|
|
182
|
-
return schema.default !== void 0 && coerceDefault(schema, schema.default).keep;
|
|
558
|
+
function hasKeptDefault(schema, definitions) {
|
|
559
|
+
return schema.default !== void 0 && coerceDefault(schema, schema.default, definitions).keep;
|
|
183
560
|
}
|
|
184
561
|
/**
|
|
185
562
|
* Whether a node's own emitter already makes it optional with a default: a kept `default`,
|
|
186
563
|
* or an `allOf` member that only carries one. An object wrapping such a property again
|
|
187
564
|
* would hide the default behind the outer wrapper.
|
|
188
565
|
*/
|
|
189
|
-
function isOptionalByDefault(schema) {
|
|
190
|
-
return hasKeptDefault(schema) || schema.allOf?.some(isDefaultOnlyMember) === true;
|
|
566
|
+
function isOptionalByDefault(schema, definitions) {
|
|
567
|
+
return hasKeptDefault(schema, definitions) || schema.allOf?.some(isDefaultOnlyMember) === true;
|
|
191
568
|
}
|
|
192
569
|
//#endregion
|
|
193
570
|
//#region src/helper/cyclic.ts
|
|
@@ -356,7 +733,20 @@ function emitOptionsOf(options) {
|
|
|
356
733
|
...options?.style !== void 0 && { style: options.style },
|
|
357
734
|
...options?.explode !== void 0 && { explode: options.explode },
|
|
358
735
|
...options?.allowEmptyValue !== void 0 && { allowEmptyValue: options.allowEmptyValue },
|
|
359
|
-
...options?.contentType !== void 0 && { contentType: options.contentType }
|
|
736
|
+
...options?.contentType !== void 0 && { contentType: options.contentType },
|
|
737
|
+
...options?.properties !== void 0 && { properties: options.properties },
|
|
738
|
+
...options?.definitions !== void 0 && { definitions: options.definitions }
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* `options.definitions` plus the document's own `$defs` / `definitions`. The document
|
|
743
|
+
* wins on a shared name. `undefined` when neither holds a schema.
|
|
744
|
+
*/
|
|
745
|
+
function definitionsOf(options, document) {
|
|
746
|
+
if (Object.keys(document).length === 0 && options?.definitions === void 0) return void 0;
|
|
747
|
+
return {
|
|
748
|
+
...options?.definitions,
|
|
749
|
+
...document
|
|
360
750
|
};
|
|
361
751
|
}
|
|
362
752
|
/**
|
|
@@ -442,9 +832,9 @@ function typeboxMetaOpts(schema, ref) {
|
|
|
442
832
|
* `stringWire` (for `query`/`path` coercion) emits the default in its string-wire
|
|
443
833
|
* form (`'1'` / `'true'`), since the coerced `Type.String(...)` input is a string.
|
|
444
834
|
*/
|
|
445
|
-
function typeboxDefaultOpt(schema, stringWire = false) {
|
|
835
|
+
function typeboxDefaultOpt(schema, stringWire = false, definitions) {
|
|
446
836
|
if (schema.default === void 0) return [];
|
|
447
|
-
const result = coerceDefault(schema, schema.default);
|
|
837
|
+
const result = coerceDefault(schema, schema.default, definitions);
|
|
448
838
|
if (!result.keep) return [];
|
|
449
839
|
const value = stringWire ? String(result.value) : result.value;
|
|
450
840
|
return [`default:${typeof value === "boolean" || typeof value === "number" ? `${value}` : JSON.stringify(value)}`];
|
|
@@ -614,6 +1004,27 @@ function wireNullText(param) {
|
|
|
614
1004
|
function ownProperties(param) {
|
|
615
1005
|
return `(${param})=>(typeof ${param}==="object"&&${param}!==null&&!Array.isArray(${param})?Object.assign(Object.create(null),${param}):${param})`;
|
|
616
1006
|
}
|
|
1007
|
+
/** The regex literal of integer text on the wire. */
|
|
1008
|
+
function integerWirePattern() {
|
|
1009
|
+
return regexSource(INTEGER_WIRE_PATTERN);
|
|
1010
|
+
}
|
|
1011
|
+
/** The regex literal of JSON number text on the wire. */
|
|
1012
|
+
function numberWirePattern() {
|
|
1013
|
+
return regexSource(NUMBER_WIRE_PATTERN);
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Whether wire text for these literals is an integer or a number. An integer rejects
|
|
1017
|
+
* exponents and fractions; a number follows the JSON number grammar.
|
|
1018
|
+
*/
|
|
1019
|
+
function wireNumericKind(schema, values) {
|
|
1020
|
+
const types = schema.type;
|
|
1021
|
+
if (types === "number") return "number";
|
|
1022
|
+
if (types === "integer") return "integer";
|
|
1023
|
+
if (Array.isArray(types) && types.includes("number")) return "number";
|
|
1024
|
+
if (Array.isArray(types) && types.includes("integer")) return "integer";
|
|
1025
|
+
if (values.every((value) => typeof value === "number" && Number.isInteger(value))) return "integer";
|
|
1026
|
+
return "number";
|
|
1027
|
+
}
|
|
617
1028
|
/** The regex literal of every spelling of a boolean on the wire. */
|
|
618
1029
|
function stringboolPattern() {
|
|
619
1030
|
return regexSource(STRINGBOOL_PATTERN);
|
|
@@ -675,7 +1086,7 @@ function effectWrap(effectStr, schema, options) {
|
|
|
675
1086
|
readCodeExtension(schema, "x-pipe", options)
|
|
676
1087
|
].filter((v) => v !== void 0);
|
|
677
1088
|
const withCodeExts = codeChain.length === 0 ? withBrand : `${withBrand}${codeChain.join("")}`;
|
|
678
|
-
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default) : void 0;
|
|
1089
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
679
1090
|
const decodingDefault = options?.stringWire === true ? "Schema.withDecodingDefaultType" : "Schema.withDecodingDefault";
|
|
680
1091
|
const withDefault = defaultResult?.keep ? `${withCodeExts}.pipe(${decodingDefault}(Effect.succeed(${defaultLiteral$1(schema, defaultResult.value)})))` : withCodeExts;
|
|
681
1092
|
const examples = examplesOf(schema);
|
|
@@ -690,7 +1101,124 @@ function effectWrap(effectStr, schema, options) {
|
|
|
690
1101
|
return `${withDefault}.annotate(${serializeJSValue(ann)})`;
|
|
691
1102
|
}
|
|
692
1103
|
//#endregion
|
|
1104
|
+
//#region src/helper/hoist-refs.ts
|
|
1105
|
+
/** Keywords whose values are data, not schemas: a `$ref` string there is not a reference. */
|
|
1106
|
+
const DATA_KEYWORDS = /* @__PURE__ */ new Set([
|
|
1107
|
+
"const",
|
|
1108
|
+
"default",
|
|
1109
|
+
"enum",
|
|
1110
|
+
"example",
|
|
1111
|
+
"examples"
|
|
1112
|
+
]);
|
|
1113
|
+
/** Whether a `$ref` already names a definition directly (`#/$defs/X`). */
|
|
1114
|
+
function isDefinitionRef(ref) {
|
|
1115
|
+
return DEFINITION_REF_PREFIXES.some((prefix) => {
|
|
1116
|
+
if (!ref.startsWith(prefix)) return false;
|
|
1117
|
+
const rest = ref.slice(prefix.length);
|
|
1118
|
+
return rest !== "" && !rest.includes("/");
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
/** A local JSON Pointer `$ref` deeper than a definition: `#/$defs/A/properties/b`. */
|
|
1122
|
+
function isNestedPointer(ref) {
|
|
1123
|
+
return ref.startsWith("#/") && !isDefinitionRef(ref);
|
|
1124
|
+
}
|
|
1125
|
+
function decodeToken(token) {
|
|
1126
|
+
return decodeURIComponent(token).replaceAll("~1", "/").replaceAll("~0", "~");
|
|
1127
|
+
}
|
|
1128
|
+
function resolvePointer(document, ref) {
|
|
1129
|
+
let node = document;
|
|
1130
|
+
for (const token of ref.slice(2).split("/").map(decodeToken)) {
|
|
1131
|
+
if (typeof node !== "object" || node === null || !Object.hasOwn(node, token)) return void 0;
|
|
1132
|
+
node = Reflect.get(node, token);
|
|
1133
|
+
}
|
|
1134
|
+
return node;
|
|
1135
|
+
}
|
|
1136
|
+
function collectNestedPointers(node, found) {
|
|
1137
|
+
if (Array.isArray(node)) {
|
|
1138
|
+
for (const item of node) collectNestedPointers(item, found);
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
if (typeof node !== "object" || node === null) return;
|
|
1142
|
+
for (const [key, value] of Object.entries(node)) if (key === "$ref" && typeof value === "string" && isNestedPointer(value)) found.add(value);
|
|
1143
|
+
else if (!DATA_KEYWORDS.has(key)) collectNestedPointers(value, found);
|
|
1144
|
+
}
|
|
1145
|
+
/** A JSON object read as a schema node: what a pointer reaches or a rewrite returns. */
|
|
1146
|
+
function isSchemaNode(value) {
|
|
1147
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1148
|
+
}
|
|
1149
|
+
function rewriteRefs(node, refs) {
|
|
1150
|
+
if (Array.isArray(node)) return node.map((item) => rewriteRefs(item, refs));
|
|
1151
|
+
if (typeof node !== "object" || node === null) return node;
|
|
1152
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => {
|
|
1153
|
+
if (key === "$ref" && typeof value === "string") return [key, refs.get(value) ?? value];
|
|
1154
|
+
return [key, DATA_KEYWORDS.has(key) ? value : rewriteRefs(value, refs)];
|
|
1155
|
+
}));
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Hoist every schema a `$ref` reaches through a nested JSON Pointer into a definition of
|
|
1159
|
+
* its own, and point the references at it.
|
|
1160
|
+
*
|
|
1161
|
+
* Bundling an external file leaves its `definitions` where the file was referenced, so a
|
|
1162
|
+
* reference inside it reads `#/$defs/Root/properties/root/definitions/Node`. The
|
|
1163
|
+
* generators name and declare top-level definitions only; a recursive schema reached that
|
|
1164
|
+
* way would otherwise be referenced by a name nothing declares. A hoisted definition is
|
|
1165
|
+
* named after the pointer's last token, numbered when the name is taken.
|
|
1166
|
+
*
|
|
1167
|
+
* Returns the document as given when it has no such reference.
|
|
1168
|
+
*/
|
|
1169
|
+
function hoistNestedRefs(schema) {
|
|
1170
|
+
const pointers = /* @__PURE__ */ new Set();
|
|
1171
|
+
collectNestedPointers(schema, pointers);
|
|
1172
|
+
if (pointers.size === 0) return schema;
|
|
1173
|
+
const container = schema.$defs === void 0 && schema.definitions !== void 0 ? "definitions" : "$defs";
|
|
1174
|
+
const taken = /* @__PURE__ */ new Set([...Object.keys(schema.$defs ?? {}), ...Object.keys(schema.definitions ?? {})]);
|
|
1175
|
+
const refs = /* @__PURE__ */ new Map();
|
|
1176
|
+
const hoisted = [];
|
|
1177
|
+
for (const pointer of pointers) {
|
|
1178
|
+
const target = resolvePointer(schema, pointer);
|
|
1179
|
+
if (!isSchemaNode(target)) continue;
|
|
1180
|
+
const base = pointer.slice(2).split("/").map(decodeToken).at(-1) ?? "Definition";
|
|
1181
|
+
let name = base;
|
|
1182
|
+
for (let index = 2; taken.has(name); index += 1) name = `${base}${index}`;
|
|
1183
|
+
taken.add(name);
|
|
1184
|
+
refs.set(pointer, `#/${container}/${encodeURIComponent(name)}`);
|
|
1185
|
+
hoisted.push([name, target]);
|
|
1186
|
+
}
|
|
1187
|
+
const rewritten = rewriteRefs(schema, refs);
|
|
1188
|
+
if (refs.size === 0 || !isSchemaNode(rewritten)) return schema;
|
|
1189
|
+
const added = {};
|
|
1190
|
+
for (const [name, target] of hoisted) {
|
|
1191
|
+
const node = rewriteRefs(target, refs);
|
|
1192
|
+
if (isSchemaNode(node)) added[name] = node;
|
|
1193
|
+
}
|
|
1194
|
+
return {
|
|
1195
|
+
...rewritten,
|
|
1196
|
+
[container]: {
|
|
1197
|
+
...rewritten[container],
|
|
1198
|
+
...added
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
//#endregion
|
|
693
1203
|
//#region src/helper/not.ts
|
|
1204
|
+
/** Whether the schema's values are bigints (`int64`, `uint64`, `bigint`). */
|
|
1205
|
+
function isBigintFormat(schema) {
|
|
1206
|
+
return schema.format === "int64" || schema.format === "uint64" || schema.format === "bigint";
|
|
1207
|
+
}
|
|
1208
|
+
/** A value as a TypeScript literal. Numbers of a bigint schema are `0n`, not `0`. */
|
|
1209
|
+
function literal(value, bigint) {
|
|
1210
|
+
if (bigint && typeof value === "number" && Number.isFinite(value)) return `${value}n`;
|
|
1211
|
+
return JSON.stringify(value);
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* An `enum` as an array literal. A bigint schema's numbers are bigint literals, so
|
|
1215
|
+
* `[0n].includes(val)` type-checks and rejects `0n`; `[0].includes(0n)` is both a
|
|
1216
|
+
* type error and always false.
|
|
1217
|
+
*/
|
|
1218
|
+
function valueListLiteral(values, bigint) {
|
|
1219
|
+
if (!bigint) return JSON.stringify(values);
|
|
1220
|
+
return `[${values.map((value) => literal(value, true)).join(",")}]`;
|
|
1221
|
+
}
|
|
694
1222
|
/** The `not` predicate bodies by `type`, over a value bound to `param`. */
|
|
695
1223
|
function notTypeBody(type, param) {
|
|
696
1224
|
switch (type) {
|
|
@@ -708,11 +1236,12 @@ function notTypeBody(type, param) {
|
|
|
708
1236
|
* The predicate refusing what a `not` schema describes, as the source of an arrow over
|
|
709
1237
|
* `param` (declared as `parameter`, which may carry a type annotation): a `const` is
|
|
710
1238
|
* refused by value, a `type` by its runtime check, a list of types by every check at
|
|
711
|
-
* once. `
|
|
712
|
-
* combinator, which each generator
|
|
1239
|
+
* once. `bigint` spells a numeric `const` as a bigint literal. `undefined` for a `not`
|
|
1240
|
+
* these shared cases do not cover — an `enum` or a combinator, which each generator
|
|
1241
|
+
* spells with its own parser.
|
|
713
1242
|
*/
|
|
714
|
-
function notPredicate(inner, param, parameter = param) {
|
|
715
|
-
if ("const" in inner) return `(${parameter}) => ${param} !== ${
|
|
1243
|
+
function notPredicate(inner, param, parameter = param, bigint = false) {
|
|
1244
|
+
if ("const" in inner) return `(${parameter}) => ${param} !== ${literal(inner.const, bigint)}`;
|
|
716
1245
|
if (typeof inner.type === "string") {
|
|
717
1246
|
const body = notTypeBody(inner.type, param);
|
|
718
1247
|
return body === void 0 ? void 0 : `(${parameter}) => ${body}`;
|
|
@@ -752,7 +1281,33 @@ function withoutParameterSerialization(options) {
|
|
|
752
1281
|
...options?.openapi !== void 0 && { openapi: options.openapi },
|
|
753
1282
|
...options?.readonly !== void 0 && { readonly: options.readonly },
|
|
754
1283
|
...options?.unsafeCodeExtensions !== void 0 && { unsafeCodeExtensions: options.unsafeCodeExtensions },
|
|
755
|
-
...options?.paramIn !== void 0 && !isJson && { paramIn: options.paramIn }
|
|
1284
|
+
...options?.paramIn !== void 0 && !isJson && { paramIn: options.paramIn },
|
|
1285
|
+
...options?.definitions !== void 0 && { definitions: options.definitions },
|
|
1286
|
+
...options?.properties !== void 0 && { properties: options.properties }
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* The emit options of one property, when that property carries its own serialization
|
|
1291
|
+
* (`x-style` / `x-explode` / `x-allowEmptyValue`, or `options.properties`). `undefined`
|
|
1292
|
+
* when it does not, so the caller keeps the object's options. Requires `paramIn`: the
|
|
1293
|
+
* keywords describe wire text.
|
|
1294
|
+
*/
|
|
1295
|
+
function propertyEmitOptions(key, property, options) {
|
|
1296
|
+
if (options?.paramIn === void 0) return void 0;
|
|
1297
|
+
const named = options.properties?.[key];
|
|
1298
|
+
const style = named?.style ?? property["x-style"];
|
|
1299
|
+
const explode = named?.explode ?? property["x-explode"];
|
|
1300
|
+
const allowEmptyValue = named?.allowEmptyValue === true || property["x-allowEmptyValue"] === true;
|
|
1301
|
+
if (style === void 0 && explode === void 0 && !allowEmptyValue) return void 0;
|
|
1302
|
+
return {
|
|
1303
|
+
...options.openapi !== void 0 && { openapi: options.openapi },
|
|
1304
|
+
...options.readonly !== void 0 && { readonly: options.readonly },
|
|
1305
|
+
...options.unsafeCodeExtensions !== void 0 && { unsafeCodeExtensions: options.unsafeCodeExtensions },
|
|
1306
|
+
...options.definitions !== void 0 && { definitions: options.definitions },
|
|
1307
|
+
paramIn: options.paramIn,
|
|
1308
|
+
...style !== void 0 && { style },
|
|
1309
|
+
...explode !== void 0 && { explode },
|
|
1310
|
+
...allowEmptyValue && { allowEmptyValue: true }
|
|
756
1311
|
};
|
|
757
1312
|
}
|
|
758
1313
|
function isJsonMediaType(contentType) {
|
|
@@ -829,6 +1384,91 @@ function parameterReaders(schema, options, param) {
|
|
|
829
1384
|
return [options?.allowEmptyValue === true ? emptyValueReader(param) : void 0, reader].filter((step) => step !== void 0);
|
|
830
1385
|
}
|
|
831
1386
|
//#endregion
|
|
1387
|
+
//#region src/helper/polymorphic-base.ts
|
|
1388
|
+
/** The definition name a `$ref` names and the prefix it is written with. */
|
|
1389
|
+
function refParts(ref) {
|
|
1390
|
+
for (const prefix of DEFINITION_REF_PREFIXES) {
|
|
1391
|
+
if (!ref.startsWith(prefix)) continue;
|
|
1392
|
+
const rest = ref.slice(prefix.length);
|
|
1393
|
+
if (rest === "" || rest.includes("/")) return void 0;
|
|
1394
|
+
return {
|
|
1395
|
+
prefix,
|
|
1396
|
+
name: decodeURIComponent(rest)
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
/** A definition's own shape: the definition without the union that lists its subtypes. */
|
|
1401
|
+
function baseOf(schema) {
|
|
1402
|
+
const { oneOf: _oneOf, anyOf: _anyOf, discriminator: _discriminator, ...base } = schema;
|
|
1403
|
+
return base;
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* Break the cycle of a polymorphic base: a definition whose `oneOf` / `anyOf` lists its
|
|
1407
|
+
* subtypes while each subtype extends it through `allOf: [{ $ref: base }, ...]` (the
|
|
1408
|
+
* OpenAPI discriminator inheritance pattern). Read literally the base is a union of its
|
|
1409
|
+
* subtypes and each subtype contains the base, which loops forever.
|
|
1410
|
+
*
|
|
1411
|
+
* In a subtype's `allOf` the base stands for its own shape only, so that member is
|
|
1412
|
+
* pointed at a new `<Base>Base` definition holding the base without its union. The base
|
|
1413
|
+
* itself stays the union of its subtypes.
|
|
1414
|
+
*
|
|
1415
|
+
* Returns `definitions` unchanged when no definition has that shape.
|
|
1416
|
+
*/
|
|
1417
|
+
function separatePolymorphicBases(definitions) {
|
|
1418
|
+
const result = { ...definitions };
|
|
1419
|
+
let changed = false;
|
|
1420
|
+
for (const [name, schema] of Object.entries(definitions)) {
|
|
1421
|
+
const members = schema.oneOf ?? schema.anyOf;
|
|
1422
|
+
if (members === void 0 || members.length === 0) continue;
|
|
1423
|
+
const extending = members.flatMap((member) => {
|
|
1424
|
+
const parts = member.$ref === void 0 ? void 0 : refParts(member.$ref);
|
|
1425
|
+
return parts === void 0 ? [] : [parts.name];
|
|
1426
|
+
}).filter((subtype) => definitions[subtype]?.allOf?.some((member) => member.$ref !== void 0 && refParts(member.$ref)?.name === name));
|
|
1427
|
+
if (extending.length === 0) continue;
|
|
1428
|
+
changed = true;
|
|
1429
|
+
const baseName = unusedName(`${name}Base`, result);
|
|
1430
|
+
result[baseName] = baseOf(schema);
|
|
1431
|
+
for (const subtype of extending) {
|
|
1432
|
+
const subtypeSchema = result[subtype];
|
|
1433
|
+
if (subtypeSchema?.allOf === void 0) continue;
|
|
1434
|
+
result[subtype] = {
|
|
1435
|
+
...subtypeSchema,
|
|
1436
|
+
allOf: subtypeSchema.allOf.map((member) => {
|
|
1437
|
+
const parts = member.$ref === void 0 ? void 0 : refParts(member.$ref);
|
|
1438
|
+
return parts?.name === name ? withRef(member, `${parts.prefix}${encodeURIComponent(baseName)}`) : member;
|
|
1439
|
+
})
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
return changed ? result : definitions;
|
|
1444
|
+
}
|
|
1445
|
+
function withRef(schema, $ref) {
|
|
1446
|
+
return {
|
|
1447
|
+
...schema,
|
|
1448
|
+
$ref
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
function unusedName(name, taken) {
|
|
1452
|
+
if (!Object.hasOwn(taken, name)) return name;
|
|
1453
|
+
let index = 2;
|
|
1454
|
+
while (Object.hasOwn(taken, `${name}${index}`)) index += 1;
|
|
1455
|
+
return `${name}${index}`;
|
|
1456
|
+
}
|
|
1457
|
+
/**
|
|
1458
|
+
* A document with {@link separatePolymorphicBases} applied to its `$defs` and
|
|
1459
|
+
* `definitions`; the document itself when neither changes.
|
|
1460
|
+
*/
|
|
1461
|
+
function withPolymorphicBases(schema) {
|
|
1462
|
+
const $defs = schema.$defs === void 0 ? void 0 : separatePolymorphicBases(schema.$defs);
|
|
1463
|
+
const definitions = schema.definitions === void 0 ? void 0 : separatePolymorphicBases(schema.definitions);
|
|
1464
|
+
if ($defs === schema.$defs && definitions === schema.definitions) return schema;
|
|
1465
|
+
return {
|
|
1466
|
+
...schema,
|
|
1467
|
+
...$defs !== void 0 && { $defs },
|
|
1468
|
+
...definitions !== void 0 && { definitions }
|
|
1469
|
+
};
|
|
1470
|
+
}
|
|
1471
|
+
//#endregion
|
|
832
1472
|
//#region src/helper/ref.ts
|
|
833
1473
|
/**
|
|
834
1474
|
* Resolves a `$ref` for the Zod, Valibot and Effect generators, which name declarations
|
|
@@ -885,27 +1525,6 @@ function refTarget(ref, rootName, isDocument, openapi) {
|
|
|
885
1525
|
return { kind: "any" };
|
|
886
1526
|
}
|
|
887
1527
|
//#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
1528
|
//#region src/helper/type.ts
|
|
910
1529
|
/** Whether a node is a 64-bit integer, whose number is a bigint. */
|
|
911
1530
|
function isInt64(schema) {
|
|
@@ -919,6 +1538,11 @@ function isInt64(schema) {
|
|
|
919
1538
|
*/
|
|
920
1539
|
function typeDeclaration(schema, flavour, rootName = DEFAULT_ROOT_NAME) {
|
|
921
1540
|
if (schema === void 0) return "";
|
|
1541
|
+
const base = nonNullType(schema, flavour, rootName);
|
|
1542
|
+
if (!(isNullable(schema) || schema.allOf?.some(isNullTypeMember) === true) || base === "null" || base === "unknown") return base;
|
|
1543
|
+
return `(${base} | null)`;
|
|
1544
|
+
}
|
|
1545
|
+
function nonNullType(schema, flavour, rootName) {
|
|
922
1546
|
const type = (s) => typeDeclaration(s, flavour, rootName);
|
|
923
1547
|
if (schema.$ref) {
|
|
924
1548
|
if (isRootRef(schema.$ref)) return flavour.inferRoot(rootName);
|
|
@@ -1000,6 +1624,65 @@ function typeboxWrap(typeboxStr, schema) {
|
|
|
1000
1624
|
return hasKeptDefault(schema) ? `Type.Optional(${withReadonly})` : withReadonly;
|
|
1001
1625
|
}
|
|
1002
1626
|
//#endregion
|
|
1627
|
+
//#region src/helper/unevaluated.ts
|
|
1628
|
+
function definitionOf(ref, definitions) {
|
|
1629
|
+
if (definitions === void 0) return void 0;
|
|
1630
|
+
for (const prefix of DEFINITION_REF_PREFIXES) {
|
|
1631
|
+
if (!ref.startsWith(prefix)) continue;
|
|
1632
|
+
const rest = ref.slice(prefix.length);
|
|
1633
|
+
if (rest === "" || rest.includes("/")) return void 0;
|
|
1634
|
+
const name = decodeURIComponent(rest);
|
|
1635
|
+
return Object.hasOwn(definitions, name) ? definitions[name] : void 0;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
/**
|
|
1639
|
+
* The properties a node evaluates for `unevaluatedProperties` (JSON Schema 2020-12 §11.3):
|
|
1640
|
+
* its own `properties` and `patternProperties`, and those of every `allOf` / `oneOf` /
|
|
1641
|
+
* `anyOf` / `then` / `else` / `dependentSchemas` member, a `$ref` read as the
|
|
1642
|
+
* definition it names. A branch that may not apply counts as evaluated, so a valid value
|
|
1643
|
+
* is never refused.
|
|
1644
|
+
*
|
|
1645
|
+
* `undefined` when every property is evaluated (an `additionalProperties` anywhere) or
|
|
1646
|
+
* when that cannot be told (a `$ref` `definitions` does not hold).
|
|
1647
|
+
*/
|
|
1648
|
+
function evaluatedProperties(schema, definitions, reading = /* @__PURE__ */ new Set()) {
|
|
1649
|
+
if (schema.additionalProperties !== void 0) return void 0;
|
|
1650
|
+
const members = [
|
|
1651
|
+
...schema.allOf ?? [],
|
|
1652
|
+
...schema.oneOf ?? [],
|
|
1653
|
+
...schema.anyOf ?? [],
|
|
1654
|
+
...schema.then === void 0 ? [] : [schema.then],
|
|
1655
|
+
...schema.else === void 0 ? [] : [schema.else],
|
|
1656
|
+
...Object.values(schema.dependentSchemas ?? {})
|
|
1657
|
+
];
|
|
1658
|
+
const names = new Set(Object.keys(schema.properties ?? {}));
|
|
1659
|
+
const patterns = new Set(Object.keys(schema.patternProperties ?? {}));
|
|
1660
|
+
if (schema.$ref !== void 0 && !reading.has(schema.$ref)) {
|
|
1661
|
+
const target = definitionOf(schema.$ref, definitions);
|
|
1662
|
+
if (target === void 0) return void 0;
|
|
1663
|
+
members.push(target);
|
|
1664
|
+
}
|
|
1665
|
+
const next = schema.$ref === void 0 ? reading : /* @__PURE__ */ new Set([...reading, schema.$ref]);
|
|
1666
|
+
for (const member of members) {
|
|
1667
|
+
if (typeof member !== "object" || member === null) continue;
|
|
1668
|
+
const evaluated = evaluatedProperties(member, definitions, next);
|
|
1669
|
+
if (evaluated === void 0) return void 0;
|
|
1670
|
+
for (const name of evaluated.names) names.add(name);
|
|
1671
|
+
for (const pattern of evaluated.patterns) patterns.add(pattern);
|
|
1672
|
+
}
|
|
1673
|
+
return {
|
|
1674
|
+
names: [...names],
|
|
1675
|
+
patterns: [...patterns]
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* The emitted expression that tells whether `key` is evaluated: a lookup in the names
|
|
1680
|
+
* plus one `RegExp` test per pattern.
|
|
1681
|
+
*/
|
|
1682
|
+
function isEvaluatedKeyExpression(evaluated, key) {
|
|
1683
|
+
return [`${JSON.stringify(evaluated.names)}.includes(${key})`, ...evaluated.patterns.map((pattern) => `new RegExp(${JSON.stringify(pattern)}).test(${key})`)].join("||");
|
|
1684
|
+
}
|
|
1685
|
+
//#endregion
|
|
1003
1686
|
//#region src/helper/valibot-wrap.ts
|
|
1004
1687
|
/** A default as the Valibot literal of the schema's input: a bigint for a 64-bit integer. */
|
|
1005
1688
|
function defaultLiteral(schema, value, stringWire) {
|
|
@@ -1032,7 +1715,7 @@ function valibotWrap(valibotStr, schema, options) {
|
|
|
1032
1715
|
const nullText = options?.nullText === true;
|
|
1033
1716
|
const withNullable = isNullable(schema) || nullText && !isNullMatchedFirst(schema) ? `v.nullable(${valibotStr})` : valibotStr;
|
|
1034
1717
|
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;
|
|
1718
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
1036
1719
|
const withDefault = defaultResult?.keep ? `v.optional(${withNullText},${defaultLiteral(schema, defaultResult.value, options?.stringWire === true)})` : withNullText;
|
|
1037
1720
|
const examples = examplesOf(schema);
|
|
1038
1721
|
const actions = [];
|
|
@@ -1082,7 +1765,7 @@ function zodWrap(zodStr, schema, options) {
|
|
|
1082
1765
|
if (schema.type === "date" && typeof value === "string") return `new Date(${JSON.stringify(value)})`;
|
|
1083
1766
|
return JSON.stringify(value);
|
|
1084
1767
|
};
|
|
1085
|
-
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default) : void 0;
|
|
1768
|
+
const defaultResult = schema.default !== void 0 ? coerceDefault(schema, schema.default, options?.definitions) : void 0;
|
|
1086
1769
|
const withNullable = isNullable(schema) ? `${zodStr}.nullable()` : zodStr;
|
|
1087
1770
|
const withDefault = defaultResult?.keep ? `${withNullable}.default(${formatLiteral(defaultResult.value)})` : withNullable;
|
|
1088
1771
|
const withReadonly = schema["x-readonly"] === true ? `${withDefault}.readonly()` : withDefault;
|
|
@@ -1112,4 +1795,4 @@ function zodWrap(zodStr, schema, options) {
|
|
|
1112
1795
|
return withMeta;
|
|
1113
1796
|
}
|
|
1114
1797
|
//#endregion
|
|
1115
|
-
export {
|
|
1798
|
+
export { hasNotKeyword 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, isOptionalByDefault as J, cyclicDefinitionNames as K, typeboxMetaOpts as L, formatPattern as M, regexSource as N, unionPrimitiveTypes as O, timePattern as P, findCodeExtensionKeysInSchema as Q, typeboxRefOpt as R, isNullTextRead as S, ownProperties as T, renderDefinitions as U, missingDefinitionComment as V, rootNameOf as W, arktypePreprocess as X, itemsSchemaOf as Y, arktypeWrap as Z, effectPreprocess as _, typeboxWrap as a, regexLiteral as at, integerWirePattern as b, withPolymorphicBases as c, propertyEmitOptions as d, constraintMessage as et, withoutParameterSerialization as f, hoistNestedRefs as g, valueListLiteral as h, isEvaluatedKeyExpression as i, patternSource as it, wireNumericKind as j, wireArrayOnce as k, hasParameterSerialization as l, notPredicate as m, valibotWrap as n, foldAllOfMembers as nt, typeDeclaration as o, isBigintFormat as p, hasRootSelfReference as q, evaluatedProperties as r, isKeywordOnlyMember as rt, refTarget as s, zodWrap as t, constraintPredicate as tt, parameterReaders as u, effectWrap as v, numberWirePattern as w, isNullMatchedFirst as x, impliedWireType as y, definitionsOf as z };
|