schema-components 1.12.11 → 1.13.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.
Files changed (63) hide show
  1. package/README.md +31 -0
  2. package/dist/core/adapter.d.mts +1 -1
  3. package/dist/core/adapter.mjs +37 -8
  4. package/dist/core/constraints.d.mts +16 -0
  5. package/dist/core/constraints.mjs +138 -0
  6. package/dist/core/merge.d.mts +32 -0
  7. package/dist/core/merge.mjs +96 -0
  8. package/dist/core/normalise.d.mts +40 -0
  9. package/dist/core/normalise.mjs +171 -0
  10. package/dist/core/openapi30.d.mts +38 -0
  11. package/dist/core/openapi30.mjs +223 -0
  12. package/dist/core/ref.d.mts +25 -0
  13. package/dist/core/ref.mjs +86 -0
  14. package/dist/core/renderer.d.mts +2 -2
  15. package/dist/core/renderer.mjs +8 -0
  16. package/dist/core/swagger2.d.mts +10 -0
  17. package/dist/core/swagger2.mjs +294 -0
  18. package/dist/core/typeInference.d.mts +2 -0
  19. package/dist/core/typeInference.mjs +1 -0
  20. package/dist/core/types.d.mts +2 -3
  21. package/dist/core/types.mjs +55 -2
  22. package/dist/core/version.d.mts +2 -0
  23. package/dist/core/version.mjs +79 -0
  24. package/dist/core/walkBuilders.d.mts +52 -0
  25. package/dist/core/walkBuilders.mjs +152 -0
  26. package/dist/core/walker.d.mts +3 -10
  27. package/dist/core/walker.mjs +143 -231
  28. package/dist/html/a11y.d.mts +5 -4
  29. package/dist/html/renderToHtml.d.mts +3 -3
  30. package/dist/html/renderToHtml.mjs +23 -379
  31. package/dist/html/renderToHtmlStream.d.mts +29 -47
  32. package/dist/html/renderToHtmlStream.mjs +33 -305
  33. package/dist/html/renderers.d.mts +14 -0
  34. package/dist/html/renderers.mjs +406 -0
  35. package/dist/html/streamRenderers.d.mts +13 -0
  36. package/dist/html/streamRenderers.mjs +243 -0
  37. package/dist/openapi/components.d.mts +2 -1
  38. package/dist/openapi/parser.d.mts +59 -2
  39. package/dist/openapi/parser.mjs +189 -8
  40. package/dist/react/SchemaComponent.d.mts +4 -2
  41. package/dist/react/SchemaComponent.mjs +39 -85
  42. package/dist/react/SchemaView.d.mts +2 -1
  43. package/dist/react/SchemaView.mjs +21 -9
  44. package/dist/react/fieldPath.d.mts +20 -0
  45. package/dist/react/fieldPath.mjs +81 -0
  46. package/dist/react/headless.d.mts +2 -4
  47. package/dist/react/headless.mjs +3 -492
  48. package/dist/react/headlessRenderers.d.mts +23 -0
  49. package/dist/react/headlessRenderers.mjs +507 -0
  50. package/dist/renderer-DseHeliw.d.mts +160 -0
  51. package/dist/themes/mantine.d.mts +1 -1
  52. package/dist/themes/mantine.mjs +2 -1
  53. package/dist/themes/mui.d.mts +1 -1
  54. package/dist/themes/mui.mjs +2 -1
  55. package/dist/themes/radix.d.mts +1 -1
  56. package/dist/themes/radix.mjs +2 -1
  57. package/dist/themes/shadcn.d.mts +1 -1
  58. package/dist/themes/shadcn.mjs +10 -6
  59. package/dist/typeInference-CRPqVwKu.d.mts +299 -0
  60. package/dist/types-ag2jYLqQ.d.mts +261 -0
  61. package/dist/version-CLchheaH.d.mts +40 -0
  62. package/package.json +1 -1
  63. package/dist/types-BJzEgJdX.d.mts +0 -335
