schema-truth 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +143 -2
- package/dist/ajv-DTP2FXRr.js +105 -0
- package/dist/ajv.d.ts +24 -0
- package/dist/ajv.js +1 -0
- package/dist/arktype-BdvvaBQX.js +746 -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-CL2mSark.js +124 -0
- package/dist/effect-mUknRPv_.js +645 -0
- package/dist/effect.d.ts +12 -0
- package/dist/effect.js +1 -0
- package/dist/helper-Cfvb5UDq.js +1115 -0
- package/dist/index-C2L8Ty0-.d.ts +65 -0
- package/dist/index-DbV3dEmN.d.ts +551 -0
- package/dist/index.d.ts +303 -0
- package/dist/index.js +10 -0
- package/dist/parser-CyJ2auea.js +36 -0
- package/dist/typebox-BdWJv3ND.js +544 -0
- package/dist/typebox.d.ts +21 -0
- package/dist/typebox.js +1 -0
- package/dist/utils-WNBq1HR0.js +1017 -0
- package/dist/valibot-CaJIynCH.js +664 -0
- package/dist/valibot.d.ts +9 -0
- package/dist/valibot.js +1 -0
- package/dist/yup-CM2TSFig.js +92 -0
- package/dist/yup.d.ts +17 -0
- package/dist/yup.js +1 -0
- package/dist/zod-BsXLZfwK.js +777 -0
- package/dist/zod.d.ts +9 -0
- package/dist/zod.js +1 -0
- package/package.json +112 -4
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
//#region src/constants/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* The values the generators agree on: every list of literals, every prefix and every
|
|
4
|
+
* pattern that more than one module reads. Types that mirror a list are derived from it in
|
|
5
|
+
* `parser/index.ts`, so adding a member here is the whole change.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The `type` values a JSON Schema node may carry, in the order the specification lists
|
|
9
|
+
* them. `date` is not a JSON Schema type: it is the extension the generators accept for a
|
|
10
|
+
* `Date`-typed value (Zod `z.date()`, Valibot `v.date()`).
|
|
11
|
+
*
|
|
12
|
+
* @see https://json-schema.org/understanding-json-schema/reference/type
|
|
13
|
+
*/
|
|
14
|
+
const JSON_SCHEMA_TYPES = [
|
|
15
|
+
"string",
|
|
16
|
+
"number",
|
|
17
|
+
"integer",
|
|
18
|
+
"boolean",
|
|
19
|
+
"array",
|
|
20
|
+
"object",
|
|
21
|
+
"null",
|
|
22
|
+
"date"
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* The `format` values a string node may carry: the JSON Schema built-ins, the OpenAPI
|
|
26
|
+
* data-type formats, and the names the target libraries add (`uuidv7`, `cuid2`, `jwt`,
|
|
27
|
+
* `hash`, ...). A format a target has no equivalent for is a no-op there.
|
|
28
|
+
*
|
|
29
|
+
* @see https://json-schema.org/understanding-json-schema/reference/string#built-in-formats
|
|
30
|
+
* @see https://zod.dev/api#strings
|
|
31
|
+
*/
|
|
32
|
+
const STRING_FORMATS = [
|
|
33
|
+
"date-time",
|
|
34
|
+
"date",
|
|
35
|
+
"time",
|
|
36
|
+
"duration",
|
|
37
|
+
"email",
|
|
38
|
+
"idn-email",
|
|
39
|
+
"hostname",
|
|
40
|
+
"idn-hostname",
|
|
41
|
+
"ipv4",
|
|
42
|
+
"ipv6",
|
|
43
|
+
"uri",
|
|
44
|
+
"uri-reference",
|
|
45
|
+
"iri",
|
|
46
|
+
"iri-reference",
|
|
47
|
+
"uuid",
|
|
48
|
+
"uri-template",
|
|
49
|
+
"json-pointer",
|
|
50
|
+
"relative-json-pointer",
|
|
51
|
+
"regex",
|
|
52
|
+
"binary",
|
|
53
|
+
"byte",
|
|
54
|
+
"password",
|
|
55
|
+
"url",
|
|
56
|
+
"httpUrl",
|
|
57
|
+
"uuidv4",
|
|
58
|
+
"uuidv6",
|
|
59
|
+
"uuidv7",
|
|
60
|
+
"guid",
|
|
61
|
+
"emoji",
|
|
62
|
+
"base64",
|
|
63
|
+
"base64url",
|
|
64
|
+
"hex",
|
|
65
|
+
"nanoid",
|
|
66
|
+
"cuid",
|
|
67
|
+
"cuid2",
|
|
68
|
+
"ulid",
|
|
69
|
+
"ksuid",
|
|
70
|
+
"xid",
|
|
71
|
+
"ip",
|
|
72
|
+
"cidrv4",
|
|
73
|
+
"cidrv6",
|
|
74
|
+
"mac",
|
|
75
|
+
"jwt",
|
|
76
|
+
"hash",
|
|
77
|
+
"e164",
|
|
78
|
+
"creditCard",
|
|
79
|
+
"iban",
|
|
80
|
+
"currencyCode",
|
|
81
|
+
"httpurl",
|
|
82
|
+
"creditcard",
|
|
83
|
+
"currencycode",
|
|
84
|
+
"toLowerCase",
|
|
85
|
+
"toUpperCase",
|
|
86
|
+
"trim",
|
|
87
|
+
"slugify"
|
|
88
|
+
];
|
|
89
|
+
/**
|
|
90
|
+
* The `format` values a numeric node may carry: the OpenAPI data-type formats plus the
|
|
91
|
+
* widths the target libraries distinguish (`int32`, `float32`, `bigint`, ...).
|
|
92
|
+
*
|
|
93
|
+
* @see https://spec.openapis.org/oas/v3.1.0#data-types
|
|
94
|
+
*/
|
|
95
|
+
const NUMBER_FORMATS = [
|
|
96
|
+
"int32",
|
|
97
|
+
"uint32",
|
|
98
|
+
"int64",
|
|
99
|
+
"uint64",
|
|
100
|
+
"bigint",
|
|
101
|
+
"float",
|
|
102
|
+
"float32",
|
|
103
|
+
"float64",
|
|
104
|
+
"double",
|
|
105
|
+
"decimal"
|
|
106
|
+
];
|
|
107
|
+
/**
|
|
108
|
+
* The `contentEncoding` values of a string node.
|
|
109
|
+
*
|
|
110
|
+
* @see https://json-schema.org/draft/2020-12/json-schema-validation#section-8.3
|
|
111
|
+
*/
|
|
112
|
+
const CONTENT_ENCODINGS = [
|
|
113
|
+
"base64",
|
|
114
|
+
"base64url",
|
|
115
|
+
"binary",
|
|
116
|
+
"7bit",
|
|
117
|
+
"8bit",
|
|
118
|
+
"quoted-printable"
|
|
119
|
+
];
|
|
120
|
+
/**
|
|
121
|
+
* The OpenAPI parameter locations, plus `formData` for the fields of an
|
|
122
|
+
* `application/x-www-form-urlencoded` or `multipart/form-data` body. Every one of them is
|
|
123
|
+
* string-wire: a primitive schema emitted under one of them is coerced from a string.
|
|
124
|
+
*
|
|
125
|
+
* @see https://spec.openapis.org/oas/v3.2.0.html#parameter-object
|
|
126
|
+
*/
|
|
127
|
+
const PARAM_LOCATIONS = [
|
|
128
|
+
"query",
|
|
129
|
+
"path",
|
|
130
|
+
"header",
|
|
131
|
+
"cookie",
|
|
132
|
+
"formData"
|
|
133
|
+
];
|
|
134
|
+
/** The parameter locations whose values arrive as strings and are coerced. */
|
|
135
|
+
const STRING_WIRE_LOCATIONS = [
|
|
136
|
+
"query",
|
|
137
|
+
"path",
|
|
138
|
+
"header",
|
|
139
|
+
"cookie",
|
|
140
|
+
"formData"
|
|
141
|
+
];
|
|
142
|
+
/**
|
|
143
|
+
* The `type` values a value on the wire is coerced to, in the order a union tries them:
|
|
144
|
+
* the text `1` is an integer before it is a number, and a string last of all.
|
|
145
|
+
*/
|
|
146
|
+
const WIRE_PRIMITIVE_ORDER = [
|
|
147
|
+
"integer",
|
|
148
|
+
"number",
|
|
149
|
+
"boolean",
|
|
150
|
+
"string"
|
|
151
|
+
];
|
|
152
|
+
/**
|
|
153
|
+
* The spellings of a boolean on the wire, as Zod's `z.stringbool()` reads them: `true`,
|
|
154
|
+
* `1`, `yes`, `on`, `y`, `enabled` and their opposites, in any case.
|
|
155
|
+
*/
|
|
156
|
+
const STRINGBOOL_PATTERN = /^(?:true|1|yes|on|y|enabled|false|0|no|off|n|disabled)$/i;
|
|
157
|
+
/** The spellings of {@link STRINGBOOL_PATTERN} that read as `true`. */
|
|
158
|
+
const STRINGBOOL_TRUTHY_PATTERN = /^(?:true|1|yes|on|y|enabled)$/i;
|
|
159
|
+
/** The text of a 64-bit integer: a sign and digits, which `BigInt` reads losslessly. */
|
|
160
|
+
const INTEGER_TEXT_PATTERN = /^-?\d+$/;
|
|
161
|
+
/** The range of each fixed-width numeric format, as the literals the generators emit. */
|
|
162
|
+
const NUMBER_RANGES = {
|
|
163
|
+
int32: {
|
|
164
|
+
minimum: "-2147483648",
|
|
165
|
+
maximum: "2147483647"
|
|
166
|
+
},
|
|
167
|
+
uint32: {
|
|
168
|
+
minimum: "0",
|
|
169
|
+
maximum: "4294967295"
|
|
170
|
+
},
|
|
171
|
+
int64: {
|
|
172
|
+
minimum: "-9223372036854775808n",
|
|
173
|
+
maximum: "9223372036854775807n"
|
|
174
|
+
},
|
|
175
|
+
uint64: {
|
|
176
|
+
minimum: "0n",
|
|
177
|
+
maximum: "18446744073709551615n"
|
|
178
|
+
},
|
|
179
|
+
float32: {
|
|
180
|
+
minimum: "-3.4028234663852886e38",
|
|
181
|
+
maximum: "3.4028234663852886e38"
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* The `style` values of an OpenAPI Parameter Object: how the parameter's text maps onto its
|
|
186
|
+
* schema.
|
|
187
|
+
*
|
|
188
|
+
* @see https://spec.openapis.org/oas/v3.2.0.html#style-values
|
|
189
|
+
*/
|
|
190
|
+
const PARAM_STYLES = [
|
|
191
|
+
"matrix",
|
|
192
|
+
"label",
|
|
193
|
+
"simple",
|
|
194
|
+
"form",
|
|
195
|
+
"spaceDelimited",
|
|
196
|
+
"pipeDelimited",
|
|
197
|
+
"deepObject"
|
|
198
|
+
];
|
|
199
|
+
/**
|
|
200
|
+
* The properties every plain object inherits from `Object.prototype`. A parameter object
|
|
201
|
+
* read from a request answers these even when the request did not send them.
|
|
202
|
+
*/
|
|
203
|
+
const OBJECT_PROTOTYPE_PROPERTIES = [
|
|
204
|
+
"__proto__",
|
|
205
|
+
"__defineGetter__",
|
|
206
|
+
"__defineSetter__",
|
|
207
|
+
"__lookupGetter__",
|
|
208
|
+
"__lookupSetter__",
|
|
209
|
+
"constructor",
|
|
210
|
+
"hasOwnProperty",
|
|
211
|
+
"isPrototypeOf",
|
|
212
|
+
"propertyIsEnumerable",
|
|
213
|
+
"toLocaleString",
|
|
214
|
+
"toString",
|
|
215
|
+
"valueOf"
|
|
216
|
+
];
|
|
217
|
+
/**
|
|
218
|
+
* The string formats checked with a regular expression where a target has no validator
|
|
219
|
+
* of its own, keyed by the lower-cased format. The patterns are Zod's, so every target
|
|
220
|
+
* accepts and rejects the same text.
|
|
221
|
+
*
|
|
222
|
+
* @see https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/core/regexes.ts
|
|
223
|
+
*/
|
|
224
|
+
const FORMAT_PATTERNS = {
|
|
225
|
+
guid: /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/,
|
|
226
|
+
cuid: /^[cC][0-9a-z]{6,}$/,
|
|
227
|
+
cuid2: /^[0-9a-z]+$/,
|
|
228
|
+
ulid: /^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$/,
|
|
229
|
+
xid: /^[0-9a-vA-V]{20}$/,
|
|
230
|
+
ksuid: /^[A-Za-z0-9]{27}$/,
|
|
231
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
232
|
+
duration: /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/,
|
|
233
|
+
emoji: /^[\p{Extended_Pictographic}\p{Emoji_Component}]+$/u,
|
|
234
|
+
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
|
|
235
|
+
ipv6: /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/,
|
|
236
|
+
mac: /^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/,
|
|
237
|
+
cidrv4: /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/,
|
|
238
|
+
cidrv6: /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
239
|
+
base64: /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/,
|
|
240
|
+
base64url: /^[A-Za-z0-9_-]*$/,
|
|
241
|
+
hostname: /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,
|
|
242
|
+
e164: /^\+[1-9]\d{6,14}$/,
|
|
243
|
+
hex: /^[0-9a-fA-F]*$/
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* The source of an RFC 3339 `full-date` that is a real calendar date: the month decides
|
|
247
|
+
* the days, and February 29 needs a leap year.
|
|
248
|
+
*/
|
|
249
|
+
const DATE_PATTERN_SOURCE = String.raw`(?:(?:\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\d|30)|(?:02)-(?:0[1-9]|1\d|2[0-8])))`;
|
|
250
|
+
/** The source of a UTC offset: `Z`, or a signed `hh:mm`. */
|
|
251
|
+
const OFFSET_PATTERN_SOURCE = String.raw`(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)`;
|
|
252
|
+
/** The keywords that only constrain a number: on their own they imply `type: number`. */
|
|
253
|
+
const NUMERIC_KEYWORDS = [
|
|
254
|
+
"minimum",
|
|
255
|
+
"maximum",
|
|
256
|
+
"exclusiveMinimum",
|
|
257
|
+
"exclusiveMaximum",
|
|
258
|
+
"multipleOf"
|
|
259
|
+
];
|
|
260
|
+
/** The keywords that only constrain a string: on their own they imply `type: string`. */
|
|
261
|
+
const STRING_KEYWORDS = [
|
|
262
|
+
"minLength",
|
|
263
|
+
"maxLength",
|
|
264
|
+
"pattern"
|
|
265
|
+
];
|
|
266
|
+
/**
|
|
267
|
+
* The keywords that hold a document's named definitions: `$defs` since Draft 2019-09, and
|
|
268
|
+
* `definitions` before it. A document may use both, and the generators read them as one map.
|
|
269
|
+
*/
|
|
270
|
+
const DEFINITION_KEYWORDS = ["definitions", "$defs"];
|
|
271
|
+
/** The `$ref` spellings of the whole document: the schema being emitted refers to itself. */
|
|
272
|
+
const ROOT_REFS = ["#", ""];
|
|
273
|
+
/** The `$ref` prefixes under which a named definition is found. */
|
|
274
|
+
const DEFINITION_REF_PREFIXES = [
|
|
275
|
+
"#/components/schemas/",
|
|
276
|
+
"#/definitions/",
|
|
277
|
+
"#/$defs/"
|
|
278
|
+
];
|
|
279
|
+
/** The `$ref` prefix of an OpenAPI component schema. */
|
|
280
|
+
const COMPONENT_SCHEMA_REF_PREFIX = "#/components/schemas/";
|
|
281
|
+
/**
|
|
282
|
+
* The variable-name suffix of each OpenAPI component kind, by its `$ref` prefix.
|
|
283
|
+
*
|
|
284
|
+
* @see https://swagger.io/docs/specification/v3_0/components/
|
|
285
|
+
*/
|
|
286
|
+
const OPENAPI_COMPONENT_SUFFIX_MAP = [
|
|
287
|
+
{
|
|
288
|
+
prefix: "#/components/schemas/",
|
|
289
|
+
suffix: "Schema"
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
prefix: "#/components/parameters/",
|
|
293
|
+
suffix: "ParamsSchema"
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
prefix: "#/components/headers/",
|
|
297
|
+
suffix: "HeaderSchema"
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
prefix: "#/components/securitySchemes/",
|
|
301
|
+
suffix: "SecurityScheme"
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
prefix: "#/components/requestBodies/",
|
|
305
|
+
suffix: "RequestBody"
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
prefix: "#/components/responses/",
|
|
309
|
+
suffix: "Response"
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
prefix: "#/components/examples/",
|
|
313
|
+
suffix: "Example"
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
prefix: "#/components/links/",
|
|
317
|
+
suffix: "Link"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
prefix: "#/components/callbacks/",
|
|
321
|
+
suffix: "Callback"
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
prefix: "#/components/pathItems/",
|
|
325
|
+
suffix: "PathItem"
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
prefix: "#/components/mediaTypes/",
|
|
329
|
+
suffix: "MediaTypeSchema"
|
|
330
|
+
}
|
|
331
|
+
];
|
|
332
|
+
/** The name a root schema is exported under when the document has no `title`. */
|
|
333
|
+
const DEFAULT_ROOT_NAME = "Schema";
|
|
334
|
+
/** The extensions of an input document: a JSON Schema as JSON or YAML. */
|
|
335
|
+
const INPUT_EXTENSIONS = [".json", ".yaml"];
|
|
336
|
+
/** The extension of the generated file. */
|
|
337
|
+
const OUTPUT_EXTENSION = ".ts";
|
|
338
|
+
/** A JavaScript identifier that can be written bare: as a binding, or a key or member access. */
|
|
339
|
+
const IDENTIFIER_PATTERN = /^[A-Za-z_$][A-Za-z0-9_$]*$/u;
|
|
340
|
+
/**
|
|
341
|
+
* A message option written as an arrow function, `(issue) => ...`, rather than as text.
|
|
342
|
+
* Such a message is spliced into the generated code as an expression instead of a string.
|
|
343
|
+
*/
|
|
344
|
+
const ARROW_FUNCTION_PATTERN = /^\s*\(.*?\)\s*=>/u;
|
|
345
|
+
/**
|
|
346
|
+
* A regular expression that needs the `u` flag: Unicode property escapes (`\p{...}`,
|
|
347
|
+
* `\P{...}`) and brace code points (`\u{...}`), which TypeScript rejects without it (TS1530).
|
|
348
|
+
*/
|
|
349
|
+
const UNICODE_ESCAPE_PATTERN = /\\[pP]\{|\\u\{/u;
|
|
350
|
+
/** A non-ASCII character, which an identifier cannot carry as is. */
|
|
351
|
+
const NON_IDENTIFIER_CHARACTER_PATTERN = /[^A-Za-z0-9_-]/u;
|
|
352
|
+
/** A character that breaks a string-DSL literal and the JS string around it. */
|
|
353
|
+
const QUOTE_OR_BACKSLASH_PATTERN = /['"\\]/u;
|
|
354
|
+
/** The code-emitting extensions each target honours, in the order they are chained. */
|
|
355
|
+
const ZOD_CODE_EXTENSION_KEYS = [
|
|
356
|
+
"x-refine",
|
|
357
|
+
"x-transform",
|
|
358
|
+
"x-pipe",
|
|
359
|
+
"x-codec",
|
|
360
|
+
"x-preprocess"
|
|
361
|
+
];
|
|
362
|
+
const VALIBOT_CODE_EXTENSION_KEYS = [
|
|
363
|
+
"x-check",
|
|
364
|
+
"x-transform",
|
|
365
|
+
"x-pipe"
|
|
366
|
+
];
|
|
367
|
+
const EFFECT_CODE_EXTENSION_KEYS = [
|
|
368
|
+
"x-filter",
|
|
369
|
+
"x-transform",
|
|
370
|
+
"x-pipe"
|
|
371
|
+
];
|
|
372
|
+
const ARKTYPE_CODE_EXTENSION_KEYS = [
|
|
373
|
+
"x-narrow",
|
|
374
|
+
"x-morph",
|
|
375
|
+
"x-pipe"
|
|
376
|
+
];
|
|
377
|
+
/** Every code-emitting extension any target honours, deduplicated and sorted. */
|
|
378
|
+
const ALL_CODE_EXTENSION_KEYS = [.../* @__PURE__ */ new Set([
|
|
379
|
+
...ZOD_CODE_EXTENSION_KEYS,
|
|
380
|
+
...VALIBOT_CODE_EXTENSION_KEYS,
|
|
381
|
+
...EFFECT_CODE_EXTENSION_KEYS,
|
|
382
|
+
...ARKTYPE_CODE_EXTENSION_KEYS
|
|
383
|
+
])].toSorted();
|
|
384
|
+
/** The first line of a file that spliced a caller-supplied code extension into its source. */
|
|
385
|
+
const UNSAFE_GENERATED_MARKER = "// @generated-with-unsafe-code-extensions";
|
|
386
|
+
/** The first line of a file whose generator dropped `if` / `then` / `else`. */
|
|
387
|
+
const IF_THEN_ELSE_UNSUPPORTED_MARKER = "// FIXME: JSON Schema if/then/else is not yet supported by this generator; conditional validation is omitted";
|
|
388
|
+
/** The first line of a TypeBox file whose generator dropped a `not` keyword. */
|
|
389
|
+
const NOT_KEYWORD_UNSUPPORTED_MARKER = "// FIXME: JSON Schema `not` is not enforced by this generator; TypeBox v1 has no runtime `Type.Not` and `Value.Check` ignores the keyword (falls back to `Type.Any()`)";
|
|
390
|
+
/** The comment left where a `$ref` names a definition the document does not hold. */
|
|
391
|
+
const MISSING_DEFINITION_PREFIX = "// ⚠️ missing definition for ";
|
|
392
|
+
/** A JSON Pointer `$ref` to a named definition: `#/$defs/Name` or `#/definitions/Name`. */
|
|
393
|
+
const DEFINITION_POINTER_PATTERN = /^#\/(?:definitions|\$defs)\/(?<name>[^/]+)$/u;
|
|
394
|
+
/** The Draft-04 anchor shorthand for a named definition: `#Name`. */
|
|
395
|
+
const ANCHOR_REF_PATTERN = /^#(?<name>[^/]+)$/u;
|
|
396
|
+
/**
|
|
397
|
+
* The ISO 4217 alphabetic codes in current use, funds and precious-metal codes included:
|
|
398
|
+
* what `format: currencyCode` accepts.
|
|
399
|
+
*
|
|
400
|
+
* @see https://www.iso.org/iso-4217-currency-codes.html
|
|
401
|
+
*/
|
|
402
|
+
const ISO_4217_CURRENCY_CODES = [
|
|
403
|
+
"AED",
|
|
404
|
+
"AFN",
|
|
405
|
+
"ALL",
|
|
406
|
+
"AMD",
|
|
407
|
+
"ANG",
|
|
408
|
+
"AOA",
|
|
409
|
+
"ARS",
|
|
410
|
+
"AUD",
|
|
411
|
+
"AWG",
|
|
412
|
+
"AZN",
|
|
413
|
+
"BAM",
|
|
414
|
+
"BBD",
|
|
415
|
+
"BDT",
|
|
416
|
+
"BGN",
|
|
417
|
+
"BHD",
|
|
418
|
+
"BIF",
|
|
419
|
+
"BMD",
|
|
420
|
+
"BND",
|
|
421
|
+
"BOB",
|
|
422
|
+
"BOV",
|
|
423
|
+
"BRL",
|
|
424
|
+
"BSD",
|
|
425
|
+
"BTN",
|
|
426
|
+
"BWP",
|
|
427
|
+
"BYN",
|
|
428
|
+
"BZD",
|
|
429
|
+
"CAD",
|
|
430
|
+
"CDF",
|
|
431
|
+
"CHE",
|
|
432
|
+
"CHF",
|
|
433
|
+
"CHW",
|
|
434
|
+
"CLF",
|
|
435
|
+
"CLP",
|
|
436
|
+
"CNY",
|
|
437
|
+
"COP",
|
|
438
|
+
"COU",
|
|
439
|
+
"CRC",
|
|
440
|
+
"CUP",
|
|
441
|
+
"CVE",
|
|
442
|
+
"CZK",
|
|
443
|
+
"DJF",
|
|
444
|
+
"DKK",
|
|
445
|
+
"DOP",
|
|
446
|
+
"DZD",
|
|
447
|
+
"EGP",
|
|
448
|
+
"ERN",
|
|
449
|
+
"ETB",
|
|
450
|
+
"EUR",
|
|
451
|
+
"FJD",
|
|
452
|
+
"FKP",
|
|
453
|
+
"GBP",
|
|
454
|
+
"GEL",
|
|
455
|
+
"GHS",
|
|
456
|
+
"GIP",
|
|
457
|
+
"GMD",
|
|
458
|
+
"GNF",
|
|
459
|
+
"GTQ",
|
|
460
|
+
"GYD",
|
|
461
|
+
"HKD",
|
|
462
|
+
"HNL",
|
|
463
|
+
"HTG",
|
|
464
|
+
"HUF",
|
|
465
|
+
"IDR",
|
|
466
|
+
"ILS",
|
|
467
|
+
"INR",
|
|
468
|
+
"IQD",
|
|
469
|
+
"IRR",
|
|
470
|
+
"ISK",
|
|
471
|
+
"JMD",
|
|
472
|
+
"JOD",
|
|
473
|
+
"JPY",
|
|
474
|
+
"KES",
|
|
475
|
+
"KGS",
|
|
476
|
+
"KHR",
|
|
477
|
+
"KMF",
|
|
478
|
+
"KPW",
|
|
479
|
+
"KRW",
|
|
480
|
+
"KWD",
|
|
481
|
+
"KYD",
|
|
482
|
+
"KZT",
|
|
483
|
+
"LAK",
|
|
484
|
+
"LBP",
|
|
485
|
+
"LKR",
|
|
486
|
+
"LRD",
|
|
487
|
+
"LSL",
|
|
488
|
+
"LYD",
|
|
489
|
+
"MAD",
|
|
490
|
+
"MDL",
|
|
491
|
+
"MGA",
|
|
492
|
+
"MKD",
|
|
493
|
+
"MMK",
|
|
494
|
+
"MNT",
|
|
495
|
+
"MOP",
|
|
496
|
+
"MRU",
|
|
497
|
+
"MUR",
|
|
498
|
+
"MVR",
|
|
499
|
+
"MWK",
|
|
500
|
+
"MXN",
|
|
501
|
+
"MXV",
|
|
502
|
+
"MYR",
|
|
503
|
+
"MZN",
|
|
504
|
+
"NAD",
|
|
505
|
+
"NGN",
|
|
506
|
+
"NIO",
|
|
507
|
+
"NOK",
|
|
508
|
+
"NPR",
|
|
509
|
+
"NZD",
|
|
510
|
+
"OMR",
|
|
511
|
+
"PAB",
|
|
512
|
+
"PEN",
|
|
513
|
+
"PGK",
|
|
514
|
+
"PHP",
|
|
515
|
+
"PKR",
|
|
516
|
+
"PLN",
|
|
517
|
+
"PYG",
|
|
518
|
+
"QAR",
|
|
519
|
+
"RON",
|
|
520
|
+
"RSD",
|
|
521
|
+
"RUB",
|
|
522
|
+
"RWF",
|
|
523
|
+
"SAR",
|
|
524
|
+
"SBD",
|
|
525
|
+
"SCR",
|
|
526
|
+
"SDG",
|
|
527
|
+
"SEK",
|
|
528
|
+
"SGD",
|
|
529
|
+
"SHP",
|
|
530
|
+
"SLE",
|
|
531
|
+
"SOS",
|
|
532
|
+
"SRD",
|
|
533
|
+
"SSP",
|
|
534
|
+
"STN",
|
|
535
|
+
"SVC",
|
|
536
|
+
"SYP",
|
|
537
|
+
"SZL",
|
|
538
|
+
"THB",
|
|
539
|
+
"TJS",
|
|
540
|
+
"TMT",
|
|
541
|
+
"TND",
|
|
542
|
+
"TOP",
|
|
543
|
+
"TRY",
|
|
544
|
+
"TTD",
|
|
545
|
+
"TWD",
|
|
546
|
+
"TZS",
|
|
547
|
+
"UAH",
|
|
548
|
+
"UGX",
|
|
549
|
+
"USD",
|
|
550
|
+
"USN",
|
|
551
|
+
"UYI",
|
|
552
|
+
"UYU",
|
|
553
|
+
"UYW",
|
|
554
|
+
"UZS",
|
|
555
|
+
"VED",
|
|
556
|
+
"VES",
|
|
557
|
+
"VND",
|
|
558
|
+
"VUV",
|
|
559
|
+
"WST",
|
|
560
|
+
"XAF",
|
|
561
|
+
"XAG",
|
|
562
|
+
"XAU",
|
|
563
|
+
"XBA",
|
|
564
|
+
"XBB",
|
|
565
|
+
"XBC",
|
|
566
|
+
"XBD",
|
|
567
|
+
"XCD",
|
|
568
|
+
"XCG",
|
|
569
|
+
"XDR",
|
|
570
|
+
"XOF",
|
|
571
|
+
"XPD",
|
|
572
|
+
"XPF",
|
|
573
|
+
"XPT",
|
|
574
|
+
"XSU",
|
|
575
|
+
"XTS",
|
|
576
|
+
"XUA",
|
|
577
|
+
"XXX",
|
|
578
|
+
"YER",
|
|
579
|
+
"ZAR",
|
|
580
|
+
"ZMW",
|
|
581
|
+
"ZWG"
|
|
582
|
+
];
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region src/guard/index.ts
|
|
585
|
+
/**
|
|
586
|
+
* Whether a value is a non-null object — the shape every schema walk descends into.
|
|
587
|
+
*
|
|
588
|
+
* `typeof null === 'object'` is the one case worth spelling out; arrays pass, which is
|
|
589
|
+
* what the walkers want, since a combinator list is an array of schemas.
|
|
590
|
+
*/
|
|
591
|
+
function isRecord(value) {
|
|
592
|
+
return typeof value === "object" && value !== null;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* A non-array object. Distinct from {@link isRecord}: an enum member that is an array
|
|
596
|
+
* needs a tuple matcher, and one that is a plain object needs an object matcher, so the
|
|
597
|
+
* two are told apart here rather than lumped together.
|
|
598
|
+
*/
|
|
599
|
+
function isPlainObject(value) {
|
|
600
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
601
|
+
}
|
|
602
|
+
/** A JSON scalar: what a `literal` matcher can take as is. */
|
|
603
|
+
function isPrimitive(value) {
|
|
604
|
+
return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
605
|
+
}
|
|
606
|
+
/** An array or object value: one that needs a structural matcher rather than a literal. */
|
|
607
|
+
function isComposite(value) {
|
|
608
|
+
return typeof value === "object" && value !== null;
|
|
609
|
+
}
|
|
610
|
+
/** Whether a key can be written bare in an object literal or as a member access. */
|
|
611
|
+
function isValidIdentifier(name) {
|
|
612
|
+
return IDENTIFIER_PATTERN.test(name);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Whether a configured message is source text for an arrow function rather than a
|
|
616
|
+
* literal sentence.
|
|
617
|
+
*
|
|
618
|
+
* The `x-*-message` extensions accept either, and a library that takes a callback
|
|
619
|
+
* (Zod's `error`, Valibot's message argument) gets the source through unquoted.
|
|
620
|
+
*/
|
|
621
|
+
function isArrowExpression(source) {
|
|
622
|
+
return ARROW_FUNCTION_PATTERN.test(source);
|
|
623
|
+
}
|
|
624
|
+
/** Whether a key holds a document's named definitions (`$defs` or `definitions`). */
|
|
625
|
+
function isDefinitionKeyword(key) {
|
|
626
|
+
return DEFINITION_KEYWORDS.some((keyword) => keyword === key);
|
|
627
|
+
}
|
|
628
|
+
/** Whether a parameter location delivers its values as strings that have to be coerced. */
|
|
629
|
+
function isStringWireLocation(paramIn) {
|
|
630
|
+
return STRING_WIRE_LOCATIONS.some((location) => location === paramIn);
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Whether a property name is one every plain object inherits (`constructor`, `toString`,
|
|
634
|
+
* ...), so reading it off a parameter object that never carried it finds a function.
|
|
635
|
+
*/
|
|
636
|
+
function isObjectPrototypeProperty(name) {
|
|
637
|
+
return OBJECT_PROTOTYPE_PROPERTIES.some((property) => property === name);
|
|
638
|
+
}
|
|
639
|
+
/** Whether a `$ref` names the whole document — the schema being emitted refers to itself. */
|
|
640
|
+
function isRootRef(ref) {
|
|
641
|
+
return ROOT_REFS.some((root) => root === ref);
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* True for a local JSON Pointer `$ref` that points *into* a component
|
|
645
|
+
* sub-schema (e.g. `#/components/schemas/X/properties/Y`) rather than at a
|
|
646
|
+
* top-level component (`#/components/schemas/X`). Such pointers have no
|
|
647
|
+
* generated declaration, so generators emit an `unknown` schema for them
|
|
648
|
+
* instead of referencing an undeclared identifier.
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* isDeepLocalPointer('#/components/schemas/User') // false
|
|
653
|
+
* isDeepLocalPointer('#/components/schemas/User/properties/x') // true
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
function isDeepLocalPointer($ref) {
|
|
657
|
+
if (!$ref.startsWith("#/")) return false;
|
|
658
|
+
const fragment = $ref.slice(2);
|
|
659
|
+
const matched = [
|
|
660
|
+
"components/schemas/",
|
|
661
|
+
"definitions/",
|
|
662
|
+
"$defs/"
|
|
663
|
+
].find((c) => fragment.startsWith(c));
|
|
664
|
+
return matched ? fragment.slice(matched.length).includes("/") : false;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* JSON Schema 2020-12 §4.3.2 / §10.3.1.2: `items`, `additionalProperties`,
|
|
668
|
+
* `unevaluatedItems` and `unevaluatedProperties` may be an object schema or a boolean
|
|
669
|
+
* schema (`true` = pass-through, `false` = reject), and `items` may also be a Draft-04
|
|
670
|
+
* tuple. This narrows the union to the single object form so callers can recurse.
|
|
671
|
+
*/
|
|
672
|
+
function isSchemaObject(value) {
|
|
673
|
+
return typeof value === "object" && !Array.isArray(value);
|
|
674
|
+
}
|
|
675
|
+
/** Draft-04's tuple form of `items`: a list of schemas rather than a single one. */
|
|
676
|
+
function isSchemaList(value) {
|
|
677
|
+
return Array.isArray(value);
|
|
678
|
+
}
|
|
679
|
+
/** Whether a node admits `null`: OpenAPI 3.0 `nullable`, or `null` among its types. */
|
|
680
|
+
function isNullable(schema) {
|
|
681
|
+
return schema.nullable === true || hasType(schema, "null");
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Whether a node takes `null`: a nullable type, a `null` among its `enum`, a `const: null`
|
|
685
|
+
* or a `oneOf` / `anyOf` member that does.
|
|
686
|
+
*/
|
|
687
|
+
function canBeNull(schema) {
|
|
688
|
+
if (isNullable(schema)) return true;
|
|
689
|
+
if (schema.const === null) return true;
|
|
690
|
+
if (schema.enum?.some((value) => value === null)) return true;
|
|
691
|
+
return [...schema.oneOf ?? [], ...schema.anyOf ?? []].some(canBeNull);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Whether a node may take a string: a `string` type, a string among its `enum` or `const`,
|
|
695
|
+
* a `oneOf` / `anyOf` member that does, or no constraint on the type at all.
|
|
696
|
+
*/
|
|
697
|
+
function canBeString(schema) {
|
|
698
|
+
if (hasType(schema, "string")) return true;
|
|
699
|
+
if (typeof schema.const === "string") return true;
|
|
700
|
+
if (schema.enum !== void 0) return schema.enum.some((value) => typeof value === "string");
|
|
701
|
+
const members = [...schema.oneOf ?? [], ...schema.anyOf ?? []];
|
|
702
|
+
if (members.length > 0) return members.some(canBeString);
|
|
703
|
+
return schema.type === void 0 && schema.const === void 0 && schema.$ref === void 0 && schema.allOf === void 0 && schema.not === void 0 && schema.properties === void 0;
|
|
704
|
+
}
|
|
705
|
+
/** Whether a node without a `type` carries a keyword that only applies to a number. */
|
|
706
|
+
function hasNumericKeyword(schema) {
|
|
707
|
+
return NUMERIC_KEYWORDS.some((keyword) => schema[keyword] !== void 0);
|
|
708
|
+
}
|
|
709
|
+
/** Whether a node without a `type` carries a keyword that only applies to a string. */
|
|
710
|
+
function hasStringKeyword(schema) {
|
|
711
|
+
return STRING_KEYWORDS.some((keyword) => schema[keyword] !== void 0);
|
|
712
|
+
}
|
|
713
|
+
/** Whether `type` names `type`, either alone or as one of a list. */
|
|
714
|
+
function hasType(schema, type) {
|
|
715
|
+
const types = schema.type;
|
|
716
|
+
if (types === void 0) return false;
|
|
717
|
+
if (typeof types === "string") return types === type;
|
|
718
|
+
return types.some((candidate) => candidate === type);
|
|
719
|
+
}
|
|
720
|
+
/** Whether a node is composed of other schemas: `oneOf`, `anyOf`, `allOf` or `not`. */
|
|
721
|
+
function hasCombinator(schema) {
|
|
722
|
+
return schema.oneOf !== void 0 || schema.anyOf !== void 0 || schema.allOf !== void 0 || schema.not !== void 0;
|
|
723
|
+
}
|
|
724
|
+
/** Whether a node carries a conditional (`if` / `then` / `else`). */
|
|
725
|
+
function hasConditional(schema) {
|
|
726
|
+
return schema.if !== void 0 || schema.then !== void 0 || schema.else !== void 0;
|
|
727
|
+
}
|
|
728
|
+
/** Whether `minLength` and `maxLength` pin a string to exactly one length. */
|
|
729
|
+
function hasFixedLength(schema) {
|
|
730
|
+
return schema.minLength !== void 0 && schema.maxLength !== void 0 && schema.minLength === schema.maxLength;
|
|
731
|
+
}
|
|
732
|
+
/** Whether `minItems` and `maxItems` pin an array to exactly one length. */
|
|
733
|
+
function hasFixedItemCount(schema) {
|
|
734
|
+
return typeof schema.minItems === "number" && typeof schema.maxItems === "number" && schema.minItems === schema.maxItems;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* An `allOf` member that only says "and it may be null".
|
|
738
|
+
*
|
|
739
|
+
* OpenAPI 3.0 spells a nullable composition as `allOf: [{...}, { nullable: true }]`, and
|
|
740
|
+
* a bare `{ type: 'null' }` is the JSON Schema equivalent. Neither contributes a shape,
|
|
741
|
+
* so every generator lifts it out of the intersection and onto the result instead.
|
|
742
|
+
*/
|
|
743
|
+
function isNullTypeMember(schema) {
|
|
744
|
+
return schema.type === "null" || schema.nullable === true && Object.keys(schema).length === 1;
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* An `allOf` member that only carries a `default`.
|
|
748
|
+
*
|
|
749
|
+
* A default is metadata on the composed value rather than a constraint of its own, so it
|
|
750
|
+
* is applied to the intersection rather than intersected into it.
|
|
751
|
+
*/
|
|
752
|
+
function isDefaultOnlyMember(schema) {
|
|
753
|
+
return Object.keys(schema).length === 1 && schema.default !== void 0;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* An `allOf` member that only carries a `const`.
|
|
757
|
+
*
|
|
758
|
+
* The other members already describe the shape; a lone `const` would narrow it to a
|
|
759
|
+
* single value in a way none of the target libraries express as an intersection, so it is
|
|
760
|
+
* dropped rather than emitted.
|
|
761
|
+
*/
|
|
762
|
+
function isConstOnlyMember(schema) {
|
|
763
|
+
return Object.keys(schema).length === 1 && schema.const !== void 0;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* True for an `allOf` member that carries no shape of its own — the ones every generator
|
|
767
|
+
* filters out before intersecting what is left.
|
|
768
|
+
*/
|
|
769
|
+
function isShapelessMember(schema) {
|
|
770
|
+
return isNullTypeMember(schema) || isDefaultOnlyMember(schema) || isConstOnlyMember(schema);
|
|
771
|
+
}
|
|
772
|
+
/** A double-quoted string literal: the ArkType string DSL form of a definition. */
|
|
773
|
+
function isQuoted(source) {
|
|
774
|
+
return source.startsWith("\"") && source.endsWith("\"");
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* A bare ArkType definition — a string, object or tuple literal — as opposed to
|
|
778
|
+
* an expression that already evaluates to a `Type` and so carries methods.
|
|
779
|
+
*/
|
|
780
|
+
function isArktypeDefinition(source) {
|
|
781
|
+
return isQuoted(source) || source.startsWith("{") && source.endsWith("}") || source.startsWith("[") && source.endsWith("]");
|
|
782
|
+
}
|
|
783
|
+
//#endregion
|
|
784
|
+
//#region src/utils/index.ts
|
|
785
|
+
/**
|
|
786
|
+
* Convert string to PascalCase (first character uppercase).
|
|
787
|
+
*
|
|
788
|
+
* When the input contains characters that are not valid in a TypeScript
|
|
789
|
+
* identifier (whitespace, dots, slashes, etc.) the input is routed through
|
|
790
|
+
* {@link toIdentifierPascalCase} so the generated variable name remains
|
|
791
|
+
* parseable. Hyphens and underscores are preserved for back-compat with
|
|
792
|
+
* upstream titles that already generated working code.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```ts
|
|
796
|
+
* toPascalCase('animal') // 'Animal'
|
|
797
|
+
* toPascalCase('userProfile') // 'UserProfile'
|
|
798
|
+
* toPascalCase('Self-Referencing Entities') // 'SelfReferencingEntities'
|
|
799
|
+
* ```
|
|
800
|
+
*/
|
|
801
|
+
function toPascalCase(name) {
|
|
802
|
+
if (NON_IDENTIFIER_CHARACTER_PATTERN.test(name)) return toIdentifierPascalCase(name);
|
|
803
|
+
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Encode non-ASCII characters as `u<hex codepoint>` so they survive identifier
|
|
807
|
+
* normalization instead of being stripped (which collapses every non-ASCII name
|
|
808
|
+
* to the same `Schema`, producing duplicate declarations). Pure-ASCII input is
|
|
809
|
+
* returned unchanged, so identifier generation stays byte-for-byte identical for
|
|
810
|
+
* the overwhelming majority of specs.
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```ts
|
|
814
|
+
* encodeNonAscii('User') // 'User'
|
|
815
|
+
* encodeNonAscii('日本語') // 'u65e5u672cu8a9e'
|
|
816
|
+
* ```
|
|
817
|
+
*/
|
|
818
|
+
function encodeNonAscii(name) {
|
|
819
|
+
return [...name].map((ch) => {
|
|
820
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
821
|
+
return cp > 127 ? `u${cp.toString(16)}` : ch;
|
|
822
|
+
}).join("");
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Convert string to a valid PascalCase identifier.
|
|
826
|
+
*
|
|
827
|
+
* Splits by non-alphanumeric characters and capitalizes each segment.
|
|
828
|
+
* Handles hyphens, underscores, dots, spaces, leading numbers, and non-ASCII characters.
|
|
829
|
+
*
|
|
830
|
+
* @example
|
|
831
|
+
* ```ts
|
|
832
|
+
* toIdentifierPascalCase('user-name') // 'UserName'
|
|
833
|
+
* toIdentifierPascalCase('hello_world') // 'HelloWorld'
|
|
834
|
+
* toIdentifierPascalCase('123value') // '_123Value'
|
|
835
|
+
* toIdentifierPascalCase('user') // 'User'
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
function toIdentifierPascalCase(name) {
|
|
839
|
+
const parts = encodeNonAscii(name).split(/[^a-zA-Z0-9]+/u).filter(Boolean);
|
|
840
|
+
if (parts.length === 0) return DEFAULT_ROOT_NAME;
|
|
841
|
+
const result = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
842
|
+
if (/^[0-9]/u.test(result)) return `_${result}`.replace(/([0-9])([a-z])/u, (_, d, c) => `${d}${c.toUpperCase()}`);
|
|
843
|
+
return result;
|
|
844
|
+
}
|
|
845
|
+
function normalizeTypes(t) {
|
|
846
|
+
if (t === void 0) return [];
|
|
847
|
+
if (typeof t === "string") return [t];
|
|
848
|
+
return [...t];
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Reconcile a schema's `default` value with the type the schema actually emits
|
|
852
|
+
* so the generated `.default(...)` stays assignable.
|
|
853
|
+
*
|
|
854
|
+
* - `null` on a non-nullable schema is unusable as a default → `keep: false`
|
|
855
|
+
* (the caller omits `.default`).
|
|
856
|
+
* - a string `'true'`/`'false'` on a boolean schema denotes the boolean it
|
|
857
|
+
* spells → coerced to the boolean value (covers `z.stringbool()` query coercion).
|
|
858
|
+
* - everything else is returned unchanged (byte-for-byte identical output).
|
|
859
|
+
*
|
|
860
|
+
* The mismatch check is deliberately shallow and only crosses the
|
|
861
|
+
* composite (array/object) ↔ scalar category boundary; scalar↔scalar
|
|
862
|
+
* mismatches and schemas without an explicit `type` (`oneOf`/`$ref` only)
|
|
863
|
+
* pass through untouched — JSON Schema 2020-12 §9.9.4.1 treats `default` as
|
|
864
|
+
* an annotation with no validity guarantee, so anything finer would need a
|
|
865
|
+
* full validator.
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* ```ts
|
|
869
|
+
* coerceDefault({ type: 'boolean' }, 'true') // { keep: true, value: true }
|
|
870
|
+
* coerceDefault({ type: 'object' }, null) // { keep: false, value: null }
|
|
871
|
+
* coerceDefault({ type: 'object', nullable: true }, null) // { keep: true, value: null }
|
|
872
|
+
* coerceDefault({ type: 'array' }, 'eval') // { keep: false, value: 'eval' }
|
|
873
|
+
* ```
|
|
874
|
+
*/
|
|
875
|
+
function coerceDefault(schema, value) {
|
|
876
|
+
const types = Array.isArray(schema.type) ? schema.type : schema.type !== void 0 ? [schema.type] : [];
|
|
877
|
+
const isNullable = schema.nullable === true || types.includes("null");
|
|
878
|
+
if (value === null && !isNullable) return {
|
|
879
|
+
keep: false,
|
|
880
|
+
value
|
|
881
|
+
};
|
|
882
|
+
if (types.includes("boolean") && (value === "true" || value === "false")) return {
|
|
883
|
+
keep: true,
|
|
884
|
+
value: value === "true"
|
|
885
|
+
};
|
|
886
|
+
const isComposite = Array.isArray(value) || typeof value === "object" && value !== null;
|
|
887
|
+
const allowsComposite = types.length === 0 || types.includes("array") || types.includes("object");
|
|
888
|
+
if (isComposite && !allowsComposite) return {
|
|
889
|
+
keep: false,
|
|
890
|
+
value
|
|
891
|
+
};
|
|
892
|
+
const allowsScalar = types.length === 0 || types.some((t) => t !== "array" && t !== "object" && t !== "null");
|
|
893
|
+
if (!isComposite && value !== null && !allowsScalar) return {
|
|
894
|
+
keep: false,
|
|
895
|
+
value
|
|
896
|
+
};
|
|
897
|
+
return {
|
|
898
|
+
keep: true,
|
|
899
|
+
value
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* A message as the expression an emitted check evaluates for each issue: an arrow source
|
|
904
|
+
* is applied to the issue, a sentence becomes a string literal.
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```ts
|
|
908
|
+
* issueMessageExpression('Invalid') // '"Invalid"'
|
|
909
|
+
* issueMessageExpression('(issue) => issue.path[0]') // '((issue) => issue.path[0])(issue)'
|
|
910
|
+
* ```
|
|
911
|
+
*/
|
|
912
|
+
function issueMessageExpression(message) {
|
|
913
|
+
return isArrowExpression(message) ? `(${message})(issue)` : JSON.stringify(message);
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Format an error message argument using the Zod v4 unified `error` parameter.
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* ```ts
|
|
920
|
+
* zodError('Name must be 3-20 characters') // '{error:"Name must be 3-20 characters"}'
|
|
921
|
+
* zodError('(val) => `Expected ${val}`') // '{error:(val) => `Expected ${val}`}'
|
|
922
|
+
* ```
|
|
923
|
+
*/
|
|
924
|
+
function zodError(message) {
|
|
925
|
+
return isArrowExpression(message) ? `{error:${message}}` : `{error:${JSON.stringify(message)}}`;
|
|
926
|
+
}
|
|
927
|
+
/** `zodError(message)` as a trailing argument, or nothing when there is no message. */
|
|
928
|
+
function zodErrorPart(message) {
|
|
929
|
+
return message ? `,${zodError(message)}` : "";
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Build the base `{error:...}` argument for a Zod v4 schema constructor when
|
|
933
|
+
* `x-error-message` (type / generic) and/or `x-required-message`
|
|
934
|
+
* (`issue.input === undefined`) are present.
|
|
935
|
+
*/
|
|
936
|
+
function zodBaseError(typeMessage, requiredMessage) {
|
|
937
|
+
if (typeMessage === void 0 && requiredMessage === void 0) return "";
|
|
938
|
+
if (requiredMessage === void 0 && typeMessage !== void 0) return zodError(typeMessage);
|
|
939
|
+
if (requiredMessage !== void 0 && typeMessage === void 0) return `{error:(issue)=>issue.input===undefined?${JSON.stringify(requiredMessage)}:undefined}`;
|
|
940
|
+
return `{error:(issue)=>issue.input===undefined?${JSON.stringify(requiredMessage)}:${JSON.stringify(typeMessage)}}`;
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Format an error message argument for Valibot.
|
|
944
|
+
*
|
|
945
|
+
* @example
|
|
946
|
+
* ```ts
|
|
947
|
+
* valibotError('Must be valid') // '"Must be valid"'
|
|
948
|
+
* valibotError('(issue) => issue.input') // '(issue) => issue.input'
|
|
949
|
+
* ```
|
|
950
|
+
*/
|
|
951
|
+
function valibotError(message) {
|
|
952
|
+
return isArrowExpression(message) ? message : JSON.stringify(message);
|
|
953
|
+
}
|
|
954
|
+
/** `valibotError(message)` as a trailing argument, or nothing when there is no message. */
|
|
955
|
+
function valibotErrorPart(message) {
|
|
956
|
+
return message ? `,${valibotError(message)}` : "";
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Format an error message annotation for Effect Schema.
|
|
960
|
+
*
|
|
961
|
+
* Effect Schema v4 types the `message` annotation as a plain `string` (v3 took
|
|
962
|
+
* a `() => string` thunk), so an arrow-expression message has no place to go
|
|
963
|
+
* and is emitted as its source text — see {@link effectMessage}.
|
|
964
|
+
*
|
|
965
|
+
* @example
|
|
966
|
+
* ```ts
|
|
967
|
+
* effectError('Required') // '{message:"Required"}'
|
|
968
|
+
* ```
|
|
969
|
+
*/
|
|
970
|
+
function effectError(message) {
|
|
971
|
+
return `{message:${effectMessage(message)}}`;
|
|
972
|
+
}
|
|
973
|
+
/** `effectError(message)` as a trailing argument, or nothing when there is no message. */
|
|
974
|
+
function effectErrorPart(message) {
|
|
975
|
+
return message ? `,${effectError(message)}` : "";
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* The `message` annotation value for Effect Schema v4: a string literal.
|
|
979
|
+
*
|
|
980
|
+
* @example
|
|
981
|
+
* ```ts
|
|
982
|
+
* effectMessage('Required') // '"Required"'
|
|
983
|
+
* ```
|
|
984
|
+
*/
|
|
985
|
+
function effectMessage(message) {
|
|
986
|
+
return JSON.stringify(message);
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Resolve an OpenAPI $ref string to a variable name with the appropriate suffix
|
|
990
|
+
*
|
|
991
|
+
* @example
|
|
992
|
+
* ```ts
|
|
993
|
+
* resolveOpenAPIRef('#/components/schemas/User') // 'UserSchema'
|
|
994
|
+
* resolveOpenAPIRef('#/components/parameters/UserId') // 'UserIdParamsSchema'
|
|
995
|
+
* resolveOpenAPIRef('#/definitions/Address') // null
|
|
996
|
+
* ```
|
|
997
|
+
*/
|
|
998
|
+
function resolveOpenAPIRef($ref) {
|
|
999
|
+
for (const { prefix, suffix } of OPENAPI_COMPONENT_SUFFIX_MAP) if ($ref.startsWith(prefix)) return toIdentifierPascalCase(decodeURIComponent($ref.slice(prefix.length))) + suffix;
|
|
1000
|
+
return null;
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Encode a property key safely for object literal output.
|
|
1004
|
+
* Bare identifiers stay unquoted; everything else is JSON-encoded.
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```ts
|
|
1008
|
+
* makeSafeKey('foo') // 'foo'
|
|
1009
|
+
* makeSafeKey('foo-bar') // '"foo-bar"'
|
|
1010
|
+
* makeSafeKey('123') // '"123"'
|
|
1011
|
+
* ```
|
|
1012
|
+
*/
|
|
1013
|
+
function makeSafeKey(key) {
|
|
1014
|
+
return isValidIdentifier(key) ? key : JSON.stringify(key);
|
|
1015
|
+
}
|
|
1016
|
+
//#endregion
|
|
1017
|
+
export { DEFINITION_REF_PREFIXES as $, isDefinitionKeyword as A, WIRE_PRIMITIVE_ORDER as At, isSchemaObject as B, hasNumericKeyword as C, STRINGBOOL_TRUTHY_PATTERN as Ct, isComposite as D, UNICODE_ESCAPE_PATTERN as Dt, isArktypeDefinition as E, STRING_WIRE_LOCATIONS as Et, isPrimitive as F, ARKTYPE_CODE_EXTENSION_KEYS as G, isStringWireLocation as H, isQuoted as I, CONTENT_ENCODINGS as J, ARROW_FUNCTION_PATTERN as K, isRecord as L, isNullable as M, isObjectPrototypeProperty as N, isDeepLocalPointer as O, UNSAFE_GENERATED_MARKER as Ot, isPlainObject as P, DEFINITION_POINTER_PATTERN as Q, isRootRef as R, hasFixedLength as S, STRINGBOOL_PATTERN as St, hasType as T, STRING_KEYWORDS as Tt, ALL_CODE_EXTENSION_KEYS as U, isShapelessMember as V, ANCHOR_REF_PATTERN as W, DEFAULT_ROOT_NAME as X, DATE_PATTERN_SOURCE as Y, DEFINITION_KEYWORDS as Z, canBeNull as _, OUTPUT_EXTENSION as _t, encodeNonAscii as a, INTEGER_TEXT_PATTERN as at, hasConditional as b, QUOTE_OR_BACKSLASH_PATTERN as bt, normalizeTypes as c, MISSING_DEFINITION_PREFIX as ct, toPascalCase as d, NUMBER_FORMATS as dt, EFFECT_CODE_EXTENSION_KEYS as et, valibotError as f, NUMBER_RANGES as ft, zodErrorPart as g, OPENAPI_COMPONENT_SUFFIX_MAP as gt, zodError as h, OFFSET_PATTERN_SOURCE as ht, effectMessage as i, INPUT_EXTENSIONS as it, isNullTypeMember as j, ZOD_CODE_EXTENSION_KEYS as jt, isDefaultOnlyMember as k, VALIBOT_CODE_EXTENSION_KEYS as kt, resolveOpenAPIRef as l, NON_IDENTIFIER_CHARACTER_PATTERN as lt, zodBaseError as m, OBJECT_PROTOTYPE_PROPERTIES as mt, effectError as n, IDENTIFIER_PATTERN as nt, issueMessageExpression as o, ISO_4217_CURRENCY_CODES as ot, valibotErrorPart as p, NUMERIC_KEYWORDS as pt, COMPONENT_SCHEMA_REF_PREFIX as q, effectErrorPart as r, IF_THEN_ELSE_UNSUPPORTED_MARKER as rt, makeSafeKey as s, JSON_SCHEMA_TYPES as st, coerceDefault as t, FORMAT_PATTERNS as tt, toIdentifierPascalCase as u, NOT_KEYWORD_UNSUPPORTED_MARKER as ut, canBeString as v, PARAM_LOCATIONS as vt, hasStringKeyword as w, STRING_FORMATS as wt, hasFixedItemCount as x, ROOT_REFS as xt, hasCombinator as y, PARAM_STYLES as yt, isSchemaList as z };
|