schema-truth 0.7.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ajv-DTP2FXRr.js → ajv-DS9gtNZC.js} +1 -1
- package/dist/ajv.d.ts +1 -1
- package/dist/ajv.js +1 -1
- package/dist/{arktype-BdvvaBQX.js → arktype-CBV4bg-0.js} +179 -75
- package/dist/arktype.d.ts +3 -3
- package/dist/arktype.js +1 -1
- package/dist/cli/ajv.js +2 -2
- package/dist/cli/arktype.js +2 -2
- package/dist/cli/effect.js +2 -2
- package/dist/cli/typebox.js +2 -2
- package/dist/cli/valibot.js +2 -2
- package/dist/cli/yup.js +2 -2
- package/dist/cli/zod.js +2 -2
- package/dist/{cli-CL2mSark.js → cli-CB9GJ7qa.js} +2 -2
- package/dist/{effect-mUknRPv_.js → effect-D9pvwi_6.js} +91 -31
- package/dist/effect.d.ts +3 -3
- package/dist/effect.js +1 -1
- package/dist/{helper-Cfvb5UDq.js → helper-S7SSsYwh.js} +777 -40
- package/dist/{index-C2L8Ty0-.d.ts → index-EK43HM_z.d.ts} +29 -25
- package/dist/{index-DbV3dEmN.d.ts → index-G7UqNQ4w.d.ts} +54 -2
- package/dist/index.d.ts +63 -25
- package/dist/index.js +9 -9
- package/dist/{typebox-BdWJv3ND.js → typebox-DGGKJLdP.js} +82 -42
- package/dist/typebox.d.ts +3 -3
- package/dist/typebox.js +1 -1
- package/dist/{utils-WNBq1HR0.js → utils-Bky98UmP.js} +160 -31
- package/dist/{valibot-CaJIynCH.js → valibot-B6y4VQFF.js} +107 -36
- package/dist/valibot.d.ts +3 -3
- package/dist/valibot.js +1 -1
- package/dist/{yup-CM2TSFig.js → yup-DuqQwHUv.js} +2 -2
- package/dist/yup.d.ts +2 -2
- package/dist/yup.js +1 -1
- package/dist/{zod-BsXLZfwK.js → zod-CXo9NFuF.js} +192 -115
- package/dist/zod.d.ts +3 -3
- package/dist/zod.js +1 -1
- package/package.json +1 -1
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Every location is string-wire — when a primitive schema is emitted under one of
|
|
8
|
-
* them, generators apply type coercion (e.g. Zod `z.coerce.number()`, Effect
|
|
9
|
-
* `Schema.FiniteFromString`), read a literal, an `enum` and the text `null` from the
|
|
10
|
-
* text, wrap a value sent once into an array of one, and copy an object with an
|
|
11
|
-
* `Object.prototype`-named property onto a null-prototype object before the check.
|
|
12
|
-
*
|
|
13
|
-
* @see https://spec.openapis.org/oas/v3.2.0.html#parameter-object
|
|
14
|
-
*/
|
|
15
|
-
type ParamIn = (typeof PARAM_LOCATIONS)[number];
|
|
16
|
-
/**
|
|
17
|
-
* The `style` of an OpenAPI Parameter Object: how its text is serialised. Defaults to
|
|
18
|
-
* `form` for `query` / `cookie` / `formData` and `simple` for `path` / `header`.
|
|
19
|
-
*
|
|
20
|
-
* @see https://spec.openapis.org/oas/v3.2.0.html#style-values
|
|
21
|
-
*/
|
|
22
|
-
type ParamStyle = (typeof PARAM_STYLES)[number];
|
|
1
|
+
import { h as ParamStyle, m as ParamIn, r as JSONSchema } from "./index-G7UqNQ4w.js";
|
|
2
|
+
//#region src/helper/schema.d.ts
|
|
3
|
+
/** Named schemas a `default` beside a `$ref` is reconciled against. */
|
|
4
|
+
type SchemaDefinitions = {
|
|
5
|
+
readonly [name: string]: JSONSchema;
|
|
6
|
+
};
|
|
23
7
|
//#endregion
|
|
24
8
|
//#region src/options/index.d.ts
|
|
25
9
|
/** The options every `schemaTo<Library>` document generator accepts. */
|
|
@@ -38,8 +22,9 @@ type GenerateOptions = {
|
|
|
38
22
|
* The parameter's `style`, which decides how its text is split into an array or an
|
|
39
23
|
* object (`form` with `explode: false` reads `1,2,3`, `deepObject` gathers `deep[key]`).
|
|
40
24
|
* Defaults to `form` for `query` / `cookie` / `formData` and `simple` for `path` / `header`.
|
|
41
|
-
* Applies to the root schema
|
|
42
|
-
*
|
|
25
|
+
* Applies to the root schema. A property's own serialization is `x-style` / `x-explode` /
|
|
26
|
+
* `x-allowEmptyValue`, or {@link GenerateOptions.properties}. Honoured by Zod, Valibot,
|
|
27
|
+
* Effect Schema and ArkType; a no-op for TypeBox, Ajv and Yup.
|
|
43
28
|
*/
|
|
44
29
|
readonly style?: ParamStyle;
|
|
45
30
|
/**
|
|
@@ -58,8 +43,27 @@ type GenerateOptions = {
|
|
|
58
43
|
* `style` is.
|
|
59
44
|
*/
|
|
60
45
|
readonly contentType?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Per-property serialization for an object schema emitted with `paramIn`: each named
|
|
48
|
+
* property is read with its own `style` / `explode` / `allowEmptyValue` instead of the
|
|
49
|
+
* root's. A keyword on the property (`x-style`, `x-explode`, `x-allowEmptyValue`) fills
|
|
50
|
+
* whatever this map leaves unset, and survives inside `$defs`. Honoured where `style` is.
|
|
51
|
+
*/
|
|
52
|
+
readonly properties?: {
|
|
53
|
+
readonly [name: string]: {
|
|
54
|
+
readonly style?: ParamStyle;
|
|
55
|
+
readonly explode?: boolean;
|
|
56
|
+
readonly allowEmptyValue?: boolean;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Named schemas used to reconcile a `default` written beside a `$ref`. `schemaTo*`
|
|
61
|
+
* fills this from the document's `$defs` / `definitions`. Honoured by Zod, Valibot,
|
|
62
|
+
* Effect Schema and TypeBox.
|
|
63
|
+
*/
|
|
64
|
+
readonly definitions?: SchemaDefinitions;
|
|
61
65
|
};
|
|
62
66
|
/** The options threaded through every node emitter: `GenerateOptions` without `exportType`. */
|
|
63
67
|
type EmitOptions = Omit<GenerateOptions, 'exportType'>;
|
|
64
68
|
//#endregion
|
|
65
|
-
export {
|
|
69
|
+
export { GenerateOptions as n, SchemaDefinitions as r, EmitOptions as t };
|
|
@@ -59,6 +59,17 @@ declare const STRINGBOOL_PATTERN: RegExp;
|
|
|
59
59
|
declare const STRINGBOOL_TRUTHY_PATTERN: RegExp;
|
|
60
60
|
/** The text of a 64-bit integer: a sign and digits, which `BigInt` reads losslessly. */
|
|
61
61
|
declare const INTEGER_TEXT_PATTERN: RegExp;
|
|
62
|
+
/**
|
|
63
|
+
* Integer text on the string wire. A sign and digits, with no leading zero, no exponent
|
|
64
|
+
* and no fraction: `Number` and `parseInt` both accept more than this (`""`, `" "`,
|
|
65
|
+
* `"0x10"`, `"1e3"`, `"1.9xyz"`).
|
|
66
|
+
*/
|
|
67
|
+
declare const INTEGER_WIRE_PATTERN: RegExp;
|
|
68
|
+
/**
|
|
69
|
+
* Number text on the string wire: the JSON number grammar (RFC 8259). An exponent is a
|
|
70
|
+
* number (`1e3`) and is not an integer. Blank, hex and trailing junk are not numbers.
|
|
71
|
+
*/
|
|
72
|
+
declare const NUMBER_WIRE_PATTERN: RegExp;
|
|
62
73
|
/** The range of each fixed-width numeric format, as the literals the generators emit. */
|
|
63
74
|
declare const NUMBER_RANGES: {
|
|
64
75
|
readonly [k: string]: {
|
|
@@ -268,6 +279,47 @@ type XExtMessages = {
|
|
|
268
279
|
readonly 'x-else-message'?: string;
|
|
269
280
|
};
|
|
270
281
|
//#endregion
|
|
282
|
+
//#region src/parser/x-ext/param.d.ts
|
|
283
|
+
/**
|
|
284
|
+
* OpenAPI parameter location, or `formData` for a field of a form body
|
|
285
|
+
* (`application/x-www-form-urlencoded`, `multipart/form-data`).
|
|
286
|
+
*
|
|
287
|
+
* Every location is string-wire — when a primitive schema is emitted under one of
|
|
288
|
+
* them, generators read a number from the JSON number grammar and an integer from
|
|
289
|
+
* digits with no leading zero before converting the text, read a literal, an `enum`
|
|
290
|
+
* and the text `null` from the text, wrap a value sent once into an array of one,
|
|
291
|
+
* and copy an object with an `Object.prototype`-named property onto a null-prototype
|
|
292
|
+
* object before the check. Boolean text is the same set for every target.
|
|
293
|
+
*
|
|
294
|
+
* @see https://spec.openapis.org/oas/v3.2.0.html#parameter-object
|
|
295
|
+
*/
|
|
296
|
+
type ParamIn = (typeof PARAM_LOCATIONS)[number];
|
|
297
|
+
/**
|
|
298
|
+
* Per-property OpenAPI serialization, for one property of an object schema emitted
|
|
299
|
+
* with `paramIn`. The root `style` / `explode` / `allowEmptyValue` options read the
|
|
300
|
+
* object itself; these keywords read that property's own text (`x-ids: 1,2,3` splits
|
|
301
|
+
* on commas before the elements are coerced). They survive inside `$defs`, unlike a
|
|
302
|
+
* generator option keyed by property name.
|
|
303
|
+
*
|
|
304
|
+
* Honoured by Zod, Valibot, Effect Schema and ArkType when the object is emitted with
|
|
305
|
+
* `paramIn`. A no-op for TypeBox, Ajv and Yup, and when `paramIn` is unset.
|
|
306
|
+
*/
|
|
307
|
+
type XExtParam = {
|
|
308
|
+
/** The property's `style`. Defaults to the location's style when unset. */
|
|
309
|
+
readonly 'x-style'?: ParamStyle;
|
|
310
|
+
/** The property's `explode`. Defaults to `true` for `style: form`, otherwise `false`. */
|
|
311
|
+
readonly 'x-explode'?: boolean;
|
|
312
|
+
/** The property's `allowEmptyValue`: an empty value reads as an absent one. */
|
|
313
|
+
readonly 'x-allowEmptyValue'?: boolean;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* The `style` of an OpenAPI Parameter Object: how its text is serialised. Defaults to
|
|
317
|
+
* `form` for `query` / `cookie` / `formData` and `simple` for `path` / `header`.
|
|
318
|
+
*
|
|
319
|
+
* @see https://spec.openapis.org/oas/v3.2.0.html#style-values
|
|
320
|
+
*/
|
|
321
|
+
type ParamStyle = (typeof PARAM_STYLES)[number];
|
|
322
|
+
//#endregion
|
|
271
323
|
//#region src/parser/x-ext/transform.d.ts
|
|
272
324
|
/**
|
|
273
325
|
* B 層: 振る舞い・transform 系 vendor extensions.
|
|
@@ -546,6 +598,6 @@ type JSONSchemaCore = {
|
|
|
546
598
|
/** Schema name (non-standard) */
|
|
547
599
|
readonly name?: string;
|
|
548
600
|
};
|
|
549
|
-
type JSONSchema = JSONSchemaCore & XExtMessages & XExtTransform & XExtCode;
|
|
601
|
+
type JSONSchema = JSONSchemaCore & XExtMessages & XExtTransform & XExtCode & XExtParam;
|
|
550
602
|
//#endregion
|
|
551
|
-
export {
|
|
603
|
+
export { QUOTE_OR_BACKSLASH_PATTERN as $, DEFINITION_REF_PREFIXES as A, MISSING_DEFINITION_PREFIX as B, ARROW_FUNCTION_PATTERN as C, DEFAULT_ROOT_NAME as D, DATE_PATTERN_SOURCE as E, INPUT_EXTENSIONS as F, NUMBER_WIRE_PATTERN as G, NOT_KEYWORD_UNSUPPORTED_MARKER as H, INTEGER_TEXT_PATTERN as I, OFFSET_PATTERN_SOURCE as J, NUMERIC_KEYWORDS as K, INTEGER_WIRE_PATTERN as L, FORMAT_PATTERNS as M, IDENTIFIER_PATTERN as N, DEFINITION_KEYWORDS as O, IF_THEN_ELSE_UNSUPPORTED_MARKER as P, PARAM_STYLES as Q, ISO_4217_CURRENCY_CODES as R, ARKTYPE_CODE_EXTENSION_KEYS as S, CONTENT_ENCODINGS as T, NUMBER_FORMATS as U, NON_IDENTIFIER_CHARACTER_PATTERN as V, NUMBER_RANGES as W, OUTPUT_EXTENSION as X, OPENAPI_COMPONENT_SUFFIX_MAP as Y, PARAM_LOCATIONS as Z, XExtMessages as _, JSONSchemaNumberFormat as a, STRING_WIRE_LOCATIONS as at, ALL_CODE_EXTENSION_KEYS as b, JSONSchemaTypes as c, VALIBOT_CODE_EXTENSION_KEYS as ct, Ref as d, ROOT_REFS as et, parseSchemaFile as f, XExtParam as g, ParamStyle as h, JSONSchemaFormat as i, STRING_KEYWORDS as it, EFFECT_CODE_EXTENSION_KEYS as j, DEFINITION_POINTER_PATTERN as k, LocalRef as l, WIRE_PRIMITIVE_ORDER as lt, ParamIn as m, ExternalRef as n, STRINGBOOL_TRUTHY_PATTERN as nt, JSONSchemaStringFormat as o, UNICODE_ESCAPE_PATTERN as ot, XExtTransform as p, OBJECT_PROTOTYPE_PROPERTIES as q, JSONSchema as r, STRING_FORMATS as rt, JSONSchemaType as s, UNSAFE_GENERATED_MARKER as st, ContentEncoding as t, STRINGBOOL_PATTERN as tt, ParseError as u, ZOD_CODE_EXTENSION_KEYS as ut, XExtCode as v, COMPONENT_SCHEMA_REF_PREFIX as w, ANCHOR_REF_PATTERN as x, JSONValue as y, JSON_SCHEMA_TYPES as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { $ as
|
|
2
|
-
import { i as ParamStyle, n as GenerateOptions, r as ParamIn, t as EmitOptions } from "./index-C2L8Ty0-.js";
|
|
1
|
+
import { $ as QUOTE_OR_BACKSLASH_PATTERN, A as DEFINITION_REF_PREFIXES, B as MISSING_DEFINITION_PREFIX, C as ARROW_FUNCTION_PATTERN, D as DEFAULT_ROOT_NAME, E as DATE_PATTERN_SOURCE, F as INPUT_EXTENSIONS, G as NUMBER_WIRE_PATTERN, H as NOT_KEYWORD_UNSUPPORTED_MARKER, I as INTEGER_TEXT_PATTERN, J as OFFSET_PATTERN_SOURCE, K as NUMERIC_KEYWORDS, L as INTEGER_WIRE_PATTERN, M as FORMAT_PATTERNS, N as IDENTIFIER_PATTERN, O as DEFINITION_KEYWORDS, P as IF_THEN_ELSE_UNSUPPORTED_MARKER, Q as PARAM_STYLES, R as ISO_4217_CURRENCY_CODES, S as ARKTYPE_CODE_EXTENSION_KEYS, T as CONTENT_ENCODINGS, U as NUMBER_FORMATS, V as NON_IDENTIFIER_CHARACTER_PATTERN, W as NUMBER_RANGES, X as OUTPUT_EXTENSION, Y as OPENAPI_COMPONENT_SUFFIX_MAP, Z as PARAM_LOCATIONS, _ as XExtMessages, a as JSONSchemaNumberFormat, at as STRING_WIRE_LOCATIONS, b as ALL_CODE_EXTENSION_KEYS, c as JSONSchemaTypes, ct as VALIBOT_CODE_EXTENSION_KEYS, d as Ref, et as ROOT_REFS, f as parseSchemaFile, g as XExtParam, h as ParamStyle, i as JSONSchemaFormat, it as STRING_KEYWORDS, j as EFFECT_CODE_EXTENSION_KEYS, k as DEFINITION_POINTER_PATTERN, l as LocalRef, lt as WIRE_PRIMITIVE_ORDER, m as ParamIn, n as ExternalRef, nt as STRINGBOOL_TRUTHY_PATTERN, o as JSONSchemaStringFormat, ot as UNICODE_ESCAPE_PATTERN, p as XExtTransform, q as OBJECT_PROTOTYPE_PROPERTIES, r as JSONSchema, rt as STRING_FORMATS, s as JSONSchemaType, st as UNSAFE_GENERATED_MARKER, t as ContentEncoding, tt as STRINGBOOL_PATTERN, u as ParseError, ut as ZOD_CODE_EXTENSION_KEYS, v as XExtCode, w as COMPONENT_SCHEMA_REF_PREFIX, x as ANCHOR_REF_PATTERN, y as JSONValue, z as JSON_SCHEMA_TYPES } from "./index-G7UqNQ4w.js";
|
|
3
2
|
import { schemaToAjv } from "./ajv.js";
|
|
3
|
+
import { n as GenerateOptions, r as SchemaDefinitions, t as EmitOptions } from "./index-EK43HM_z.js";
|
|
4
4
|
import { schemaToArktype } from "./arktype.js";
|
|
5
5
|
import { schemaToEffect } from "./effect.js";
|
|
6
6
|
import { TypeboxGenerateOptions, schemaToTypebox } from "./typebox.js";
|
|
@@ -87,6 +87,8 @@ export type TypeboxOptions = {
|
|
|
87
87
|
readonly paramIn?: ParamIn;
|
|
88
88
|
readonly cyclicRefs?: ReadonlySet<string>;
|
|
89
89
|
readonly ref?: string;
|
|
90
|
+
/** Definitions a `default` beside a `$ref` is checked against. */
|
|
91
|
+
readonly definitions?: SchemaDefinitions;
|
|
90
92
|
};
|
|
91
93
|
/**
|
|
92
94
|
* Options for a nested TypeBox node: the same as `options` but without a
|
|
@@ -121,6 +123,13 @@ export declare function valibot(schema: JSONSchema, rootName?: string, isValibot
|
|
|
121
123
|
export declare function yup(schema: JSONSchema): string;
|
|
122
124
|
//#endregion
|
|
123
125
|
//#region src/generator/zod/zod.d.ts
|
|
126
|
+
/**
|
|
127
|
+
* Re-push `issue` into `ctx` with `message`. Each issue code is narrowed first: the union of
|
|
128
|
+
* parsed issues does not type-check as a raw issue, while each member does.
|
|
129
|
+
*/
|
|
130
|
+
export declare function forwardIssue(message: string): string;
|
|
131
|
+
/** A message for the issues under the value (with a path); the root's own issues keep theirs. */
|
|
132
|
+
export declare function nestedIssueMessageWrap(inner: string, msg: string): string;
|
|
124
133
|
/**
|
|
125
134
|
* Generate Zod schema code from JSON Schema.
|
|
126
135
|
*
|
|
@@ -179,38 +188,67 @@ export declare function encodeNonAscii(name: string): string;
|
|
|
179
188
|
*/
|
|
180
189
|
export declare function toIdentifierPascalCase(name: string): string;
|
|
181
190
|
export declare function normalizeTypes(t?: string | readonly string[]): string[];
|
|
191
|
+
/** The keywords of a schema node {@link coerceDefault} reads a `default` against. */
|
|
192
|
+
export type DefaultSchema = {
|
|
193
|
+
readonly $ref?: string;
|
|
194
|
+
readonly type?: string | readonly string[];
|
|
195
|
+
readonly nullable?: boolean;
|
|
196
|
+
readonly enum?: readonly unknown[];
|
|
197
|
+
readonly const?: unknown;
|
|
198
|
+
readonly items?: DefaultSchema | readonly DefaultSchema[] | boolean;
|
|
199
|
+
readonly properties?: {
|
|
200
|
+
readonly [key: string]: DefaultSchema;
|
|
201
|
+
};
|
|
202
|
+
readonly required?: readonly string[];
|
|
203
|
+
readonly additionalProperties?: boolean | object;
|
|
204
|
+
readonly patternProperties?: object;
|
|
205
|
+
readonly unevaluatedProperties?: boolean | object;
|
|
206
|
+
readonly allOf?: readonly DefaultSchema[];
|
|
207
|
+
readonly oneOf?: readonly DefaultSchema[];
|
|
208
|
+
readonly anyOf?: readonly DefaultSchema[];
|
|
209
|
+
};
|
|
210
|
+
type DefaultResult = {
|
|
211
|
+
readonly keep: boolean;
|
|
212
|
+
readonly value: unknown;
|
|
213
|
+
};
|
|
182
214
|
/**
|
|
183
215
|
* Reconcile a schema's `default` value with the type the schema actually emits
|
|
184
|
-
* so the generated `.default(...)` stays assignable.
|
|
216
|
+
* so the generated `.default(...)` stays assignable. The default is kept only
|
|
217
|
+
* when it is a value of the schema, read the way a document usually writes it:
|
|
185
218
|
*
|
|
186
|
-
* - `null` on a
|
|
187
|
-
*
|
|
188
|
-
* - a
|
|
189
|
-
*
|
|
190
|
-
*
|
|
219
|
+
* - `null` is kept only on a nullable schema (or one whose `enum` lists it).
|
|
220
|
+
* - an `enum` / `const` takes only what it lists.
|
|
221
|
+
* - text that spells a value of the schema's type is read as that value: `'true'`
|
|
222
|
+
* for a boolean, `'7'` for a number or an integer; a number or a boolean for a
|
|
223
|
+
* string is written as text. Any other scalar of the wrong type is dropped
|
|
224
|
+
* (`'null'` for an integer, `'strict'` with the quotes for an enum).
|
|
225
|
+
* - an array keeps its items when every one is a value of `items`.
|
|
226
|
+
* - an object default drops keys the schema does not declare, when the emitted
|
|
227
|
+
* object has no index signature (`additionalProperties` is not `true` or a
|
|
228
|
+
* schema, and there are no `patternProperties`), and needs every `required`
|
|
229
|
+
* key. A loose or record schema keeps every key.
|
|
230
|
+
* - a `$ref`, or an `allOf` the node has no type of its own beside, is read as
|
|
231
|
+
* the definition it names when `definitions` has it; a `oneOf` / `anyOf` keeps
|
|
232
|
+
* the default when a member takes it, whatever `type` sits beside it.
|
|
233
|
+
* - a node with nothing to check against (no `type`, an unknown `$ref`) keeps
|
|
234
|
+
* the default as written.
|
|
191
235
|
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* mismatches and schemas without an explicit `type` (`oneOf`/`$ref` only)
|
|
195
|
-
* pass through untouched — JSON Schema 2020-12 §9.9.4.1 treats `default` as
|
|
196
|
-
* an annotation with no validity guarantee, so anything finer would need a
|
|
197
|
-
* full validator.
|
|
236
|
+
* JSON Schema 2020-12 §9.9.4.1 treats `default` as an annotation with no
|
|
237
|
+
* validity guarantee, so an invalid one is dropped rather than reported.
|
|
198
238
|
*
|
|
199
239
|
* @example
|
|
200
240
|
* ```ts
|
|
201
|
-
* coerceDefault({ type: 'boolean' }, 'true')
|
|
202
|
-
* coerceDefault({ type: '
|
|
241
|
+
* coerceDefault({ type: 'boolean' }, 'true') // { keep: true, value: true }
|
|
242
|
+
* coerceDefault({ type: 'number' }, '7') // { keep: true, value: 7 }
|
|
243
|
+
* coerceDefault({ type: 'object' }, null) // { keep: false, value: null }
|
|
203
244
|
* coerceDefault({ type: 'object', nullable: true }, null) // { keep: true, value: null }
|
|
204
|
-
* coerceDefault({ type: 'array' }, 'eval')
|
|
245
|
+
* coerceDefault({ type: 'array' }, 'eval') // { keep: false, value: 'eval' }
|
|
246
|
+
* coerceDefault({ enum: ['a', 'b'] }, "'a'") // { keep: false, value: "'a'" }
|
|
205
247
|
* ```
|
|
206
248
|
*/
|
|
207
|
-
export declare function coerceDefault(schema: {
|
|
208
|
-
readonly
|
|
209
|
-
|
|
210
|
-
}, value: unknown): {
|
|
211
|
-
readonly keep: boolean;
|
|
212
|
-
readonly value: unknown;
|
|
213
|
-
};
|
|
249
|
+
export declare function coerceDefault(schema: DefaultSchema, value: unknown, definitions?: {
|
|
250
|
+
readonly [name: string]: DefaultSchema;
|
|
251
|
+
}): DefaultResult;
|
|
214
252
|
/**
|
|
215
253
|
* A message as the expression an emitted check evaluates for each issue: an arrow source
|
|
216
254
|
* is applied to the issue, a sentence becomes a string literal.
|
|
@@ -300,4 +338,4 @@ export declare function resolveOpenAPIRef($ref: string): string | null;
|
|
|
300
338
|
*/
|
|
301
339
|
export declare function makeSafeKey(key: string): string;
|
|
302
340
|
//#endregion
|
|
303
|
-
export { ALL_CODE_EXTENSION_KEYS, ANCHOR_REF_PATTERN, ARKTYPE_CODE_EXTENSION_KEYS, ARROW_FUNCTION_PATTERN, COMPONENT_SCHEMA_REF_PREFIX, CONTENT_ENCODINGS, ContentEncoding, DATE_PATTERN_SOURCE, DEFAULT_ROOT_NAME, DEFINITION_KEYWORDS, DEFINITION_POINTER_PATTERN, DEFINITION_REF_PREFIXES, EFFECT_CODE_EXTENSION_KEYS, type EmitOptions, ExternalRef, FORMAT_PATTERNS, type GenerateOptions, IDENTIFIER_PATTERN, IF_THEN_ELSE_UNSUPPORTED_MARKER, INPUT_EXTENSIONS, INTEGER_TEXT_PATTERN, ISO_4217_CURRENCY_CODES, JSONSchema, JSONSchemaFormat, JSONSchemaNumberFormat, JSONSchemaStringFormat, JSONSchemaType, JSONSchemaTypes, type JSONValue, JSON_SCHEMA_TYPES, LocalRef, MISSING_DEFINITION_PREFIX, NON_IDENTIFIER_CHARACTER_PATTERN, NOT_KEYWORD_UNSUPPORTED_MARKER, NUMBER_FORMATS, NUMBER_RANGES, NUMERIC_KEYWORDS, OBJECT_PROTOTYPE_PROPERTIES, OFFSET_PATTERN_SOURCE, OPENAPI_COMPONENT_SUFFIX_MAP, OUTPUT_EXTENSION, PARAM_LOCATIONS, PARAM_STYLES, type ParamIn, type ParamStyle, ParseError, QUOTE_OR_BACKSLASH_PATTERN, ROOT_REFS, Ref, STRINGBOOL_PATTERN, STRINGBOOL_TRUTHY_PATTERN, STRING_FORMATS, STRING_KEYWORDS, STRING_WIRE_LOCATIONS, TypeboxGenerateOptions, UNICODE_ESCAPE_PATTERN, UNSAFE_GENERATED_MARKER, VALIBOT_CODE_EXTENSION_KEYS, WIRE_PRIMITIVE_ORDER, type XExtCode, type XExtMessages, type XExtTransform, ZOD_CODE_EXTENSION_KEYS, parseSchemaFile, schemaToAjv, schemaToArktype, schemaToEffect, schemaToTypebox, schemaToValibot, schemaToYup, schemaToZod };
|
|
341
|
+
export { ALL_CODE_EXTENSION_KEYS, ANCHOR_REF_PATTERN, ARKTYPE_CODE_EXTENSION_KEYS, ARROW_FUNCTION_PATTERN, COMPONENT_SCHEMA_REF_PREFIX, CONTENT_ENCODINGS, ContentEncoding, DATE_PATTERN_SOURCE, DEFAULT_ROOT_NAME, DEFINITION_KEYWORDS, DEFINITION_POINTER_PATTERN, DEFINITION_REF_PREFIXES, EFFECT_CODE_EXTENSION_KEYS, type EmitOptions, ExternalRef, FORMAT_PATTERNS, type GenerateOptions, IDENTIFIER_PATTERN, IF_THEN_ELSE_UNSUPPORTED_MARKER, INPUT_EXTENSIONS, INTEGER_TEXT_PATTERN, INTEGER_WIRE_PATTERN, ISO_4217_CURRENCY_CODES, JSONSchema, JSONSchemaFormat, JSONSchemaNumberFormat, JSONSchemaStringFormat, JSONSchemaType, JSONSchemaTypes, type JSONValue, JSON_SCHEMA_TYPES, LocalRef, MISSING_DEFINITION_PREFIX, NON_IDENTIFIER_CHARACTER_PATTERN, NOT_KEYWORD_UNSUPPORTED_MARKER, NUMBER_FORMATS, NUMBER_RANGES, NUMBER_WIRE_PATTERN, NUMERIC_KEYWORDS, OBJECT_PROTOTYPE_PROPERTIES, OFFSET_PATTERN_SOURCE, OPENAPI_COMPONENT_SUFFIX_MAP, OUTPUT_EXTENSION, PARAM_LOCATIONS, PARAM_STYLES, type ParamIn, type ParamStyle, ParseError, QUOTE_OR_BACKSLASH_PATTERN, ROOT_REFS, Ref, STRINGBOOL_PATTERN, STRINGBOOL_TRUTHY_PATTERN, STRING_FORMATS, STRING_KEYWORDS, STRING_WIRE_LOCATIONS, TypeboxGenerateOptions, UNICODE_ESCAPE_PATTERN, UNSAFE_GENERATED_MARKER, VALIBOT_CODE_EXTENSION_KEYS, WIRE_PRIMITIVE_ORDER, type XExtCode, type XExtMessages, type XExtParam, type XExtTransform, ZOD_CODE_EXTENSION_KEYS, parseSchemaFile, schemaToAjv, schemaToArktype, schemaToEffect, schemaToTypebox, schemaToValibot, schemaToYup, schemaToZod };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { $ as DEFINITION_REF_PREFIXES, At as
|
|
1
|
+
import { $ as DEFINITION_REF_PREFIXES, At as UNSAFE_GENERATED_MARKER, Ct as ROOT_REFS, Dt as STRING_KEYWORDS, Et as STRING_FORMATS, G as ARKTYPE_CODE_EXTENSION_KEYS, J as CONTENT_ENCODINGS, K as ARROW_FUNCTION_PATTERN, Mt as WIRE_PRIMITIVE_ORDER, Nt as ZOD_CODE_EXTENSION_KEYS, Ot as STRING_WIRE_LOCATIONS, Q as DEFINITION_POINTER_PATTERN, St as QUOTE_OR_BACKSLASH_PATTERN, Tt as STRINGBOOL_TRUTHY_PATTERN, U as ALL_CODE_EXTENSION_KEYS, W as ANCHOR_REF_PATTERN, X as DEFAULT_ROOT_NAME, Y as DATE_PATTERN_SOURCE, Z as DEFINITION_KEYWORDS, _t as OFFSET_PATTERN_SOURCE, a as encodeNonAscii, at as INTEGER_TEXT_PATTERN, bt as PARAM_LOCATIONS, c as normalizeTypes, ct as JSON_SCHEMA_TYPES, d as toPascalCase, dt as NOT_KEYWORD_UNSUPPORTED_MARKER, et as EFFECT_CODE_EXTENSION_KEYS, f as valibotError, ft as NUMBER_FORMATS, g as zodErrorPart, gt as OBJECT_PROTOTYPE_PROPERTIES, h as zodError, ht as NUMERIC_KEYWORDS, i as effectMessage, it as INPUT_EXTENSIONS, jt as VALIBOT_CODE_EXTENSION_KEYS, kt as UNICODE_ESCAPE_PATTERN, l as resolveOpenAPIRef, lt as MISSING_DEFINITION_PREFIX, m as zodBaseError, mt as NUMBER_WIRE_PATTERN, n as effectError, nt as IDENTIFIER_PATTERN, o as issueMessageExpression, ot as INTEGER_WIRE_PATTERN, p as valibotErrorPart, pt as NUMBER_RANGES, q as COMPONENT_SCHEMA_REF_PREFIX, r as effectErrorPart, rt as IF_THEN_ELSE_UNSUPPORTED_MARKER, s as makeSafeKey, st as ISO_4217_CURRENCY_CODES, t as coerceDefault, tt as FORMAT_PATTERNS, u as toIdentifierPascalCase, ut as NON_IDENTIFIER_CHARACTER_PATTERN, vt as OPENAPI_COMPONENT_SUFFIX_MAP, wt as STRINGBOOL_PATTERN, xt as PARAM_STYLES, yt as OUTPUT_EXTENSION } from "./utils-Bky98UmP.js";
|
|
2
2
|
import { i as fmt, n as parseSchemaFile, r as FormatError, t as ParseError } from "./parser-CyJ2auea.js";
|
|
3
|
-
import { n as ajv, t as schemaToAjv } from "./ajv-
|
|
4
|
-
import { n as arktype, t as schemaToArktype } from "./arktype-
|
|
5
|
-
import { n as effect, r as wholeValueMessage, t as schemaToEffect } from "./effect-
|
|
6
|
-
import { n as nestedTypeboxOptions, r as typebox, t as schemaToTypebox } from "./typebox-
|
|
7
|
-
import { n as valibot, t as schemaToValibot } from "./valibot-
|
|
8
|
-
import { n as yup, t as schemaToYup } from "./yup-
|
|
9
|
-
import {
|
|
10
|
-
export { ALL_CODE_EXTENSION_KEYS, ANCHOR_REF_PATTERN, ARKTYPE_CODE_EXTENSION_KEYS, ARROW_FUNCTION_PATTERN, COMPONENT_SCHEMA_REF_PREFIX, CONTENT_ENCODINGS, DATE_PATTERN_SOURCE, DEFAULT_ROOT_NAME, DEFINITION_KEYWORDS, DEFINITION_POINTER_PATTERN, DEFINITION_REF_PREFIXES, EFFECT_CODE_EXTENSION_KEYS, FORMAT_PATTERNS, FormatError, IDENTIFIER_PATTERN, IF_THEN_ELSE_UNSUPPORTED_MARKER, INPUT_EXTENSIONS, INTEGER_TEXT_PATTERN, ISO_4217_CURRENCY_CODES, JSON_SCHEMA_TYPES, MISSING_DEFINITION_PREFIX, NON_IDENTIFIER_CHARACTER_PATTERN, NOT_KEYWORD_UNSUPPORTED_MARKER, NUMBER_FORMATS, NUMBER_RANGES, NUMERIC_KEYWORDS, OBJECT_PROTOTYPE_PROPERTIES, OFFSET_PATTERN_SOURCE, OPENAPI_COMPONENT_SUFFIX_MAP, OUTPUT_EXTENSION, PARAM_LOCATIONS, PARAM_STYLES, ParseError, QUOTE_OR_BACKSLASH_PATTERN, ROOT_REFS, STRINGBOOL_PATTERN, STRINGBOOL_TRUTHY_PATTERN, STRING_FORMATS, STRING_KEYWORDS, STRING_WIRE_LOCATIONS, UNICODE_ESCAPE_PATTERN, UNSAFE_GENERATED_MARKER, VALIBOT_CODE_EXTENSION_KEYS, WIRE_PRIMITIVE_ORDER, ZOD_CODE_EXTENSION_KEYS, ajv, arktype, coerceDefault, effect, effectError, effectErrorPart, effectMessage, encodeNonAscii, fmt, issueMessageExpression, makeSafeKey, nestedTypeboxOptions, normalizeTypes, parseSchemaFile, resolveOpenAPIRef, schemaToAjv, schemaToArktype, schemaToEffect, schemaToTypebox, schemaToValibot, schemaToYup, schemaToZod, toIdentifierPascalCase, toPascalCase, typebox, valibot, valibotError, valibotErrorPart, wholeValueMessage, yup, zod, zodBaseError, zodError, zodErrorPart };
|
|
3
|
+
import { n as ajv, t as schemaToAjv } from "./ajv-DS9gtNZC.js";
|
|
4
|
+
import { n as arktype, t as schemaToArktype } from "./arktype-CBV4bg-0.js";
|
|
5
|
+
import { n as effect, r as wholeValueMessage, t as schemaToEffect } from "./effect-D9pvwi_6.js";
|
|
6
|
+
import { n as nestedTypeboxOptions, r as typebox, t as schemaToTypebox } from "./typebox-DGGKJLdP.js";
|
|
7
|
+
import { n as valibot, t as schemaToValibot } from "./valibot-B6y4VQFF.js";
|
|
8
|
+
import { n as yup, t as schemaToYup } from "./yup-DuqQwHUv.js";
|
|
9
|
+
import { i as zod, n as forwardIssue, r as nestedIssueMessageWrap, t as schemaToZod } from "./zod-CXo9NFuF.js";
|
|
10
|
+
export { ALL_CODE_EXTENSION_KEYS, ANCHOR_REF_PATTERN, ARKTYPE_CODE_EXTENSION_KEYS, ARROW_FUNCTION_PATTERN, COMPONENT_SCHEMA_REF_PREFIX, CONTENT_ENCODINGS, DATE_PATTERN_SOURCE, DEFAULT_ROOT_NAME, DEFINITION_KEYWORDS, DEFINITION_POINTER_PATTERN, DEFINITION_REF_PREFIXES, EFFECT_CODE_EXTENSION_KEYS, FORMAT_PATTERNS, FormatError, IDENTIFIER_PATTERN, IF_THEN_ELSE_UNSUPPORTED_MARKER, INPUT_EXTENSIONS, INTEGER_TEXT_PATTERN, INTEGER_WIRE_PATTERN, ISO_4217_CURRENCY_CODES, JSON_SCHEMA_TYPES, MISSING_DEFINITION_PREFIX, NON_IDENTIFIER_CHARACTER_PATTERN, NOT_KEYWORD_UNSUPPORTED_MARKER, NUMBER_FORMATS, NUMBER_RANGES, NUMBER_WIRE_PATTERN, NUMERIC_KEYWORDS, OBJECT_PROTOTYPE_PROPERTIES, OFFSET_PATTERN_SOURCE, OPENAPI_COMPONENT_SUFFIX_MAP, OUTPUT_EXTENSION, PARAM_LOCATIONS, PARAM_STYLES, ParseError, QUOTE_OR_BACKSLASH_PATTERN, ROOT_REFS, STRINGBOOL_PATTERN, STRINGBOOL_TRUTHY_PATTERN, STRING_FORMATS, STRING_KEYWORDS, STRING_WIRE_LOCATIONS, UNICODE_ESCAPE_PATTERN, UNSAFE_GENERATED_MARKER, VALIBOT_CODE_EXTENSION_KEYS, WIRE_PRIMITIVE_ORDER, ZOD_CODE_EXTENSION_KEYS, ajv, arktype, coerceDefault, effect, effectError, effectErrorPart, effectMessage, encodeNonAscii, fmt, forwardIssue, issueMessageExpression, makeSafeKey, nestedIssueMessageWrap, nestedTypeboxOptions, normalizeTypes, parseSchemaFile, resolveOpenAPIRef, schemaToAjv, schemaToArktype, schemaToEffect, schemaToTypebox, schemaToValibot, schemaToYup, schemaToZod, toIdentifierPascalCase, toPascalCase, typebox, valibot, valibotError, valibotErrorPart, wholeValueMessage, yup, zod, zodBaseError, zodError, zodErrorPart };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { $ as DEFINITION_REF_PREFIXES, 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, j as isNullTypeMember, k as isDefaultOnlyMember, l as resolveOpenAPIRef, s as makeSafeKey,
|
|
2
|
-
import { D as
|
|
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 { B as emitOptionsOf, D as stringboolTruthyPattern, E as stringboolPattern, G as schemaNamer, H as planDocument, I as typeboxDefaultOpt, J as hasRootSelfReference, K as discriminatedMembers, L as typeboxMetaOpts, R as typeboxRefOpt, U as renderDefinitions, V as missingDefinitionComment, W as rootNameOf, X as itemsSchemaOf, Y as isOptionalByDefault, a as typeboxWrap, at as foldAllOfMembers, b as integerWirePattern, c as withPolymorphicBases, g as hoistNestedRefs, nt as hasNotKeyword, ot as isKeywordOnlyMember, q as cyclicDefinitionNames, st as patternSource, w as numberWirePattern, z as definitionsOf } from "./helper-S7SSsYwh.js";
|
|
3
3
|
//#region src/generator/typebox/enum.ts
|
|
4
4
|
function lit(v) {
|
|
5
5
|
if (v === null) return "null";
|
|
@@ -123,7 +123,7 @@ function number(schema, ref) {
|
|
|
123
123
|
function object(schema, rootName, isTypebox, options) {
|
|
124
124
|
const childOptions = nestedTypeboxOptions(options);
|
|
125
125
|
if (hasCombinator(schema)) return typebox(schema, rootName, isTypebox, options);
|
|
126
|
-
if (typeof schema.additionalProperties === "object") {
|
|
126
|
+
if (typeof schema.additionalProperties === "object" && schema.properties === void 0 && schema.patternProperties === void 0) {
|
|
127
127
|
const value = typebox(schema.additionalProperties, rootName, isTypebox, childOptions);
|
|
128
128
|
const recordOpts = [
|
|
129
129
|
...typeboxRefOpt(options?.ref),
|
|
@@ -144,6 +144,7 @@ function object(schema, rootName, isTypebox, options) {
|
|
|
144
144
|
}
|
|
145
145
|
const emptyOpts = [
|
|
146
146
|
...typeboxRefOpt(options?.ref),
|
|
147
|
+
schema.additionalProperties === false ? "additionalProperties:false" : typeof schema.additionalProperties === "object" ? `additionalProperties:${typebox(schema.additionalProperties, rootName, isTypebox, childOptions)}` : void 0,
|
|
147
148
|
...buildAdvancedOpts(schema, rootName, isTypebox, childOptions),
|
|
148
149
|
...typeboxMetaOpts(schema),
|
|
149
150
|
...typeboxDefaultOpt(schema)
|
|
@@ -151,12 +152,13 @@ function object(schema, rootName, isTypebox, options) {
|
|
|
151
152
|
return emptyOpts.length === 0 ? "Type.Object({})" : `Type.Object({},{${emptyOpts.join(",")}})`;
|
|
152
153
|
}
|
|
153
154
|
const required = new Set(schema.required);
|
|
154
|
-
const
|
|
155
|
+
const undeclaredRequired = (schema.required ?? []).filter((key) => !Object.hasOwn(schema.properties ?? {}, key)).map((key) => `${makeSafeKey(key)}:Type.Unknown()`);
|
|
156
|
+
const props = [...Object.entries(schema.properties).map(([key, propSchema]) => {
|
|
155
157
|
const parsed = typebox(propSchema, rootName, isTypebox, childOptions);
|
|
156
158
|
if (!parsed) return null;
|
|
157
159
|
const safeKey = makeSafeKey(key);
|
|
158
|
-
return required.has(key) || isOptionalByDefault(propSchema) ? `${safeKey}:${parsed}` : `${safeKey}:Type.Optional(${parsed})`;
|
|
159
|
-
}).filter((p) => p !== null);
|
|
160
|
+
return required.has(key) || isOptionalByDefault(propSchema, options?.definitions) ? `${safeKey}:${parsed}` : `${safeKey}:Type.Optional(${parsed})`;
|
|
161
|
+
}).filter((p) => p !== null), ...undeclaredRequired];
|
|
160
162
|
const objectErrorMessageEntries = [];
|
|
161
163
|
const objectErrorMessage = schema["x-error-message"];
|
|
162
164
|
if (objectErrorMessage) objectErrorMessageEntries.push(`type:${JSON.stringify(objectErrorMessage)}`);
|
|
@@ -188,7 +190,7 @@ function object(schema, rootName, isTypebox, options) {
|
|
|
188
190
|
const objectErrorMessageField = objectErrorMessageEntries.length > 0 ? `errorMessage:{${objectErrorMessageEntries.join(",")}}` : void 0;
|
|
189
191
|
const optParts = [
|
|
190
192
|
...typeboxRefOpt(options?.ref),
|
|
191
|
-
schema.additionalProperties === false ? "additionalProperties:false" : void 0,
|
|
193
|
+
schema.additionalProperties === false ? "additionalProperties:false" : typeof schema.additionalProperties === "object" ? `additionalProperties:${typebox(schema.additionalProperties, rootName, isTypebox, childOptions)}` : void 0,
|
|
192
194
|
...buildAdvancedOpts(schema, rootName, isTypebox, childOptions),
|
|
193
195
|
objectErrorMessageField,
|
|
194
196
|
...typeboxMetaOpts(schema),
|
|
@@ -209,6 +211,8 @@ function buildAdvancedOpts(schema, rootName, isTypebox, options) {
|
|
|
209
211
|
const thenOpt = schema.then ? `then:${typebox(schema.then, rootName, isTypebox, options)}` : void 0;
|
|
210
212
|
const elseOpt = schema.else ? `else:${typebox(schema.else, rootName, isTypebox, options)}` : void 0;
|
|
211
213
|
const requiredOpt = !schema.properties && Array.isArray(schema.required) && schema.required.length > 0 ? `required:${JSON.stringify(schema.required)}` : void 0;
|
|
214
|
+
const unevaluated = schema.unevaluatedProperties;
|
|
215
|
+
const unevaluatedOpt = unevaluated === false ? "unevaluatedProperties:false" : typeof unevaluated === "object" ? `unevaluatedProperties:${typebox(unevaluated, rootName, isTypebox, options)}` : void 0;
|
|
212
216
|
return [
|
|
213
217
|
typeof schema.minProperties === "number" ? `minProperties:${schema.minProperties}` : void 0,
|
|
214
218
|
typeof schema.maxProperties === "number" ? `maxProperties:${schema.maxProperties}` : void 0,
|
|
@@ -218,7 +222,8 @@ function buildAdvancedOpts(schema, rootName, isTypebox, options) {
|
|
|
218
222
|
ifOpt,
|
|
219
223
|
thenOpt,
|
|
220
224
|
elseOpt,
|
|
221
|
-
requiredOpt
|
|
225
|
+
requiredOpt,
|
|
226
|
+
unevaluatedOpt
|
|
222
227
|
];
|
|
223
228
|
}
|
|
224
229
|
//#endregion
|
|
@@ -260,7 +265,7 @@ function string(schema, ref) {
|
|
|
260
265
|
const opts = [
|
|
261
266
|
...typeboxRefOpt(ref),
|
|
262
267
|
schema.format && FORMAT_MAP[schema.format] ? `format:${JSON.stringify(FORMAT_MAP[schema.format])}` : void 0,
|
|
263
|
-
schema.pattern ? `pattern:${JSON.stringify(schema.pattern)}` : contentPattern ? `pattern:${JSON.stringify(contentPattern)}` : void 0,
|
|
268
|
+
schema.pattern ? `pattern:${JSON.stringify(patternSource(schema.pattern))}` : contentPattern ? `pattern:${JSON.stringify(contentPattern)}` : void 0,
|
|
264
269
|
isFixedLength ? `minLength:${schema.minLength}` : void 0,
|
|
265
270
|
isFixedLength ? `maxLength:${schema.maxLength}` : void 0,
|
|
266
271
|
!isFixedLength && schema.minLength !== void 0 ? `minLength:${schema.minLength}` : void 0,
|
|
@@ -290,7 +295,8 @@ function nestedTypeboxOptions(options) {
|
|
|
290
295
|
...options.openapi !== void 0 && { openapi: options.openapi },
|
|
291
296
|
...options.readonly !== void 0 && { readonly: options.readonly },
|
|
292
297
|
...options.paramIn !== void 0 && { paramIn: options.paramIn },
|
|
293
|
-
...options.cyclicRefs !== void 0 && { cyclicRefs: options.cyclicRefs }
|
|
298
|
+
...options.cyclicRefs !== void 0 && { cyclicRefs: options.cyclicRefs },
|
|
299
|
+
...options.definitions !== void 0 && { definitions: options.definitions }
|
|
294
300
|
};
|
|
295
301
|
}
|
|
296
302
|
/**
|
|
@@ -312,10 +318,26 @@ function tbPrim(name, schema, extraOpts = [], ref) {
|
|
|
312
318
|
* Codec decodes from a string. A host `ref` lands on this inner `Type.String`,
|
|
313
319
|
* not on the `Codec(...)` wrapper.
|
|
314
320
|
*/
|
|
315
|
-
function wireString(schema, ref) {
|
|
316
|
-
const opts = [
|
|
321
|
+
function wireString(schema, ref, pattern) {
|
|
322
|
+
const opts = [
|
|
323
|
+
...typeboxRefOpt(ref),
|
|
324
|
+
...pattern === void 0 ? [] : [`pattern:${pattern}`],
|
|
325
|
+
...typeboxDefaultOpt(schema, true)
|
|
326
|
+
];
|
|
317
327
|
return opts.length > 0 ? `Type.String({${opts.join(",")}})` : "Type.String()";
|
|
318
328
|
}
|
|
329
|
+
/** A keyword-only `allOf` member. TypeBox has no type-guarded check, so the keyword's type carries it. */
|
|
330
|
+
function keywordSchema(schema) {
|
|
331
|
+
if (hasStringKeyword(schema) && !hasNumericKeyword(schema)) return string({
|
|
332
|
+
...schema,
|
|
333
|
+
type: "string"
|
|
334
|
+
});
|
|
335
|
+
if (hasNumericKeyword(schema) && !hasStringKeyword(schema)) return number({
|
|
336
|
+
...schema,
|
|
337
|
+
type: "number"
|
|
338
|
+
});
|
|
339
|
+
return "Type.Any()";
|
|
340
|
+
}
|
|
319
341
|
/**
|
|
320
342
|
* Emits a two-argument TypeBox factory call (`Type.X(payload, {opts})`),
|
|
321
343
|
* embedding any meta options from the schema. Returns `Type.X(payload)` when
|
|
@@ -342,6 +364,7 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
342
364
|
const readonly = (v) => options?.readonly ? `Type.Readonly(${v})` : v;
|
|
343
365
|
const hostRef = options?.ref;
|
|
344
366
|
const childOptions = nestedTypeboxOptions(options);
|
|
367
|
+
const definitions = options?.definitions;
|
|
345
368
|
const named = (name, s) => options?.cyclicRefs?.has(name) ? byName(name, s) : name;
|
|
346
369
|
if (schema.$ref) {
|
|
347
370
|
const ref = (s) => {
|
|
@@ -364,13 +387,27 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
364
387
|
}
|
|
365
388
|
return "Type.Any()";
|
|
366
389
|
};
|
|
390
|
+
const withDefault = (s) => {
|
|
391
|
+
const defaultOpt = typeboxDefaultOpt(s, false, definitions);
|
|
392
|
+
const target = ref(s);
|
|
393
|
+
const inner = defaultOpt.length === 0 ? target : `Type.Intersect([${target}],{${defaultOpt.join(",")}})`;
|
|
394
|
+
return typeboxWrap(inner, s, definitions);
|
|
395
|
+
};
|
|
367
396
|
const items = itemsSchemaOf(schema.items);
|
|
368
|
-
if (schema.type === "array" && items?.$ref) return `Type.Array(${
|
|
369
|
-
return
|
|
397
|
+
if (schema.type === "array" && items?.$ref) return `Type.Array(${withDefault(items)})`;
|
|
398
|
+
return withDefault(schema);
|
|
370
399
|
}
|
|
371
400
|
if (schema.oneOf) {
|
|
372
401
|
if (schema.oneOf.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
373
|
-
const
|
|
402
|
+
const discriminated = discriminatedMembers(schema, schema.oneOf);
|
|
403
|
+
const schemas = schema.oneOf.map((s, i) => {
|
|
404
|
+
const member = typebox(s, rootName, isTypebox, childOptions);
|
|
405
|
+
const values = discriminated?.members[i]?.values;
|
|
406
|
+
if (discriminated === void 0 || values === void 0) return member;
|
|
407
|
+
const literals = values.map((value) => `Type.Literal(${JSON.stringify(value)})`);
|
|
408
|
+
const literal = literals.length === 1 ? literals[0] : `Type.Union([${literals.join(",")}])`;
|
|
409
|
+
return `Type.Intersect([${member},Type.Object({${makeSafeKey(discriminated.propertyName)}:${literal}})])`;
|
|
410
|
+
});
|
|
374
411
|
return typeboxWrap(tbComp("Type.Union", `[${schemas.join(",")}]`, schema, messageOpt(schema["x-oneOf-message"]), hostRef), schema);
|
|
375
412
|
}
|
|
376
413
|
if (schema.anyOf) {
|
|
@@ -383,26 +420,26 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
383
420
|
if (schema.allOf.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
384
421
|
const nullable = isNullable(schema) || schema.allOf.some(isNullTypeMember);
|
|
385
422
|
const defaultValue = schema.allOf.find(isDefaultOnlyMember)?.default;
|
|
386
|
-
const schemas = schema.allOf.filter((s) => !isShapelessMember(s)).map((s) => typebox(s, rootName, isTypebox, childOptions));
|
|
423
|
+
const schemas = foldAllOfMembers(schema.allOf.filter((s) => !isShapelessMember(s)), isStringWireParam).map((s) => isKeywordOnlyMember(s) ? keywordSchema(s) : typebox(s, rootName, isTypebox, childOptions));
|
|
387
424
|
if (schemas.length === 0) return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), {
|
|
388
425
|
...schema,
|
|
389
426
|
nullable
|
|
390
427
|
});
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
...
|
|
404
|
-
|
|
405
|
-
|
|
428
|
+
const unevaluated = schema.unevaluatedProperties;
|
|
429
|
+
const unevaluatedOpt = [...schema.patternProperties === void 0 ? [] : [`patternProperties:{${Object.entries(schema.patternProperties).map(([pattern, propSchema]) => `${JSON.stringify(pattern)}:${typebox(propSchema, rootName, isTypebox, childOptions)}`).join(",")}}`], ...unevaluated === false ? ["unevaluatedProperties:false"] : typeof unevaluated === "object" && unevaluated !== null ? [`unevaluatedProperties:${typebox(unevaluated, rootName, isTypebox, childOptions)}`] : []];
|
|
430
|
+
const { default: ownDefault, ...withoutDefault } = schema;
|
|
431
|
+
const withMergedDefault = {
|
|
432
|
+
...withoutDefault,
|
|
433
|
+
nullable,
|
|
434
|
+
...defaultValue !== void 0 ? { default: defaultValue } : ownDefault !== void 0 && { default: ownDefault }
|
|
435
|
+
};
|
|
436
|
+
const defaultOpt = typeboxDefaultOpt(withMergedDefault, false, definitions);
|
|
437
|
+
const baseResult = schemas.length === 1 && unevaluatedOpt.length === 0 && defaultOpt.length === 0 ? schemas[0] : tbComp("Type.Intersect", `[${schemas.join(",")}]`, withoutDefault, [
|
|
438
|
+
...messageOpt(schema["x-allOf-message"]),
|
|
439
|
+
...unevaluatedOpt,
|
|
440
|
+
...defaultOpt
|
|
441
|
+
], hostRef);
|
|
442
|
+
return typeboxWrap(baseResult, withMergedDefault, definitions);
|
|
406
443
|
}
|
|
407
444
|
if (schema.not) return typeboxWrap(tbPrim("Type.Any", schema, messageOpt(schema["x-not-message"]), hostRef), schema);
|
|
408
445
|
if (schema.const !== void 0) {
|
|
@@ -418,7 +455,7 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
418
455
|
if (types.includes("number")) {
|
|
419
456
|
const base = number(schema, hostRef);
|
|
420
457
|
if (isStringWireParam) {
|
|
421
|
-
const wire = wireString(schema, hostRef);
|
|
458
|
+
const wire = wireString(schema, hostRef, numberWirePattern());
|
|
422
459
|
return typeboxWrap(`Codec(${wire}).Decode((value)=>Number(value)).Encode((value)=>String(value))`, schema);
|
|
423
460
|
}
|
|
424
461
|
return typeboxWrap(base, schema);
|
|
@@ -426,16 +463,15 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
426
463
|
if (types.includes("integer")) {
|
|
427
464
|
const base = integer(schema, hostRef);
|
|
428
465
|
if (isStringWireParam) {
|
|
429
|
-
const wire = wireString(schema, hostRef);
|
|
430
|
-
return typeboxWrap(`Codec(${wire}).Decode((value)=>Number
|
|
466
|
+
const wire = wireString(schema, hostRef, integerWirePattern());
|
|
467
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>Number(value)).Encode((value)=>String(value))`, schema);
|
|
431
468
|
}
|
|
432
469
|
return typeboxWrap(base, schema);
|
|
433
470
|
}
|
|
434
471
|
if (types.includes("boolean")) {
|
|
435
472
|
if (isStringWireParam) {
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
return typeboxWrap(`Codec(${union}).Decode((value)=>value==='true').Encode((value)=>value?'true':'false')`, schema);
|
|
473
|
+
const wire = wireString(schema, hostRef, stringboolPattern());
|
|
474
|
+
return typeboxWrap(`Codec(${wire}).Decode((value)=>${stringboolTruthyPattern()}.test(value)).Encode((value)=>value?'true':'false')`, schema);
|
|
439
475
|
}
|
|
440
476
|
return typeboxWrap(tbPrim("Type.Boolean", schema, [], hostRef), schema);
|
|
441
477
|
}
|
|
@@ -482,6 +518,7 @@ function typebox(schema, rootName = DEFAULT_ROOT_NAME, isTypebox = false, option
|
|
|
482
518
|
return typeboxWrap(`Codec(${wire}).Decode((value)=>new Date(value)).Encode((value)=>value.toISOString())`, schema);
|
|
483
519
|
}
|
|
484
520
|
if (types.length === 1 && types[0] === "null") return typeboxWrap(tbPrim("Type.Null", schema, [], hostRef), schema);
|
|
521
|
+
if (typeof schema.additionalProperties === "object") return readonly(typeboxWrap(object(schema, rootName, isTypebox, options), schema));
|
|
485
522
|
return typeboxWrap(tbPrim("Type.Any", schema, [], hostRef), schema);
|
|
486
523
|
}
|
|
487
524
|
//#endregion
|
|
@@ -495,23 +532,26 @@ function importLine(emitted) {
|
|
|
495
532
|
* with plain `const` declarations, so a cyclic document becomes one
|
|
496
533
|
* `Type.Cyclic($defs, root)` map instead.
|
|
497
534
|
*/
|
|
498
|
-
function schemaToTypebox(
|
|
535
|
+
function schemaToTypebox(input, options) {
|
|
536
|
+
const schema = withPolymorphicBases(hoistNestedRefs(input));
|
|
499
537
|
const exportType = options?.exportType ?? true;
|
|
500
538
|
const openapi = options?.openapi ?? false;
|
|
501
539
|
const hostRef = options?.ref;
|
|
540
|
+
const toName = schemaNamer(openapi);
|
|
541
|
+
const plan = planDocument(schema, rootNameOf(schema, toName));
|
|
542
|
+
const { rootName } = plan;
|
|
543
|
+
const definitions = definitionsOf(emitOptionsOf(options), plan.definitions);
|
|
502
544
|
const genOptions = {
|
|
503
545
|
openapi,
|
|
504
546
|
readonly: options?.readonly ?? false,
|
|
505
|
-
...options?.paramIn !== void 0 && { paramIn: options.paramIn }
|
|
547
|
+
...options?.paramIn !== void 0 && { paramIn: options.paramIn },
|
|
548
|
+
...definitions !== void 0 && { definitions }
|
|
506
549
|
};
|
|
507
550
|
const rootOptions = hostRef === void 0 ? genOptions : {
|
|
508
551
|
...genOptions,
|
|
509
552
|
ref: hostRef
|
|
510
553
|
};
|
|
511
554
|
const notKeywordPresent = hasNotKeyword(schema);
|
|
512
|
-
const toName = schemaNamer(openapi);
|
|
513
|
-
const plan = planDocument(schema, rootNameOf(schema, toName));
|
|
514
|
-
const { rootName } = plan;
|
|
515
555
|
if (hasRootSelfReference(plan.root, (ref) => isRootRef(ref) || openapi && resolveOpenAPIRef(ref) === rootName) || cyclicDefinitionNames(schema).size > 0) {
|
|
516
556
|
const cyclicRefs = /* @__PURE__ */ new Set([...plan.orderedNames.map(toName), rootName]);
|
|
517
557
|
const cyclicOptions = {
|
package/dist/typebox.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { r as JSONSchema } from "./index-
|
|
2
|
-
import { n as GenerateOptions } from "./index-
|
|
1
|
+
import { r as JSONSchema } from "./index-G7UqNQ4w.js";
|
|
2
|
+
import { n as GenerateOptions } from "./index-EK43HM_z.js";
|
|
3
3
|
//#region src/generator/typebox/index.d.ts
|
|
4
4
|
/** The options of `schemaToTypebox`: the common ones, plus a host `$ref` name. */
|
|
5
5
|
export type TypeboxGenerateOptions = Omit<GenerateOptions, 'unsafeCodeExtensions'> & {
|
|
@@ -17,5 +17,5 @@ export type TypeboxGenerateOptions = Omit<GenerateOptions, 'unsafeCodeExtensions
|
|
|
17
17
|
* with plain `const` declarations, so a cyclic document becomes one
|
|
18
18
|
* `Type.Cyclic($defs, root)` map instead.
|
|
19
19
|
*/
|
|
20
|
-
export declare function schemaToTypebox(
|
|
20
|
+
export declare function schemaToTypebox(input: JSONSchema, options?: TypeboxGenerateOptions): string;
|
|
21
21
|
//#endregion
|
package/dist/typebox.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as schemaToTypebox } from "./typebox-
|
|
1
|
+
export { t as schemaToTypebox } from "./typebox-DGGKJLdP.js";
|