schema-truth 0.0.0-stage → 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/README.md +143 -2
- package/dist/ajv-DS9gtNZC.js +105 -0
- package/dist/ajv.d.ts +24 -0
- package/dist/ajv.js +1 -0
- package/dist/arktype-CRZ8vXZ5.js +803 -0
- package/dist/arktype.d.ts +10 -0
- package/dist/arktype.js +1 -0
- package/dist/cli/ajv.d.ts +1 -0
- package/dist/cli/ajv.js +15 -0
- package/dist/cli/arktype.d.ts +1 -0
- package/dist/cli/arktype.js +15 -0
- package/dist/cli/effect.d.ts +1 -0
- package/dist/cli/effect.js +15 -0
- package/dist/cli/typebox.d.ts +1 -0
- package/dist/cli/typebox.js +15 -0
- package/dist/cli/valibot.d.ts +1 -0
- package/dist/cli/valibot.js +15 -0
- package/dist/cli/yup.d.ts +1 -0
- package/dist/cli/yup.js +15 -0
- package/dist/cli/zod.d.ts +1 -0
- package/dist/cli/zod.js +15 -0
- package/dist/cli-eAY8LqwP.js +124 -0
- package/dist/effect-BkWovUi3.js +678 -0
- package/dist/effect.d.ts +12 -0
- package/dist/effect.js +1 -0
- package/dist/helper-CbeedY35.js +1798 -0
- package/dist/index-DYzAccyT.d.ts +69 -0
- package/dist/index-G7UqNQ4w.d.ts +603 -0
- package/dist/index.d.ts +332 -0
- package/dist/index.js +10 -0
- package/dist/parser-CyJ2auea.js +36 -0
- package/dist/typebox-wYeJqy7T.js +563 -0
- package/dist/typebox.d.ts +21 -0
- package/dist/typebox.js +1 -0
- package/dist/utils-Bky98UmP.js +1146 -0
- package/dist/valibot-DQUEDYKt.js +705 -0
- package/dist/valibot.d.ts +9 -0
- package/dist/valibot.js +1 -0
- package/dist/yup-BMpa9Co1.js +92 -0
- package/dist/yup.d.ts +17 -0
- package/dist/yup.js +1 -0
- package/dist/zod-Dq9LtqPL.js +816 -0
- package/dist/zod.d.ts +9 -0
- package/dist/zod.js +1 -0
- package/package.json +112 -4
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
import { $ as DEFINITION_REF_PREFIXES, C as hasNumericKeyword, D as isComposite, H as isStringWireLocation, M as isNullable, O as isDeepLocalPointer, R as isRootRef, S as hasFixedLength, V as isShapelessMember, X as DEFAULT_ROOT_NAME, c as normalizeTypes, dt as NOT_KEYWORD_UNSUPPORTED_MARKER, j as isNullTypeMember, k as isDefaultOnlyMember, l as resolveOpenAPIRef, s as makeSafeKey, w as hasStringKeyword, y as hasCombinator } from "./utils-Bky98UmP.js";
|
|
2
|
+
import { $ as hasNotKeyword, D as stringboolTruthyPattern, E as stringboolPattern, G as schemaNamer, H as planDocument, I as typeboxDefaultOpt, J as isOptionalByDefault, K as cyclicDefinitionNames, L as typeboxMetaOpts, R as typeboxRefOpt, U as renderDefinitions, V as missingDefinitionComment, W as rootNameOf, Y as itemsSchemaOf, a as typeboxWrap, b as integerWirePattern, c as withPolymorphicBases, g as hoistNestedRefs, it as patternSource, nt as foldAllOfMembers, q as hasRootSelfReference, rt as isKeywordOnlyMember, w as numberWirePattern } from "./helper-CbeedY35.js";
|
|
3
|
+
//#region src/generator/typebox/enum.ts
|
|
4
|
+
function lit(v) {
|
|
5
|
+
if (v === null) return "null";
|
|
6
|
+
if (typeof v === "string") return JSON.stringify(v);
|
|
7
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
8
|
+
return JSON.stringify(v) ?? "null";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* One enum member as a TypeBox type. TypeBox v1's `TLiteralValue` excludes `null`
|
|
12
|
+
* (string | number | boolean | bigint only), so a `null` member is `Type.Null()`, not
|
|
13
|
+
* `Type.Literal(null)`.
|
|
14
|
+
*/
|
|
15
|
+
function member(v) {
|
|
16
|
+
return v === null ? "Type.Null()" : `Type.Literal(${lit(v)})`;
|
|
17
|
+
}
|
|
18
|
+
function _enum(schema, ref) {
|
|
19
|
+
const errorMessage = schema["x-enum-message"] ?? schema["x-error-message"];
|
|
20
|
+
const metaOpts = [
|
|
21
|
+
...typeboxRefOpt(ref),
|
|
22
|
+
...typeboxMetaOpts(schema),
|
|
23
|
+
...typeboxDefaultOpt(schema)
|
|
24
|
+
];
|
|
25
|
+
const optsParts = [errorMessage ? `errorMessage:${JSON.stringify(errorMessage)}` : void 0, ...metaOpts].filter((v) => v !== void 0);
|
|
26
|
+
const optsTrailer = optsParts.length > 0 ? `,{${optsParts.join(",")}}` : "";
|
|
27
|
+
if (!schema.enum || schema.enum.length === 0) return optsParts.length > 0 ? `Type.Any({${optsParts.join(",")}})` : "Type.Any()";
|
|
28
|
+
if (schema.enum.some(isComposite)) return optsParts.length > 0 ? `Type.Any({${optsParts.join(",")}})` : "Type.Any()";
|
|
29
|
+
if (schema.enum.length === 1) return schema.enum[0] === null ? `Type.Null(${optsParts.length > 0 ? `{${optsParts.join(",")}}` : ""})` : `Type.Literal(${lit(schema.enum[0])}${optsTrailer})`;
|
|
30
|
+
return `Type.Union([${schema.enum.map(member).join(",")}]${optsTrailer})`;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/generator/typebox/integer.ts
|
|
34
|
+
function integer(schema, ref) {
|
|
35
|
+
const errorMessage = schema["x-error-message"];
|
|
36
|
+
const requiredMessage = schema["x-required-message"];
|
|
37
|
+
const minMessage = schema["x-minimum-message"];
|
|
38
|
+
const maxMessage = schema["x-maximum-message"];
|
|
39
|
+
const exclusiveMinimumMessage = schema["x-exclusiveMinimum-message"];
|
|
40
|
+
const exclusiveMaximumMessage = schema["x-exclusiveMaximum-message"];
|
|
41
|
+
const multipleOfMessage = schema["x-multipleOf-message"];
|
|
42
|
+
const perKeywordEntries = [];
|
|
43
|
+
if (requiredMessage) perKeywordEntries.push(`required:${JSON.stringify(requiredMessage)}`);
|
|
44
|
+
if (minMessage) perKeywordEntries.push(`minimum:${JSON.stringify(minMessage)}`);
|
|
45
|
+
if (maxMessage) perKeywordEntries.push(`maximum:${JSON.stringify(maxMessage)}`);
|
|
46
|
+
if (exclusiveMinimumMessage) perKeywordEntries.push(`exclusiveMinimum:${JSON.stringify(exclusiveMinimumMessage)}`);
|
|
47
|
+
if (exclusiveMaximumMessage) perKeywordEntries.push(`exclusiveMaximum:${JSON.stringify(exclusiveMaximumMessage)}`);
|
|
48
|
+
if (multipleOfMessage) perKeywordEntries.push(`multipleOf:${JSON.stringify(multipleOfMessage)}`);
|
|
49
|
+
const errorMessageField = perKeywordEntries.length === 0 ? errorMessage ? `errorMessage:${JSON.stringify(errorMessage)}` : void 0 : `errorMessage:{${perKeywordEntries.join(",")}${errorMessage ? `,_:${JSON.stringify(errorMessage)}` : ""}}`;
|
|
50
|
+
const metaOpts = [...typeboxMetaOpts(schema), ...typeboxDefaultOpt(schema)];
|
|
51
|
+
if (schema.format === "bigint") {
|
|
52
|
+
const opts = [
|
|
53
|
+
...typeboxRefOpt(ref),
|
|
54
|
+
schema.minimum !== void 0 ? `minimum:BigInt(${schema.minimum})` : void 0,
|
|
55
|
+
schema.maximum !== void 0 ? `maximum:BigInt(${schema.maximum})` : void 0,
|
|
56
|
+
errorMessageField,
|
|
57
|
+
...metaOpts
|
|
58
|
+
].filter((v) => v !== void 0);
|
|
59
|
+
if (opts.length > 0) return `Type.BigInt({${opts.join(",")}})`;
|
|
60
|
+
return "Type.BigInt()";
|
|
61
|
+
}
|
|
62
|
+
const opts = [
|
|
63
|
+
...typeboxRefOpt(ref),
|
|
64
|
+
schema.minimum !== void 0 ? `minimum:${schema.minimum}` : void 0,
|
|
65
|
+
typeof schema.exclusiveMinimum === "number" ? `exclusiveMinimum:${schema.exclusiveMinimum}` : void 0,
|
|
66
|
+
schema.maximum !== void 0 ? `maximum:${schema.maximum}` : void 0,
|
|
67
|
+
typeof schema.exclusiveMaximum === "number" ? `exclusiveMaximum:${schema.exclusiveMaximum}` : void 0,
|
|
68
|
+
schema.multipleOf !== void 0 ? `multipleOf:${schema.multipleOf}` : void 0,
|
|
69
|
+
errorMessageField,
|
|
70
|
+
...metaOpts
|
|
71
|
+
].filter((v) => v !== void 0);
|
|
72
|
+
if (opts.length > 0) return `Type.Integer({${opts.join(",")}})`;
|
|
73
|
+
return "Type.Integer()";
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/generator/typebox/number.ts
|
|
77
|
+
function number(schema, ref) {
|
|
78
|
+
const errorMessage = schema["x-error-message"];
|
|
79
|
+
const requiredMessage = schema["x-required-message"];
|
|
80
|
+
const minMessage = schema["x-minimum-message"];
|
|
81
|
+
const maxMessage = schema["x-maximum-message"];
|
|
82
|
+
const exclusiveMinimumMessage = schema["x-exclusiveMinimum-message"];
|
|
83
|
+
const exclusiveMaximumMessage = schema["x-exclusiveMaximum-message"];
|
|
84
|
+
const multipleOfMessage = schema["x-multipleOf-message"];
|
|
85
|
+
const perKeywordEntries = [];
|
|
86
|
+
if (requiredMessage) perKeywordEntries.push(`required:${JSON.stringify(requiredMessage)}`);
|
|
87
|
+
if (minMessage) perKeywordEntries.push(`minimum:${JSON.stringify(minMessage)}`);
|
|
88
|
+
if (maxMessage) perKeywordEntries.push(`maximum:${JSON.stringify(maxMessage)}`);
|
|
89
|
+
if (exclusiveMinimumMessage) perKeywordEntries.push(`exclusiveMinimum:${JSON.stringify(exclusiveMinimumMessage)}`);
|
|
90
|
+
if (exclusiveMaximumMessage) perKeywordEntries.push(`exclusiveMaximum:${JSON.stringify(exclusiveMaximumMessage)}`);
|
|
91
|
+
if (multipleOfMessage) perKeywordEntries.push(`multipleOf:${JSON.stringify(multipleOfMessage)}`);
|
|
92
|
+
const errorMessageField = perKeywordEntries.length === 0 ? errorMessage ? `errorMessage:${JSON.stringify(errorMessage)}` : void 0 : `errorMessage:{${perKeywordEntries.join(",")}${errorMessage ? `,_:${JSON.stringify(errorMessage)}` : ""}}`;
|
|
93
|
+
const opts = [
|
|
94
|
+
...typeboxRefOpt(ref),
|
|
95
|
+
schema.minimum !== void 0 ? `minimum:${schema.minimum}` : void 0,
|
|
96
|
+
typeof schema.exclusiveMinimum === "number" ? `exclusiveMinimum:${schema.exclusiveMinimum}` : void 0,
|
|
97
|
+
schema.maximum !== void 0 ? `maximum:${schema.maximum}` : void 0,
|
|
98
|
+
typeof schema.exclusiveMaximum === "number" ? `exclusiveMaximum:${schema.exclusiveMaximum}` : void 0,
|
|
99
|
+
schema.multipleOf !== void 0 ? `multipleOf:${schema.multipleOf}` : void 0,
|
|
100
|
+
errorMessageField,
|
|
101
|
+
...typeboxMetaOpts(schema),
|
|
102
|
+
...typeboxDefaultOpt(schema)
|
|
103
|
+
].filter((v) => v !== void 0);
|
|
104
|
+
if (opts.length > 0) return `Type.Number({${opts.join(",")}})`;
|
|
105
|
+
return "Type.Number()";
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/generator/typebox/object.ts
|
|
109
|
+
/**
|
|
110
|
+
* Generate a TypeBox object schema for a JSON Schema object node.
|
|
111
|
+
*
|
|
112
|
+
* Combinators (oneOf/anyOf/allOf/not) delegate to the main `typebox` entry.
|
|
113
|
+
* JSON Schema 2020-12 keywords (`minProperties`, `maxProperties`,
|
|
114
|
+
* `propertyNames`, `patternProperties`, `dependentRequired`) are emitted as
|
|
115
|
+
* options on the `Type.Object(payload, opts)` constructor — TypeBox forwards
|
|
116
|
+
* these to AJV at runtime.
|
|
117
|
+
*
|
|
118
|
+
* Note: TypeBox has no native runtime refinement API, so all advanced
|
|
119
|
+
* keywords are passed through as JSON Schema options. Custom error messages
|
|
120
|
+
* (via `x-*-message` extensions) are not emitted because TypeBox / AJV require
|
|
121
|
+
* the optional `ajv-errors` plugin for that — we keep generation portable.
|
|
122
|
+
*/
|
|
123
|
+
function object(schema, rootName, isTypebox, options) {
|
|
124
|
+
const childOptions = nestedTypeboxOptions(options);
|
|
125
|
+
if (hasCombinator(schema)) return typebox(schema, rootName, isTypebox, options);
|
|
126
|
+
if (typeof schema.additionalProperties === "object") {
|
|
127
|
+
const value = typebox(schema.additionalProperties, rootName, isTypebox, childOptions);
|
|
128
|
+
const recordOpts = [
|
|
129
|
+
...typeboxRefOpt(options?.ref),
|
|
130
|
+
...buildAdvancedOpts(schema, rootName, isTypebox, childOptions),
|
|
131
|
+
...typeboxMetaOpts(schema),
|
|
132
|
+
...typeboxDefaultOpt(schema)
|
|
133
|
+
].filter((v) => v !== void 0);
|
|
134
|
+
return recordOpts.length === 0 ? `Type.Record(Type.String(),${value})` : `Type.Record(Type.String(),${value},{${recordOpts.join(",")}})`;
|
|
135
|
+
}
|
|
136
|
+
if (!schema.properties) {
|
|
137
|
+
if (schema.additionalProperties === true) {
|
|
138
|
+
const anyOpts = [
|
|
139
|
+
...typeboxRefOpt(options?.ref),
|
|
140
|
+
...typeboxMetaOpts(schema),
|
|
141
|
+
...typeboxDefaultOpt(schema)
|
|
142
|
+
];
|
|
143
|
+
return anyOpts.length === 0 ? "Type.Any()" : `Type.Any({${anyOpts.join(",")}})`;
|
|
144
|
+
}
|
|
145
|
+
const emptyOpts = [
|
|
146
|
+
...typeboxRefOpt(options?.ref),
|
|
147
|
+
...buildAdvancedOpts(schema, rootName, isTypebox, childOptions),
|
|
148
|
+
...typeboxMetaOpts(schema),
|
|
149
|
+
...typeboxDefaultOpt(schema)
|
|
150
|
+
].filter((v) => v !== void 0);
|
|
151
|
+
return emptyOpts.length === 0 ? "Type.Object({})" : `Type.Object({},{${emptyOpts.join(",")}})`;
|
|
152
|
+
}
|
|
153
|
+
const required = new Set(schema.required);
|
|
154
|
+
const props = Object.entries(schema.properties).map(([key, propSchema]) => {
|
|
155
|
+
const parsed = typebox(propSchema, rootName, isTypebox, childOptions);
|
|
156
|
+
if (!parsed) return null;
|
|
157
|
+
const safeKey = makeSafeKey(key);
|
|
158
|
+
return required.has(key) || isOptionalByDefault(propSchema) ? `${safeKey}:${parsed}` : `${safeKey}:Type.Optional(${parsed})`;
|
|
159
|
+
}).filter((p) => p !== null);
|
|
160
|
+
const objectErrorMessageEntries = [];
|
|
161
|
+
const objectErrorMessage = schema["x-error-message"];
|
|
162
|
+
if (objectErrorMessage) objectErrorMessageEntries.push(`type:${JSON.stringify(objectErrorMessage)}`);
|
|
163
|
+
const objectMinPropertiesMessage = schema["x-minProperties-message"];
|
|
164
|
+
if (objectMinPropertiesMessage) objectErrorMessageEntries.push(`minProperties:${JSON.stringify(objectMinPropertiesMessage)}`);
|
|
165
|
+
const objectMaxPropertiesMessage = schema["x-maxProperties-message"];
|
|
166
|
+
if (objectMaxPropertiesMessage) objectErrorMessageEntries.push(`maxProperties:${JSON.stringify(objectMaxPropertiesMessage)}`);
|
|
167
|
+
const objectAdditionalPropertiesMessage = schema["x-additionalProperties-message"];
|
|
168
|
+
if (objectAdditionalPropertiesMessage) objectErrorMessageEntries.push(`additionalProperties:${JSON.stringify(objectAdditionalPropertiesMessage)}`);
|
|
169
|
+
const objectPropertyNamesMessage = schema["x-propertyNames-message"];
|
|
170
|
+
if (objectPropertyNamesMessage) objectErrorMessageEntries.push(`propertyNames:${JSON.stringify(objectPropertyNamesMessage)}`);
|
|
171
|
+
const objectPatternPropertiesMessage = schema["x-patternProperties-message"];
|
|
172
|
+
if (objectPatternPropertiesMessage) objectErrorMessageEntries.push(`patternProperties:${JSON.stringify(objectPatternPropertiesMessage)}`);
|
|
173
|
+
const objectDependentRequiredMessage = schema["x-dependentRequired-message"];
|
|
174
|
+
if (objectDependentRequiredMessage) objectErrorMessageEntries.push(`dependentRequired:${JSON.stringify(objectDependentRequiredMessage)}`);
|
|
175
|
+
const objectDependentSchemasMessage = schema["x-dependentSchemas-message"];
|
|
176
|
+
if (objectDependentSchemasMessage) objectErrorMessageEntries.push(`dependentSchemas:${JSON.stringify(objectDependentSchemasMessage)}`);
|
|
177
|
+
const objectRequiredMessage = schema["x-required-message"];
|
|
178
|
+
if (objectRequiredMessage) objectErrorMessageEntries.push(`required:${JSON.stringify(objectRequiredMessage)}`);
|
|
179
|
+
const objectPropertiesMessage = schema["x-properties-message"];
|
|
180
|
+
if (objectPropertiesMessage) objectErrorMessageEntries.push(`properties:${JSON.stringify(objectPropertiesMessage)}`);
|
|
181
|
+
const objectIfMessage = schema["x-if-message"];
|
|
182
|
+
const objectThenMessage = schema["x-then-message"] ?? objectIfMessage;
|
|
183
|
+
if (objectThenMessage) objectErrorMessageEntries.push(`then:${JSON.stringify(objectThenMessage)}`);
|
|
184
|
+
const objectElseMessage = schema["x-else-message"] ?? objectIfMessage;
|
|
185
|
+
if (objectElseMessage) objectErrorMessageEntries.push(`else:${JSON.stringify(objectElseMessage)}`);
|
|
186
|
+
const objectUnevaluatedPropertiesMessage = schema["x-unevaluatedProperties-message"];
|
|
187
|
+
if (objectUnevaluatedPropertiesMessage) objectErrorMessageEntries.push(`unevaluatedProperties:${JSON.stringify(objectUnevaluatedPropertiesMessage)}`);
|
|
188
|
+
const objectErrorMessageField = objectErrorMessageEntries.length > 0 ? `errorMessage:{${objectErrorMessageEntries.join(",")}}` : void 0;
|
|
189
|
+
const optParts = [
|
|
190
|
+
...typeboxRefOpt(options?.ref),
|
|
191
|
+
schema.additionalProperties === false ? "additionalProperties:false" : void 0,
|
|
192
|
+
...buildAdvancedOpts(schema, rootName, isTypebox, childOptions),
|
|
193
|
+
objectErrorMessageField,
|
|
194
|
+
...typeboxMetaOpts(schema),
|
|
195
|
+
...typeboxDefaultOpt(schema)
|
|
196
|
+
].filter((v) => v !== void 0);
|
|
197
|
+
const opts = optParts.length > 0 ? `,{${optParts.join(",")}}` : "";
|
|
198
|
+
return `Type.Object({${props.join(",")}}${opts})`;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Build TypeBox option entries for advanced JSON Schema 2020-12 keywords.
|
|
202
|
+
* Returns an array of `'key:value'` strings ready to be joined into `{...}`.
|
|
203
|
+
*/
|
|
204
|
+
function buildAdvancedOpts(schema, rootName, isTypebox, options) {
|
|
205
|
+
const propertyNamesOpt = schema.propertyNames ? `propertyNames:${typebox(schema.propertyNames, rootName, isTypebox, options)}` : void 0;
|
|
206
|
+
const patternPropertiesOpt = schema.patternProperties ? `patternProperties:{${Object.entries(schema.patternProperties).map(([pattern, propSchema]) => `${JSON.stringify(pattern)}:${typebox(propSchema, rootName, isTypebox, options)}`).join(",")}}` : void 0;
|
|
207
|
+
const dependentRequiredOpt = schema.dependentRequired ? `dependentRequired:{${Object.entries(schema.dependentRequired).map(([key, deps]) => `${JSON.stringify(key)}:${JSON.stringify(deps)}`).join(",")}}` : void 0;
|
|
208
|
+
const ifOpt = schema.if ? `if:${typebox(schema.if, rootName, isTypebox, options)}` : void 0;
|
|
209
|
+
const thenOpt = schema.then ? `then:${typebox(schema.then, rootName, isTypebox, options)}` : void 0;
|
|
210
|
+
const elseOpt = schema.else ? `else:${typebox(schema.else, rootName, isTypebox, options)}` : void 0;
|
|
211
|
+
const requiredOpt = !schema.properties && Array.isArray(schema.required) && schema.required.length > 0 ? `required:${JSON.stringify(schema.required)}` : void 0;
|
|
212
|
+
return [
|
|
213
|
+
typeof schema.minProperties === "number" ? `minProperties:${schema.minProperties}` : void 0,
|
|
214
|
+
typeof schema.maxProperties === "number" ? `maxProperties:${schema.maxProperties}` : void 0,
|
|
215
|
+
propertyNamesOpt,
|
|
216
|
+
patternPropertiesOpt,
|
|
217
|
+
dependentRequiredOpt,
|
|
218
|
+
ifOpt,
|
|
219
|
+
thenOpt,
|
|
220
|
+
elseOpt,
|
|
221
|
+
requiredOpt
|
|
222
|
+
];
|
|
223
|
+
}
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/generator/typebox/string.ts
|
|
226
|
+
const FORMAT_MAP = {
|
|
227
|
+
email: "email",
|
|
228
|
+
uuid: "uuid",
|
|
229
|
+
uri: "uri",
|
|
230
|
+
ipv4: "ipv4",
|
|
231
|
+
ipv6: "ipv6",
|
|
232
|
+
"date-time": "date-time",
|
|
233
|
+
date: "date",
|
|
234
|
+
time: "time"
|
|
235
|
+
};
|
|
236
|
+
function escapeRegex(s) {
|
|
237
|
+
return s.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
238
|
+
}
|
|
239
|
+
function string(schema, ref) {
|
|
240
|
+
const errorMessage = schema["x-error-message"];
|
|
241
|
+
const requiredMessage = schema["x-required-message"];
|
|
242
|
+
const patternMessage = schema["x-pattern-message"];
|
|
243
|
+
const minLengthMessage = schema["x-minLength-message"];
|
|
244
|
+
const maxLengthMessage = schema["x-maxLength-message"];
|
|
245
|
+
const perKeywordEntries = [];
|
|
246
|
+
if (requiredMessage) perKeywordEntries.push(`required:${JSON.stringify(requiredMessage)}`);
|
|
247
|
+
if (patternMessage) perKeywordEntries.push(`pattern:${JSON.stringify(patternMessage)}`);
|
|
248
|
+
if (minLengthMessage) perKeywordEntries.push(`minLength:${JSON.stringify(minLengthMessage)}`);
|
|
249
|
+
if (maxLengthMessage) perKeywordEntries.push(`maxLength:${JSON.stringify(maxLengthMessage)}`);
|
|
250
|
+
const errorMessageField = perKeywordEntries.length === 0 ? errorMessage ? `errorMessage:${JSON.stringify(errorMessage)}` : void 0 : `errorMessage:{${perKeywordEntries.join(",")}${errorMessage ? `,_:${JSON.stringify(errorMessage)}` : ""}}`;
|
|
251
|
+
const isFixedLength = hasFixedLength(schema);
|
|
252
|
+
const contentPattern = (() => {
|
|
253
|
+
if (schema.pattern) return void 0;
|
|
254
|
+
const parts = [];
|
|
255
|
+
if (typeof schema["x-startsWith"] === "string") parts.push(`^${escapeRegex(schema["x-startsWith"])}`);
|
|
256
|
+
if (typeof schema["x-endsWith"] === "string") parts.push(`${escapeRegex(schema["x-endsWith"])}$`);
|
|
257
|
+
if (typeof schema["x-includes"] === "string") parts.push(escapeRegex(schema["x-includes"]));
|
|
258
|
+
return parts.length > 0 ? parts.join("|") : void 0;
|
|
259
|
+
})();
|
|
260
|
+
const opts = [
|
|
261
|
+
...typeboxRefOpt(ref),
|
|
262
|
+
schema.format && FORMAT_MAP[schema.format] ? `format:${JSON.stringify(FORMAT_MAP[schema.format])}` : void 0,
|
|
263
|
+
schema.pattern ? `pattern:${JSON.stringify(patternSource(schema.pattern))}` : contentPattern ? `pattern:${JSON.stringify(contentPattern)}` : void 0,
|
|
264
|
+
isFixedLength ? `minLength:${schema.minLength}` : void 0,
|
|
265
|
+
isFixedLength ? `maxLength:${schema.maxLength}` : void 0,
|
|
266
|
+
!isFixedLength && schema.minLength !== void 0 ? `minLength:${schema.minLength}` : void 0,
|
|
267
|
+
!isFixedLength && schema.maxLength !== void 0 ? `maxLength:${schema.maxLength}` : void 0,
|
|
268
|
+
errorMessageField,
|
|
269
|
+
...typeboxMetaOpts(schema),
|
|
270
|
+
...typeboxDefaultOpt(schema)
|
|
271
|
+
].filter((v) => v !== void 0);
|
|
272
|
+
const stringExpr = opts.length > 0 ? `Type.String({${opts.join(",")}})` : "Type.String()";
|
|
273
|
+
const transforms = [];
|
|
274
|
+
if (schema["x-trim"] === true) transforms.push(".trim()");
|
|
275
|
+
if (schema["x-toLowerCase"] === true) transforms.push(".toLowerCase()");
|
|
276
|
+
if (schema["x-toUpperCase"] === true) transforms.push(".toUpperCase()");
|
|
277
|
+
if (typeof schema["x-normalize"] === "string") transforms.push(`.normalize(${JSON.stringify(schema["x-normalize"])})`);
|
|
278
|
+
if (transforms.length > 0) return `Codec(${stringExpr}).Decode((value: string) => value${transforms.join("")}).Encode((value: string) => value)`;
|
|
279
|
+
return stringExpr;
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/generator/typebox/typebox.ts
|
|
283
|
+
/**
|
|
284
|
+
* Options for a nested TypeBox node: the same as `options` but without a
|
|
285
|
+
* host `ref`, so only the outermost factory of a declaration receives it.
|
|
286
|
+
*/
|
|
287
|
+
function nestedTypeboxOptions(options) {
|
|
288
|
+
if (options === void 0) return void 0;
|
|
289
|
+
return {
|
|
290
|
+
...options.openapi !== void 0 && { openapi: options.openapi },
|
|
291
|
+
...options.readonly !== void 0 && { readonly: options.readonly },
|
|
292
|
+
...options.paramIn !== void 0 && { paramIn: options.paramIn },
|
|
293
|
+
...options.cyclicRefs !== void 0 && { cyclicRefs: options.cyclicRefs }
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Emits a single-argument TypeBox factory call (`Type.X({opts})`), embedding
|
|
298
|
+
* any meta options from the schema. Returns `Type.X()` when no options.
|
|
299
|
+
*/
|
|
300
|
+
function tbPrim(name, schema, extraOpts = [], ref) {
|
|
301
|
+
const opts = [
|
|
302
|
+
...typeboxRefOpt(ref),
|
|
303
|
+
...extraOpts,
|
|
304
|
+
...typeboxMetaOpts(schema),
|
|
305
|
+
...typeboxDefaultOpt(schema)
|
|
306
|
+
];
|
|
307
|
+
return opts.length === 0 ? `${name}()` : `${name}({${opts.join(",")}})`;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The `Type.String(...)` input of a string-wire coercion `Codec`. A query/path
|
|
311
|
+
* default is carried here in its string-wire form (`{default:'1'}`), since the
|
|
312
|
+
* Codec decodes from a string. A host `ref` lands on this inner `Type.String`,
|
|
313
|
+
* not on the `Codec(...)` wrapper.
|
|
314
|
+
*/
|
|
315
|
+
function wireString(schema, ref, pattern) {
|
|
316
|
+
const opts = [
|
|
317
|
+
...typeboxRefOpt(ref),
|
|
318
|
+
...pattern === void 0 ? [] : [`pattern:${pattern}`],
|
|
319
|
+
...typeboxDefaultOpt(schema, true)
|
|
320
|
+
];
|
|
321
|
+
return opts.length > 0 ? `Type.String({${opts.join(",")}})` : "Type.String()";
|
|
322
|
+
}
|
|
323
|
+
/** A keyword-only `allOf` member. TypeBox has no type-guarded check, so the keyword's type carries it. */
|
|
324
|
+
function keywordSchema(schema) {
|
|
325
|
+
if (hasStringKeyword(schema) && !hasNumericKeyword(schema)) return string({
|
|
326
|
+
...schema,
|
|
327
|
+
type: "string"
|
|
328
|
+
});
|
|
329
|
+
if (hasNumericKeyword(schema) && !hasStringKeyword(schema)) return number({
|
|
330
|
+
...schema,
|
|
331
|
+
type: "number"
|
|
332
|
+
});
|
|
333
|
+
return "Type.Any()";
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Emits a two-argument TypeBox factory call (`Type.X(payload, {opts})`),
|
|
337
|
+
* embedding any meta options from the schema. Returns `Type.X(payload)` when
|
|
338
|
+
* no options.
|
|
339
|
+
*/
|
|
340
|
+
function tbComp(name, payload, schema, extraOpts = [], ref) {
|
|
341
|
+
const opts = [
|
|
342
|
+
...typeboxRefOpt(ref),
|
|
343
|
+
...extraOpts,
|
|
344
|
+
...typeboxMetaOpts(schema),
|
|
345
|
+
...typeboxDefaultOpt(schema)
|
|
346
|
+
];
|
|
347
|
+
return opts.length === 0 ? `${name}(${payload})` : `${name}(${payload},{${opts.join(",")}})`;
|
|
348
|
+
}
|
|
349
|
+
/** A reference to a peer of a `Type.Cyclic` map, reached by name. */
|
|
350
|
+
function byName(name, s) {
|
|
351
|
+
return tbComp("Type.Ref", `'${name}'`, s);
|
|
352
|
+
}
|
|
353
|
+
function messageOpt(msg) {
|
|
354
|
+
return typeof msg === "string" ? [`errorMessage:${JSON.stringify(msg)}`] : [];
|
|
355
|
+
}
|
|
356
|
+
function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, options) {
|
|
357
|
+
const isStringWireParam = isStringWireLocation(options?.paramIn) && schema["x-coerce"] !== false;
|
|
358
|
+
const readonly = (v) => options?.readonly ? `Type.Readonly(${v})` : v;
|
|
359
|
+
const hostRef = options?.ref;
|
|
360
|
+
const childOptions = nestedTypeboxOptions(options);
|
|
361
|
+
const named = (name, s) => options?.cyclicRefs?.has(name) ? byName(name, s) : name;
|
|
362
|
+
if (schema.$ref) {
|
|
363
|
+
const ref = (s) => {
|
|
364
|
+
if (isRootRef(s.$ref)) return byName(rootName, s);
|
|
365
|
+
if (typeof s.$ref === "string" && isDeepLocalPointer(s.$ref)) return "Type.Unknown()";
|
|
366
|
+
if (options?.openapi && s.$ref) {
|
|
367
|
+
const resolved = resolveOpenAPIRef(s.$ref);
|
|
368
|
+
if (resolved) return resolved === rootName ? byName(resolved, s) : named(resolved, s);
|
|
369
|
+
}
|
|
370
|
+
const toName = schemaNamer(options?.openapi);
|
|
371
|
+
for (const prefix of DEFINITION_REF_PREFIXES) if (s.$ref?.startsWith(prefix)) return named(toName(s.$ref.slice(prefix.length)), s);
|
|
372
|
+
if (s.$ref?.startsWith("#")) {
|
|
373
|
+
const refName = s.$ref.slice(1);
|
|
374
|
+
return refName === "" ? named(rootName, s) : named(toName(refName), s);
|
|
375
|
+
}
|
|
376
|
+
if (s.$ref?.includes("#")) return "Type.Unknown()";
|
|
377
|
+
if (s.$ref?.startsWith("http")) {
|
|
378
|
+
const last = s.$ref.split("/").at(-1);
|
|
379
|
+
if (last) return last.replace(/\.json$/, "");
|
|
380
|
+
}
|
|
381
|
+
return "Type.Any()";
|
|
382
|
+
};
|
|
383
|
+
const items = itemsSchemaOf(schema.items);
|
|
384
|
+
if (schema.type === "array" && items?.$ref) return `Type.Array(${typeboxWrap(ref(items), items)})`;
|
|
385
|
+
return typeboxWrap(ref(schema), schema);
|
|
386
|
+
}
|
|
387
|
+
if (schema.oneOf) {
|
|
388
|
+
if (schema.oneOf.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
389
|
+
const schemas = schema.oneOf.map((s) => typebox(s, rootName, isTypebox, childOptions));
|
|
390
|
+
return typeboxWrap(tbComp("Type.Union", `[${schemas.join(",")}]`, schema, messageOpt(schema["x-oneOf-message"]), hostRef), schema);
|
|
391
|
+
}
|
|
392
|
+
if (schema.anyOf) {
|
|
393
|
+
if (schema.anyOf.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
394
|
+
const schemas = schema.anyOf.map((s) => typebox(s, rootName, isTypebox, childOptions));
|
|
395
|
+
const anyOfMessage = schema["x-implication-message"] ?? schema["x-anyOf-message"];
|
|
396
|
+
return typeboxWrap(tbComp("Type.Union", `[${schemas.join(",")}]`, schema, messageOpt(anyOfMessage), hostRef), schema);
|
|
397
|
+
}
|
|
398
|
+
if (schema.allOf) {
|
|
399
|
+
if (schema.allOf.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
400
|
+
const nullable = isNullable(schema) || schema.allOf.some(isNullTypeMember);
|
|
401
|
+
const defaultValue = schema.allOf.find(isDefaultOnlyMember)?.default;
|
|
402
|
+
const schemas = foldAllOfMembers(schema.allOf.filter((s) => !isShapelessMember(s)), isStringWireParam).map((s) => isKeywordOnlyMember(s) ? keywordSchema(s) : typebox(s, rootName, isTypebox, childOptions));
|
|
403
|
+
if (schemas.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), {
|
|
404
|
+
...schema,
|
|
405
|
+
nullable
|
|
406
|
+
});
|
|
407
|
+
const unevaluated = schema.unevaluatedProperties;
|
|
408
|
+
const unevaluatedOpt = unevaluated === false ? ["unevaluatedProperties:false"] : typeof unevaluated === "object" && unevaluated !== null ? [`unevaluatedProperties:${typebox(unevaluated, rootName, isTypebox, childOptions)}`] : [];
|
|
409
|
+
const baseResult = schemas.length === 1 && unevaluatedOpt.length === 0 ? schemas[0] : tbComp("Type.Intersect", `[${schemas.join(",")}]`, schema, [...messageOpt(schema["x-allOf-message"]), ...unevaluatedOpt], hostRef);
|
|
410
|
+
if (defaultValue !== void 0) {
|
|
411
|
+
const formatLiteral = typeof defaultValue === "boolean" || typeof defaultValue === "number" ? `${defaultValue}` : JSON.stringify(defaultValue);
|
|
412
|
+
const intersectOpts = [
|
|
413
|
+
...typeboxRefOpt(hostRef),
|
|
414
|
+
...messageOpt(schema["x-allOf-message"]),
|
|
415
|
+
`default:${formatLiteral}`
|
|
416
|
+
];
|
|
417
|
+
const withDefault = `Type.Optional(${`Type.Intersect([${schemas.join(",")}],{${intersectOpts.join(",")}})`})`;
|
|
418
|
+
return nullable ? `Type.Union([${withDefault},Type.Null()])` : withDefault;
|
|
419
|
+
}
|
|
420
|
+
return typeboxWrap(baseResult, {
|
|
421
|
+
...schema,
|
|
422
|
+
nullable
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
if (schema.not) return typeboxWrap(tbPrim("Type.Any", schema, messageOpt(schema["x-not-message"]), hostRef), schema);
|
|
426
|
+
if (schema.const !== void 0) {
|
|
427
|
+
const constMessage = schema["x-const-message"] ?? schema["x-error-message"];
|
|
428
|
+
if (schema.const === null) return typeboxWrap(tbPrim("Type.Null", schema, messageOpt(constMessage), hostRef), schema);
|
|
429
|
+
if (typeof schema.const === "object") return typeboxWrap(tbPrim("Type.Any", schema, messageOpt(constMessage), hostRef), schema);
|
|
430
|
+
return typeboxWrap(tbComp("Type.Literal", JSON.stringify(schema.const), schema, messageOpt(constMessage), hostRef), schema);
|
|
431
|
+
}
|
|
432
|
+
if (schema.enum) return typeboxWrap(_enum(schema, hostRef), schema);
|
|
433
|
+
if (schema.properties) return readonly(typeboxWrap(object(schema, rootName, isTypebox, options), schema));
|
|
434
|
+
const types = normalizeTypes(schema.type);
|
|
435
|
+
if (types.includes("string")) return typeboxWrap(string(schema, hostRef), schema);
|
|
436
|
+
if (types.includes("number")) {
|
|
437
|
+
const base = number(schema, hostRef);
|
|
438
|
+
if (isStringWireParam) {
|
|
439
|
+
const wire = wireString(schema, hostRef, numberWirePattern());
|
|
440
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>Number(value)).Encode((value)=>String(value))`, schema);
|
|
441
|
+
}
|
|
442
|
+
return typeboxWrap(base, schema);
|
|
443
|
+
}
|
|
444
|
+
if (types.includes("integer")) {
|
|
445
|
+
const base = integer(schema, hostRef);
|
|
446
|
+
if (isStringWireParam) {
|
|
447
|
+
const wire = wireString(schema, hostRef, integerWirePattern());
|
|
448
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>Number(value)).Encode((value)=>String(value))`, schema);
|
|
449
|
+
}
|
|
450
|
+
return typeboxWrap(base, schema);
|
|
451
|
+
}
|
|
452
|
+
if (types.includes("boolean")) {
|
|
453
|
+
if (isStringWireParam) {
|
|
454
|
+
const wire = wireString(schema, hostRef, stringboolPattern());
|
|
455
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>${stringboolTruthyPattern()}.test(value)).Encode((value)=>value?'true':'false')`, schema);
|
|
456
|
+
}
|
|
457
|
+
return typeboxWrap(tbPrim("Type.Boolean", schema, [], hostRef), schema);
|
|
458
|
+
}
|
|
459
|
+
if (types.includes("array")) {
|
|
460
|
+
if (schema.prefixItems?.length) {
|
|
461
|
+
const items = schema.prefixItems.map((s) => typebox(s, rootName, isTypebox, childOptions));
|
|
462
|
+
const prefixItemsMessage = schema["x-prefixItems-message"];
|
|
463
|
+
const tupleOpts = prefixItemsMessage ? [`errorMessage:{prefixItems:${JSON.stringify(prefixItemsMessage)},items:${JSON.stringify(prefixItemsMessage)}}`] : [];
|
|
464
|
+
return readonly(typeboxWrap(tbComp("Type.Tuple", `[${items.join(",")}]`, schema, tupleOpts, hostRef), schema));
|
|
465
|
+
}
|
|
466
|
+
const itemSchema = itemsSchemaOf(schema.items);
|
|
467
|
+
const items = itemSchema ? typebox(itemSchema, rootName, isTypebox, childOptions) : "Type.Any()";
|
|
468
|
+
const arrayErrorMessageEntries = [];
|
|
469
|
+
const arrayErrorMessage = schema["x-error-message"];
|
|
470
|
+
if (arrayErrorMessage) arrayErrorMessageEntries.push(`type:${JSON.stringify(arrayErrorMessage)}`);
|
|
471
|
+
const arrayLengthMessage = schema["x-length-message"];
|
|
472
|
+
const arrayMinItemsMessage = schema["x-minItems-message"] ?? (typeof schema.minItems === "number" ? arrayLengthMessage : void 0);
|
|
473
|
+
if (arrayMinItemsMessage) arrayErrorMessageEntries.push(`minItems:${JSON.stringify(arrayMinItemsMessage)}`);
|
|
474
|
+
const arrayMaxItemsMessage = schema["x-maxItems-message"] ?? (typeof schema.maxItems === "number" ? arrayLengthMessage : void 0);
|
|
475
|
+
if (arrayMaxItemsMessage) arrayErrorMessageEntries.push(`maxItems:${JSON.stringify(arrayMaxItemsMessage)}`);
|
|
476
|
+
const arrayUniqueItemsMessage = schema["x-uniqueItems-message"];
|
|
477
|
+
if (arrayUniqueItemsMessage) arrayErrorMessageEntries.push(`uniqueItems:${JSON.stringify(arrayUniqueItemsMessage)}`);
|
|
478
|
+
const arrayContainsMessage = schema["x-contains-message"];
|
|
479
|
+
if (arrayContainsMessage) arrayErrorMessageEntries.push(`contains:${JSON.stringify(arrayContainsMessage)}`);
|
|
480
|
+
const arrayMinContainsMessage = schema["x-minContains-message"];
|
|
481
|
+
if (arrayMinContainsMessage) arrayErrorMessageEntries.push(`minContains:${JSON.stringify(arrayMinContainsMessage)}`);
|
|
482
|
+
const arrayMaxContainsMessage = schema["x-maxContains-message"];
|
|
483
|
+
if (arrayMaxContainsMessage) arrayErrorMessageEntries.push(`maxContains:${JSON.stringify(arrayMaxContainsMessage)}`);
|
|
484
|
+
const arrayItemsMessage = schema["x-items-message"];
|
|
485
|
+
if (arrayItemsMessage) arrayErrorMessageEntries.push(`items:${JSON.stringify(arrayItemsMessage)}`);
|
|
486
|
+
const arrayUnevaluatedItemsMessage = schema["x-unevaluatedItems-message"];
|
|
487
|
+
if (arrayUnevaluatedItemsMessage) arrayErrorMessageEntries.push(`unevaluatedItems:${JSON.stringify(arrayUnevaluatedItemsMessage)}`);
|
|
488
|
+
const arrayOpts = [
|
|
489
|
+
typeof schema.minItems === "number" ? `minItems:${schema.minItems}` : void 0,
|
|
490
|
+
typeof schema.maxItems === "number" ? `maxItems:${schema.maxItems}` : void 0,
|
|
491
|
+
schema.uniqueItems === true ? `uniqueItems:true` : void 0,
|
|
492
|
+
arrayErrorMessageEntries.length > 0 ? `errorMessage:{${arrayErrorMessageEntries.join(",")}}` : void 0
|
|
493
|
+
].filter((v) => v !== void 0);
|
|
494
|
+
return readonly(typeboxWrap(tbComp("Type.Array", items, schema, arrayOpts, hostRef), schema));
|
|
495
|
+
}
|
|
496
|
+
if (types.includes("object")) return readonly(typeboxWrap(object(schema, rootName, isTypebox, options), schema));
|
|
497
|
+
if (types.includes("date")) {
|
|
498
|
+
const wire = wireString(schema, hostRef);
|
|
499
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>new Date(value)).Encode((value)=>value.toISOString())`, schema);
|
|
500
|
+
}
|
|
501
|
+
if (types.length === 1 && types[0] === "null") return typeboxWrap(tbPrim("Type.Null", schema, [], hostRef), schema);
|
|
502
|
+
if (typeof schema.additionalProperties === "object") return readonly(typeboxWrap(object(schema, rootName, isTypebox, options), schema));
|
|
503
|
+
return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
504
|
+
}
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/generator/typebox/index.ts
|
|
507
|
+
/** The import line, carrying `Codec` only when the emitted source calls it. */
|
|
508
|
+
function importLine(emitted) {
|
|
509
|
+
return `import { ${emitted.includes("Codec(") ? "Codec, " : ""}Type, type Static } from 'typebox'`;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* A whole JSON Schema document as a TypeBox module. A `$ref` cycle cannot be expressed
|
|
513
|
+
* with plain `const` declarations, so a cyclic document becomes one
|
|
514
|
+
* `Type.Cyclic($defs, root)` map instead.
|
|
515
|
+
*/
|
|
516
|
+
function schemaToTypebox(input, options) {
|
|
517
|
+
const schema = withPolymorphicBases(hoistNestedRefs(input));
|
|
518
|
+
const exportType = options?.exportType ?? true;
|
|
519
|
+
const openapi = options?.openapi ?? false;
|
|
520
|
+
const hostRef = options?.ref;
|
|
521
|
+
const genOptions = {
|
|
522
|
+
openapi,
|
|
523
|
+
readonly: options?.readonly ?? false,
|
|
524
|
+
...options?.paramIn !== void 0 && { paramIn: options.paramIn }
|
|
525
|
+
};
|
|
526
|
+
const rootOptions = hostRef === void 0 ? genOptions : {
|
|
527
|
+
...genOptions,
|
|
528
|
+
ref: hostRef
|
|
529
|
+
};
|
|
530
|
+
const notKeywordPresent = hasNotKeyword(schema);
|
|
531
|
+
const toName = schemaNamer(openapi);
|
|
532
|
+
const plan = planDocument(schema, rootNameOf(schema, toName));
|
|
533
|
+
const { rootName } = plan;
|
|
534
|
+
if (hasRootSelfReference(plan.root, (ref) => isRootRef(ref) || openapi && resolveOpenAPIRef(ref) === rootName) || cyclicDefinitionNames(schema).size > 0) {
|
|
535
|
+
const cyclicRefs = /* @__PURE__ */ new Set([...plan.orderedNames.map(toName), rootName]);
|
|
536
|
+
const cyclicOptions = {
|
|
537
|
+
...genOptions,
|
|
538
|
+
cyclicRefs
|
|
539
|
+
};
|
|
540
|
+
const cyclicExport = `export const ${rootName} = Type.Cyclic({\n${[...plan.nonRootNames.map((name) => {
|
|
541
|
+
const def = plan.definitions[name];
|
|
542
|
+
const pc = toName(name);
|
|
543
|
+
return def ? `${pc}: ${typebox(def, pc, true, cyclicOptions)}` : missingDefinitionComment(name);
|
|
544
|
+
}), `${rootName}: ${typebox(plan.root, rootName, true, cyclicOptions)}`].join(",\n")}\n},'${rootName}'${hostRef === void 0 ? "" : `,{ref:${JSON.stringify(hostRef)}}`})`;
|
|
545
|
+
return [
|
|
546
|
+
...notKeywordPresent ? [NOT_KEYWORD_UNSUPPORTED_MARKER] : [],
|
|
547
|
+
importLine(cyclicExport),
|
|
548
|
+
cyclicExport,
|
|
549
|
+
...exportType ? [`export type ${rootName} = Static<typeof ${rootName}>`] : []
|
|
550
|
+
].filter(Boolean).join("\n\n");
|
|
551
|
+
}
|
|
552
|
+
const schemaDefsCode = renderDefinitions(plan, toName, (def, name) => `const ${name} = ${typebox(def, name, true, genOptions)}`);
|
|
553
|
+
const rootExport = `export const ${rootName} = ${typebox(plan.root, rootName, true, rootOptions)}`;
|
|
554
|
+
return [
|
|
555
|
+
...notKeywordPresent ? [NOT_KEYWORD_UNSUPPORTED_MARKER] : [],
|
|
556
|
+
importLine(`${schemaDefsCode}\n${rootExport}`),
|
|
557
|
+
schemaDefsCode,
|
|
558
|
+
rootExport,
|
|
559
|
+
...exportType ? [`export type ${rootName} = Static<typeof ${rootName}>`] : []
|
|
560
|
+
].filter(Boolean).join("\n\n");
|
|
561
|
+
}
|
|
562
|
+
//#endregion
|
|
563
|
+
export { nestedTypeboxOptions as n, typebox as r, schemaToTypebox as t };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { r as JSONSchema } from "./index-G7UqNQ4w.js";
|
|
2
|
+
import { n as GenerateOptions } from "./index-DYzAccyT.js";
|
|
3
|
+
//#region src/generator/typebox/index.d.ts
|
|
4
|
+
/** The options of `schemaToTypebox`: the common ones, plus a host `$ref` name. */
|
|
5
|
+
export type TypeboxGenerateOptions = Omit<GenerateOptions, 'unsafeCodeExtensions'> & {
|
|
6
|
+
/**
|
|
7
|
+
* Host `$ref` name for OpenAPI component registration. TypeBox has no
|
|
8
|
+
* post-construction `.meta()`, so this lands in the outermost builder's
|
|
9
|
+
* options (`Type.String({ref})`, `Type.Object({…},{ref})`, or the third
|
|
10
|
+
* argument of `Type.Cyclic`). Named `$defs` members are not given this
|
|
11
|
+
* value. Omit it and the emit is unchanged.
|
|
12
|
+
*/
|
|
13
|
+
readonly ref?: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A whole JSON Schema document as a TypeBox module. A `$ref` cycle cannot be expressed
|
|
17
|
+
* with plain `const` declarations, so a cyclic document becomes one
|
|
18
|
+
* `Type.Cyclic($defs, root)` map instead.
|
|
19
|
+
*/
|
|
20
|
+
export declare function schemaToTypebox(input: JSONSchema, options?: TypeboxGenerateOptions): string;
|
|
21
|
+
//#endregion
|
package/dist/typebox.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as schemaToTypebox } from "./typebox-wYeJqy7T.js";
|