@@ -1,335 +0,0 @@
1
- //#region src/core/renderer.d.ts
2
- /**
3
- * Properties available on every schema field, regardless of rendering target.
4
- * Both React and HTML renderers receive these.
5
- */
6
- interface BaseFieldProps {
7
- /** Current field value. */
8
- value: unknown;
9
- /** Whether to render as read-only display. */
10
- readOnly: boolean;
11
- /** Whether to render as an empty input. */
12
- writeOnly: boolean;
13
- /** Schema metadata for this field. */
14
- meta: SchemaMeta;
15
- /** Constraints from schema checks. */
16
- constraints: FieldConstraints;
17
- /** Dot-separated path from root (e.g. "address.city"). */
18
- path: string;
19
- /** For enums: the allowed values. */
20
- enumValues?: string[];
21
- /** For arrays: the element schema. */
22
- element?: WalkedField;
23
- /** For objects: map of field name → WalkedField. */
24
- fields?: Record<string, WalkedField>;
25
- /** For unions: the option schemas. */
26
- options?: WalkedField[];
27
- /** For discriminated unions: the discriminator key. */
28
- discriminator?: string;
29
- /** For records: key and value schemas. */
30
- keyType?: WalkedField;
31
- valueType?: WalkedField;
32
- /** Walked field tree for recursive rendering. */
33
- tree: WalkedField;
34
- }
35
- /**
36
- * Props for React render functions. Extends BaseFieldProps with:
37
- * - `onChange` — callback to propagate value changes back to state
38
- * - `renderChild` — recursively renders a child field, threading onChange
39
- */
40
- interface RenderProps extends BaseFieldProps {
41
- /** Callback to update the field value. */
42
- onChange: (value: unknown) => void;
43
- /**
44
- * Render a child field. Theme adapters call this to recursively render
45
- * nested structures (object fields, array elements, union options).
46
- * The resolver and rendering context are already wired in.
47
- */
48
- renderChild: (tree: WalkedField, value: unknown, onChange: (v: unknown) => void) => unknown;
49
- }
50
- /**
51
- * Props for HTML render functions. Extends BaseFieldProps with:
52
- * - `renderChild` — recursively renders a child field to HTML string
53
- *
54
- * No `onChange` — HTML rendering is pure output with no event handling.
55
- */
56
- interface HtmlRenderProps extends BaseFieldProps {
57
- /**
58
- * Render a child field to an HTML string. Theme adapters call this
59
- * to recursively render nested structures.
60
- *
61
- * @param tree - The walked field tree for the child
62
- * @param value - The child's current value
63
- * @param pathSuffix - Path segment from the parent (e.g. "city",
64
- * "[0]"). When omitted, the child's description is used as fallback.
65
- */
66
- renderChild: (tree: WalkedField, value: unknown, pathSuffix?: string) => string;
67
- }
68
- type RenderFunction = (props: RenderProps) => unknown;
69
- interface ComponentResolver {
70
- string?: RenderFunction;
71
- number?: RenderFunction;
72
- boolean?: RenderFunction;
73
- enum?: RenderFunction;
74
- object?: RenderFunction;
75
- array?: RenderFunction;
76
- record?: RenderFunction;
77
- union?: RenderFunction;
78
- discriminatedUnion?: RenderFunction;
79
- literal?: RenderFunction;
80
- file?: RenderFunction;
81
- unknown?: RenderFunction;
82
- }
83
- /** An HTML render function returns a string. */
84
- type HtmlRenderFunction = (props: HtmlRenderProps) => string;
85
- /**
86
- * HTML resolver — maps schema types to HTML string renderers.
87
- * Structurally mirrors ComponentResolver but produces strings.
88
- */
89
- interface HtmlResolver {
90
- string?: HtmlRenderFunction;
91
- number?: HtmlRenderFunction;
92
- boolean?: HtmlRenderFunction;
93
- enum?: HtmlRenderFunction;
94
- object?: HtmlRenderFunction;
95
- array?: HtmlRenderFunction;
96
- record?: HtmlRenderFunction;
97
- union?: HtmlRenderFunction;
98
- discriminatedUnion?: HtmlRenderFunction;
99
- literal?: HtmlRenderFunction;
100
- file?: HtmlRenderFunction;
101
- unknown?: HtmlRenderFunction;
102
- }
103
- declare const RESOLVER_KEYS: readonly ["string", "number", "boolean", "enum", "object", "array", "record", "union", "discriminatedUnion", "literal", "file", "unknown"];
104
- type ResolverKey = (typeof RESOLVER_KEYS)[number];
105
- /**
106
- * Map a schema type to the resolver key that handles it.
107
- * `discriminatedUnion` → `union`. Unknown types → `unknown`.
108
- */
109
- declare function typeToKey(type: WalkedField["type"]): ResolverKey;
110
- /**
111
- * Look up the render function for a schema type in a ComponentResolver.
112
- */
113
- declare function getRenderFunction(type: WalkedField["type"], resolver: ComponentResolver): RenderFunction | undefined;
114
- /**
115
- * Look up the render function for a schema type in an HtmlResolver.
116
- */
117
- declare function getHtmlRenderFn(type: WalkedField["type"], resolver: HtmlResolver): HtmlRenderFunction | undefined;
118
- /**
119
- * Merge two ComponentResolvers — user values take priority, fallback fills gaps.
120
- */
121
- declare function mergeResolvers(user: ComponentResolver, fallback: ComponentResolver): ComponentResolver;
122
- /**
123
- * Merge two HtmlResolvers — user values take priority, fallback fills gaps.
124
- */
125
- declare function mergeHtmlResolvers(user: HtmlResolver, fallback: HtmlResolver): HtmlResolver;
126
- //#endregion
127
- //#region src/core/types.d.ts
128
- /**
129
- * Core types for schema-components.
130
- *
131
- * These types define the vocabulary shared between the schema walker,
132
- * component resolver, and React components.
133
- */
134
- /** A raw JSON object (JSON Schema or OpenAPI document). */
135
- type JsonObject = Record<string, unknown>;
136
- /**
137
- * Metadata attached to schemas via `.meta()` or passed as props to
138
- * `<SchemaComponent>`. Every field is also available as a top-level
139
- * prop on `<SchemaComponent>` (with TypeScript-enforced exclusivity
140
- * between prop and `meta`).
141
- */
142
- interface SchemaMeta {
143
- readOnly?: boolean;
144
- writeOnly?: boolean;
145
- description?: string;
146
- title?: string;
147
- deprecated?: boolean;
148
- /** Component hint — resolved before theme adapter. */
149
- component?: string;
150
- /** Sort order for object fields. Lower values render first. */
151
- order?: number;
152
- /** Arbitrary UI hints passed through to theme adapters. */
153
- [key: string]: unknown;
154
- }
155
- type Editability = "presentation" | "input" | "editable";
156
- /**
157
- * Resolved editability state for a single field.
158
- *
159
- * Priority (highest wins):
160
- * 1. Property-level readOnly → presentation
161
- * 2. Property-level writeOnly → input
162
- * 3. Component-level readOnly → presentation
163
- * 4. Component-level writeOnly → input
164
- * 5. Schema root readOnly → presentation
165
- * 6. Schema root writeOnly → input
166
- * 7. Neither → editable
167
- */
168
- declare function resolveEditability(propertyMeta: SchemaMeta | undefined, componentMeta: SchemaMeta | undefined, rootMeta: SchemaMeta | undefined): Editability;
169
- /**
170
- * Recursive mapped type that mirrors a schema's shape for per-field
171
- * overrides. Each leaf accepts schema meta overrides and an optional
172
- * per-field validation error callback. Objects recurse and also accept
173
- * their own overrides.
174
- */
175
- type FieldOverrides<T> = { [K in keyof T]?: T[K] extends object ? FieldOverrides<T[K]> & FieldOverride : FieldOverride };
176
- /**
177
- * Per-field override. Extends SchemaMeta with rendering controls
178
- * and a per-field validation error callback.
179
- */
180
- type FieldOverride = Partial<SchemaMeta> & {
181
- /** Called with the ZodError when this field fails validation. */onValidationError?: (error: unknown) => void; /** Hide this field when false. Defaults to true (visible). */
182
- visible?: boolean;
183
- };
184
- type SchemaType = "string" | "number" | "boolean" | "null" | "enum" | "literal" | "object" | "array" | "record" | "union" | "discriminatedUnion" | "optional" | "nullable" | "default" | "readonly" | "pipe" | "lazy" | "file" | "unknown";
185
- interface WalkedField {
186
- type: SchemaType;
187
- editability: Editability;
188
- meta: SchemaMeta;
189
- /** For objects: map of field name → WalkedField. */
190
- fields?: Record<string, WalkedField>;
191
- /** For arrays: the element schema. */
192
- element?: WalkedField;
193
- /** For enums: the allowed values. */
194
- enumValues?: string[];
195
- /** For unions/discriminated unions: the options. */
196
- options?: WalkedField[];
197
- discriminator?: string;
198
- /** For records: key and value schemas. */
199
- keyType?: WalkedField;
200
- valueType?: WalkedField;
201
- /** For literals: the literal value(s). */
202
- literalValues?: (string | number | boolean | null)[];
203
- /** Whether the field is optional. */
204
- isOptional?: boolean;
205
- /** Whether the field is nullable. */
206
- isNullable?: boolean;
207
- /** Default value if present on the schema. */
208
- defaultValue?: unknown;
209
- /** Constraints from Zod checks (min, max, pattern, etc.). */
210
- constraints: FieldConstraints;
211
- }
212
- interface FieldConstraints {
213
- minLength?: number;
214
- maxLength?: number;
215
- minimum?: number;
216
- maximum?: number;
217
- pattern?: string;
218
- format?: string;
219
- mimeTypes?: string[];
220
- minItems?: number;
221
- maxItems?: number;
222
- }
223
- /**
224
- * Maps a JSON Schema structure to a TypeScript type.
225
- * Works with `as const` literals — provides full autocomplete for `fields`.
226
- */
227
- type FromJSONSchema<S> = S extends {
228
- type: "string";
229
- } ? string : S extends {
230
- type: "number" | "integer";
231
- } ? number : S extends {
232
- type: "boolean";
233
- } ? boolean : S extends {
234
- type: "null";
235
- } ? null : S extends {
236
- type: "array";
237
- items: infer I;
238
- } ? FromJSONSchema<I>[] : S extends {
239
- type: "object";
240
- properties: infer P;
241
- required?: infer R;
242
- } ? { [K in keyof P]: K extends R ? FromJSONSchema<P[K]> : FromJSONSchema<P[K]> | undefined } : unknown;
243
- /**
244
- * Resolves an OpenAPI `ref` string to its JSON Schema, then parses it.
245
- */
246
- type ResolveOpenAPIRef<Spec extends Record<string, unknown>, Ref extends string> = Ref extends `#/components/schemas/${infer Name}` ? Spec["components"] extends Record<string, unknown> ? Spec["components"]["schemas"] extends Record<string, unknown> ? Name extends keyof Spec["components"]["schemas"] ? FromJSONSchema<Spec["components"]["schemas"][Name]> : unknown : unknown : unknown : Ref extends `${string}/${string}` ? unknown : unknown;
247
- /** Navigate to a path item in an OpenAPI document. */
248
- type PathItemOf<Doc, Path extends string> = Doc extends {
249
- paths: Record<string, unknown>;
250
- } ? Path extends keyof Doc["paths"] ? Doc["paths"][Path] : unknown : unknown;
251
- /** Navigate to an operation within a path item. */
252
- type OperationOf<PathItem, Method extends string> = PathItem extends Record<string, unknown> ? Method extends keyof PathItem ? PathItem[Method] : unknown : unknown;
253
- /** Extract the schema from request body content. */
254
- type RequestBodySchemaOf<Op> = Op extends {
255
- requestBody: {
256
- content: {
257
- "application/json": {
258
- schema: infer S;
259
- };
260
- };
261
- };
262
- } ? S : Op extends {
263
- requestBody: {
264
- content: Record<string, {
265
- schema: infer S;
266
- }>;
267
- };
268
- } ? S : unknown;
269
- /** Extract the schema from response content. */
270
- type ResponseSchemaOf<Op, Status extends string> = Op extends {
271
- responses: Record<string, unknown>;
272
- } ? Status extends keyof Op["responses"] ? Op["responses"][Status] extends {
273
- content: {
274
- "application/json": {
275
- schema: infer S;
276
- };
277
- };
278
- } ? S : Op["responses"][Status] extends {
279
- content: Record<string, {
280
- schema: infer S;
281
- }>;
282
- } ? S : unknown : unknown : unknown;
283
- /** Resolve a schema that may be a $ref pointer. */
284
- type ResolveMaybeRef<Doc, S> = S extends {
285
- $ref: infer R extends string;
286
- } ? ResolveOpenAPIRef<Doc & Record<string, unknown>, R> : S extends Record<string, unknown> ? FromJSONSchema<S> : unknown;
287
- /** Extract parameter names from an operation. */
288
- type ParameterNamesOf<Doc, Path extends string, Method extends string> = OperationOf<PathItemOf<Doc, Path>, Method> extends {
289
- parameters: readonly unknown[];
290
- } ? OperationOf<PathItemOf<Doc, Path>, Method>["parameters"][number] extends {
291
- name: infer N;
292
- } ? N extends string ? N : never : never : never;
293
- /**
294
- * Infer the TypeScript type of an OpenAPI operation's request body.
295
- */
296
- type OpenAPIRequestBodyType<Doc, Path extends string, Method extends string> = ResolveMaybeRef<Doc, RequestBodySchemaOf<OperationOf<PathItemOf<Doc, Path>, Method>>>;
297
- /**
298
- * Infer the TypeScript type of an OpenAPI operation's response.
299
- */
300
- type OpenAPIResponseType<Doc, Path extends string, Method extends string, Status extends string> = ResolveMaybeRef<Doc, ResponseSchemaOf<OperationOf<PathItemOf<Doc, Path>, Method>, Status>>;
301
- /**
302
- * Infer the fields prop type for ApiRequestBody.
303
- * Falls back to Record<string, FieldOverride> for runtime documents.
304
- */
305
- type InferRequestBodyFields<Doc, Path extends string, Method extends string> = unknown extends OpenAPIRequestBodyType<Doc, Path, Method> ? Record<string, FieldOverride> : FieldOverrides<OpenAPIRequestBodyType<Doc, Path, Method>>;
306
- /**
307
- * Infer the fields prop type for ApiResponse.
308
- * Falls back to Record<string, FieldOverride> for runtime documents.
309
- */
310
- type InferResponseFields<Doc, Path extends string, Method extends string, Status extends string> = unknown extends OpenAPIResponseType<Doc, Path, Method, Status> ? Record<string, FieldOverride> : FieldOverrides<OpenAPIResponseType<Doc, Path, Method, Status>>;
311
- /**
312
- * Infer the overrides prop type for ApiParameters.
313
- * Falls back to Record<string, FieldOverride> for runtime documents.
314
- */
315
- type InferParameterOverrides<Doc, Path extends string, Method extends string> = string extends ParameterNamesOf<Doc, Path, Method> ? Record<string, FieldOverride> : Partial<Record<ParameterNamesOf<Doc, Path, Method>, FieldOverride>>;
316
- /**
317
- * Check if T is a "narrow" type (not wide like object, Record, or unknown).
318
- * Used to determine if we can enumerate keys for path inference.
319
- */
320
- type IsNarrowObject<T> = T extends string | number | boolean | null | undefined | unknown[] ? false : T extends object ? Record<string, never> extends T ? false : true : false;
321
- /**
322
- * Extract all valid dot-separated paths from an object type.
323
- * Produces paths like "name" | "address.city" | "address.postcode".
324
- * Stops at leaf types (string, number, boolean, null) and arrays.
325
- * Returns `string` for wide types (object, Record, unknown).
326
- * Handles optional/nullable fields by unwrapping T | undefined.
327
- */
328
- type PathOfType<T, Prefix extends string = ""> = IsNarrowObject<T> extends true ? { [K in keyof T & string]: T[K] extends string | number | boolean | null | undefined ? `${Prefix}${K}` : T[K] extends unknown[] ? `${Prefix}${K}` : T[K] extends object | undefined ? PathOfType<Exclude<T[K], undefined>, `${Prefix}${K}.`> | `${Prefix}${K}` : `${Prefix}${K}` }[keyof T & string] : string;
329
- /**
330
- * Extract the type at a given dot-separated path.
331
- * PathOfType<T> produces valid paths; TypeAtPath resolves the leaf type.
332
- */
333
- type TypeAtPath<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? TypeAtPath<T[Key], Rest> : unknown : P extends keyof T ? T[P] : unknown;
334
- //#endregion
335
- export { mergeResolvers as A, HtmlResolver as C, getHtmlRenderFn as D, RenderProps as E, getRenderFunction as O, HtmlRenderProps as S, RenderFunction as T, WalkedField as _, FromJSONSchema as a, ComponentResolver as b, InferResponseFields as c, OpenAPIResponseType as d, PathOfType as f, TypeAtPath as g, SchemaType as h, FieldOverrides as i, typeToKey as j, mergeHtmlResolvers as k, JsonObject as l, SchemaMeta as m, FieldConstraints as n, InferParameterOverrides as o, ResolveOpenAPIRef as p, FieldOverride as r, InferRequestBodyFields as s, Editability as t, OpenAPIRequestBodyType as u, resolveEditability as v, RESOLVER_KEYS as w, HtmlRenderFunction as x, BaseFieldProps as y };