json-schema-to-blocks 0.1.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/LICENSE +21 -0
- package/README.md +118 -0
- package/constraints.d.ts +10 -0
- package/constraints.js +31 -0
- package/convert.d.ts +18 -0
- package/convert.js +267 -0
- package/esm/constraints.js +28 -0
- package/esm/convert.js +263 -0
- package/esm/index.js +5 -0
- package/esm/rules.js +109 -0
- package/esm/schema.js +89 -0
- package/esm/types.js +8 -0
- package/index.d.ts +5 -0
- package/index.js +21 -0
- package/package.json +50 -0
- package/rules.d.ts +8 -0
- package/rules.js +113 -0
- package/schema.d.ts +20 -0
- package/schema.js +96 -0
- package/types.d.ts +110 -0
- package/types.js +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Constructive
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# json-schema-to-blocks
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
Lower a JSON Schema into a [`blocks-schema`](../blocks-schema) UI document: any
|
|
8
|
+
schema-described value gets a form with no hand-authored JSON. Pure functions,
|
|
9
|
+
no React — usable in servers, workers, and agents.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add json-schema-to-blocks blocks-schema
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Built with `makage` and published from `dist`, so deep imports resolve without
|
|
18
|
+
an exports map:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { schemaToDocument } from 'json-schema-to-blocks';
|
|
22
|
+
import { defaultWidgetRules } from 'json-schema-to-blocks/rules';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { schemaToDocument } from 'json-schema-to-blocks';
|
|
29
|
+
|
|
30
|
+
const document = schemaToDocument({
|
|
31
|
+
$id: 'invite-user',
|
|
32
|
+
title: 'Invite user',
|
|
33
|
+
type: 'object',
|
|
34
|
+
required: ['email'],
|
|
35
|
+
properties: {
|
|
36
|
+
email: { type: 'string', format: 'email' },
|
|
37
|
+
role: { type: 'string', enum: ['viewer', 'editor', 'admin'] },
|
|
38
|
+
notes: { type: 'string' },
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The result is an ordinary `UIDocument` — persist it, render it with
|
|
44
|
+
`blocks-renderer`, hand-edit it in the form builder, or layer overrides on it
|
|
45
|
+
with `composeDocument`.
|
|
46
|
+
|
|
47
|
+
## Lowering rules
|
|
48
|
+
|
|
49
|
+
| Schema | Node |
|
|
50
|
+
|--------|------|
|
|
51
|
+
| `properties` | `Section` per nested object, widget node per leaf, field `name` is the dot path |
|
|
52
|
+
| `required`, `minLength`, `maxLength`, `pattern`, `minimum`/`maximum`, `exclusiveMinimum`/`exclusiveMaximum` | `props.required` and `props.constraints` |
|
|
53
|
+
| `enum` | `RadioGroup` (≤ 3 values) or `Select`, with `props.options` |
|
|
54
|
+
| `boolean` | `Switch` |
|
|
55
|
+
| `number` / `integer` | `NumberInput` (`multipleOf` → `step`) |
|
|
56
|
+
| `string` + `format` | `DatePicker`, `DateTimePicker`, `TimePicker`, `PhoneInput`, `MarkdownEditor`, `CodeEditor`, `JsonEditor`, `FileUpload`, or `Input` with an `inputType` |
|
|
57
|
+
| `string` (unbounded or long `maxLength`) | `Textarea` |
|
|
58
|
+
| `array` | repeatable `Section` holding the item's nodes (`minItems`/`maxItems` carried through) |
|
|
59
|
+
| `oneOf` / `anyOf` | `Tabs` with one `Tab` per variant |
|
|
60
|
+
| `allOf` | flattened into the parent group |
|
|
61
|
+
| `$ref` (local) | resolved against `$defs`/`definitions`; unresolved refs lower to `JsonEditor` |
|
|
62
|
+
| `title`, `description`, `default`, `const`, `readOnly` | `label`, `description`, `defaultValue`, `disabled` |
|
|
63
|
+
|
|
64
|
+
## Composability
|
|
65
|
+
|
|
66
|
+
Widget selection is an ordered rule list, so an app prepends rules rather than
|
|
67
|
+
forking the converter — the first match wins:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { schemaToDocument } from 'json-schema-to-blocks';
|
|
71
|
+
|
|
72
|
+
const document = schemaToDocument(schema, {
|
|
73
|
+
rules: [
|
|
74
|
+
{
|
|
75
|
+
name: 'rich-text',
|
|
76
|
+
match: (ctx) => ctx.type === 'string' && ctx.schema.format === 'html',
|
|
77
|
+
node: 'MarkdownEditor',
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Pass `replaceDefaultRules: true` to drop the defaults entirely. Per-field
|
|
84
|
+
overrides live inline in the schema under `x-ui`:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"type": "string",
|
|
89
|
+
"x-ui": { "widget": "CodeEditor", "label": "Query", "order": 1, "props": { "language": "sql" } }
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`meta-to-blocks` (`_meta` → document) shares this rule/annotation shape, so a
|
|
94
|
+
widget preference set once applies to both database-derived and
|
|
95
|
+
schema-derived forms.
|
|
96
|
+
|
|
97
|
+
## Options
|
|
98
|
+
|
|
99
|
+
| Option | Default | Purpose |
|
|
100
|
+
|--------|---------|---------|
|
|
101
|
+
| `id` | schema `$id` or `'document'` | Document id |
|
|
102
|
+
| `rules` | — | Rules tried before the defaults |
|
|
103
|
+
| `replaceDefaultRules` | `false` | Use only the supplied rules |
|
|
104
|
+
| `form` | `true` | Wrap fields in a `Form` node |
|
|
105
|
+
| `submitLabel` | — | `Form` submit label |
|
|
106
|
+
| `rootKey` | `'page'` | Root node key |
|
|
107
|
+
| `includeReadOnly` | `true` | Emit `readOnly` properties as disabled fields |
|
|
108
|
+
|
|
109
|
+
## API
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
schemaToDocument(schema, options?) // → UIDocument
|
|
113
|
+
schemaToNodes(schema, options?) // → UINode[] (no envelope)
|
|
114
|
+
defaultWidgetRules // the ordered default rule list
|
|
115
|
+
toConstraints(schema) // validation keywords → UINodeConstraints
|
|
116
|
+
createResolver(root) // local $ref resolution
|
|
117
|
+
mergeAllOf(schema, resolve) // allOf flattening
|
|
118
|
+
```
|
package/constraints.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** JSON Schema validation keywords → `UINodeConstraints`. */
|
|
2
|
+
import type { UINodeConstraints } from 'blocks-schema';
|
|
3
|
+
import type { JSONSchema } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Exclusive bounds are tightened to the nearest representable inclusive bound
|
|
6
|
+
* for integers (a fractional bound floors/ceils first, so `exclusiveMinimum:
|
|
7
|
+
* 0.5` still admits `1`), and passed through for numbers — the document format
|
|
8
|
+
* only carries inclusive `minValue`/`maxValue`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function toConstraints(schema: JSONSchema): UINodeConstraints | undefined;
|
package/constraints.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** JSON Schema validation keywords → `UINodeConstraints`. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.toConstraints = toConstraints;
|
|
5
|
+
/**
|
|
6
|
+
* Exclusive bounds are tightened to the nearest representable inclusive bound
|
|
7
|
+
* for integers (a fractional bound floors/ceils first, so `exclusiveMinimum:
|
|
8
|
+
* 0.5` still admits `1`), and passed through for numbers — the document format
|
|
9
|
+
* only carries inclusive `minValue`/`maxValue`.
|
|
10
|
+
*/
|
|
11
|
+
function toConstraints(schema) {
|
|
12
|
+
const constraints = {};
|
|
13
|
+
if (schema.minLength != null)
|
|
14
|
+
constraints.minLength = schema.minLength;
|
|
15
|
+
if (schema.maxLength != null)
|
|
16
|
+
constraints.maxLength = schema.maxLength;
|
|
17
|
+
if (schema.pattern)
|
|
18
|
+
constraints.pattern = schema.pattern;
|
|
19
|
+
const isInteger = schema.type === 'integer' || (Array.isArray(schema.type) && schema.type.includes('integer'));
|
|
20
|
+
if (schema.minimum != null)
|
|
21
|
+
constraints.minValue = schema.minimum;
|
|
22
|
+
else if (schema.exclusiveMinimum != null) {
|
|
23
|
+
constraints.minValue = isInteger ? Math.floor(schema.exclusiveMinimum) + 1 : schema.exclusiveMinimum;
|
|
24
|
+
}
|
|
25
|
+
if (schema.maximum != null)
|
|
26
|
+
constraints.maxValue = schema.maximum;
|
|
27
|
+
else if (schema.exclusiveMaximum != null) {
|
|
28
|
+
constraints.maxValue = isInteger ? Math.ceil(schema.exclusiveMaximum) - 1 : schema.exclusiveMaximum;
|
|
29
|
+
}
|
|
30
|
+
return Object.keys(constraints).length > 0 ? constraints : undefined;
|
|
31
|
+
}
|
package/convert.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lowering itself: JSON Schema in, `blocks-schema` document out.
|
|
3
|
+
*
|
|
4
|
+
* Structure keywords become container nodes (`Section`, `Tabs`), leaf schemas
|
|
5
|
+
* become widget nodes chosen by the rule pipeline, and validation keywords
|
|
6
|
+
* become node constraints. Nothing here renders — the output is plain JSON that
|
|
7
|
+
* any `blocks-schema` adapter can draw.
|
|
8
|
+
*/
|
|
9
|
+
import { type UIDocument, type UINode } from 'blocks-schema';
|
|
10
|
+
import type { ConvertOptions, JSONSchema } from './types';
|
|
11
|
+
/** Lower a schema to a node tree without the document envelope. */
|
|
12
|
+
export declare function schemaToNodes(schema: JSONSchema, options?: ConvertOptions): UINode[];
|
|
13
|
+
/**
|
|
14
|
+
* Lower a JSON Schema to a UI document: a `Page` wrapping a `Form` of the
|
|
15
|
+
* schema's fields. The result is an ordinary document — it can be persisted,
|
|
16
|
+
* hand-edited, and composed over with `composeDocument`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function schemaToDocument(schema: JSONSchema, options?: ConvertOptions): UIDocument;
|
package/convert.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The lowering itself: JSON Schema in, `blocks-schema` document out.
|
|
4
|
+
*
|
|
5
|
+
* Structure keywords become container nodes (`Section`, `Tabs`), leaf schemas
|
|
6
|
+
* become widget nodes chosen by the rule pipeline, and validation keywords
|
|
7
|
+
* become node constraints. Nothing here renders — the output is plain JSON that
|
|
8
|
+
* any `blocks-schema` adapter can draw.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.schemaToNodes = schemaToNodes;
|
|
12
|
+
exports.schemaToDocument = schemaToDocument;
|
|
13
|
+
const blocks_schema_1 = require("blocks-schema");
|
|
14
|
+
const constraints_1 = require("./constraints");
|
|
15
|
+
const rules_1 = require("./rules");
|
|
16
|
+
const schema_1 = require("./schema");
|
|
17
|
+
/**
|
|
18
|
+
* `first_name`/`shippingCity` → `First name`. Sentence case, not title case,
|
|
19
|
+
* and all-caps words are left alone so acronyms survive (`apiURL` → `Api URL`).
|
|
20
|
+
*/
|
|
21
|
+
function titleize(name) {
|
|
22
|
+
if (!name)
|
|
23
|
+
return '';
|
|
24
|
+
const words = name
|
|
25
|
+
.replace(/[_-]+/g, ' ')
|
|
26
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
27
|
+
.trim()
|
|
28
|
+
.split(/\s+/)
|
|
29
|
+
.map((word) => (word === word.toUpperCase() ? word : word.toLowerCase()));
|
|
30
|
+
return words.join(' ').replace(/^./, (character) => character.toUpperCase());
|
|
31
|
+
}
|
|
32
|
+
function context(schema, name, path, required, lowering) {
|
|
33
|
+
return {
|
|
34
|
+
schema,
|
|
35
|
+
name,
|
|
36
|
+
path,
|
|
37
|
+
required,
|
|
38
|
+
type: (0, schema_1.primaryType)(schema),
|
|
39
|
+
ui: (0, schema_1.annotation)(schema),
|
|
40
|
+
resolve: lowering.resolve,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function applyRules(ctx, rules) {
|
|
44
|
+
for (const rule of rules) {
|
|
45
|
+
if (!rule.match(ctx))
|
|
46
|
+
continue;
|
|
47
|
+
const result = typeof rule.node === 'function' ? rule.node(ctx) : rule.node;
|
|
48
|
+
return typeof result === 'string' ? { type: result } : result;
|
|
49
|
+
}
|
|
50
|
+
return { type: 'Input' };
|
|
51
|
+
}
|
|
52
|
+
function widgetNode(ctx, lowering) {
|
|
53
|
+
const { schema, ui } = ctx;
|
|
54
|
+
const partial = applyRules(ctx, lowering.rules);
|
|
55
|
+
const constraints = { ...(0, constraints_1.toConstraints)(schema), ...partial.constraints };
|
|
56
|
+
// The document format only carries scalar defaults, so an object or array
|
|
57
|
+
// default is dropped rather than emitted as an invalid document.
|
|
58
|
+
const raw = schema.default ?? schema.const;
|
|
59
|
+
const defaultValue = isScalar(raw) ? raw : undefined;
|
|
60
|
+
return {
|
|
61
|
+
type: (partial.type ?? 'Input'),
|
|
62
|
+
key: ctx.path || 'field',
|
|
63
|
+
props: {
|
|
64
|
+
name: ctx.path,
|
|
65
|
+
label: ui.label ?? schema.title ?? titleize(ctx.name),
|
|
66
|
+
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
67
|
+
...(ui.placeholder ? { placeholder: ui.placeholder } : {}),
|
|
68
|
+
...(ctx.required ? { required: true } : {}),
|
|
69
|
+
...(ui.hidden ? { hidden: true } : {}),
|
|
70
|
+
...(ui.disabled || schema.readOnly ? { disabled: true } : {}),
|
|
71
|
+
...(ui.className ? { className: ui.className } : {}),
|
|
72
|
+
...((0, schema_1.isNullable)(schema) ? { nullable: true } : {}),
|
|
73
|
+
...(defaultValue !== undefined ? { defaultValue } : {}),
|
|
74
|
+
...(constraints && Object.keys(constraints).length > 0 ? { constraints } : {}),
|
|
75
|
+
...partial.props,
|
|
76
|
+
...ui.props,
|
|
77
|
+
},
|
|
78
|
+
children: partial.children ?? [],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function isScalar(value) {
|
|
82
|
+
return value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
|
|
83
|
+
}
|
|
84
|
+
function localRef(schema) {
|
|
85
|
+
const ref = schema?.$ref;
|
|
86
|
+
return typeof ref === 'string' && ref.startsWith('#/') ? ref : undefined;
|
|
87
|
+
}
|
|
88
|
+
/** An object schema only lowers to a group when it has fields to put in it. */
|
|
89
|
+
function hasFields(schema) {
|
|
90
|
+
return Object.keys(schema.properties ?? {}).length > 0;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A `$defs` entry that references itself (a tree node, a linked list) would
|
|
94
|
+
* lower forever, so re-entering the same ref on one path stops at a terminal
|
|
95
|
+
* JsonEditor instead.
|
|
96
|
+
*/
|
|
97
|
+
function guarded(ref, lowering, onCycle, lower) {
|
|
98
|
+
if (!ref)
|
|
99
|
+
return lower();
|
|
100
|
+
if (lowering.visiting.has(ref))
|
|
101
|
+
return onCycle();
|
|
102
|
+
lowering.visiting.add(ref);
|
|
103
|
+
try {
|
|
104
|
+
return lower();
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
lowering.visiting.delete(ref);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function cycleNode(ctx) {
|
|
111
|
+
return {
|
|
112
|
+
type: 'JsonEditor',
|
|
113
|
+
key: ctx.path || 'field',
|
|
114
|
+
props: {
|
|
115
|
+
name: ctx.path,
|
|
116
|
+
label: ctx.ui.label ?? ctx.schema.title ?? titleize(ctx.name),
|
|
117
|
+
...(ctx.required ? { required: true } : {}),
|
|
118
|
+
},
|
|
119
|
+
children: [],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function containerProps(ctx) {
|
|
123
|
+
const { schema, ui } = ctx;
|
|
124
|
+
return {
|
|
125
|
+
...(ctx.path ? { name: ctx.path } : {}),
|
|
126
|
+
...(ui.label ?? schema.title ?? ctx.name ? { label: ui.label ?? schema.title ?? titleize(ctx.name) } : {}),
|
|
127
|
+
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
128
|
+
...(ui.className ? { className: ui.className } : {}),
|
|
129
|
+
...ui.props,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** Variant unions become tabs: one tab per branch, the branch's fields inside. */
|
|
133
|
+
function variantNode(ctx, variants, lowering) {
|
|
134
|
+
return {
|
|
135
|
+
type: 'Tabs',
|
|
136
|
+
key: ctx.path || 'variants',
|
|
137
|
+
props: containerProps(ctx),
|
|
138
|
+
children: variants.map((rawVariant, index) => {
|
|
139
|
+
const variant = (0, schema_1.mergeAllOf)(lowering.resolve(rawVariant), lowering.resolve);
|
|
140
|
+
const variantCtx = context(variant, ctx.name, ctx.path, ctx.required, lowering);
|
|
141
|
+
return {
|
|
142
|
+
type: 'Tab',
|
|
143
|
+
key: `${ctx.path || 'variants'}.variant${index}`,
|
|
144
|
+
props: { label: (0, schema_1.annotation)(variant).label ?? variant.title ?? `Option ${index + 1}` },
|
|
145
|
+
children: guarded(localRef(rawVariant), lowering, () => [cycleNode(variantCtx)],
|
|
146
|
+
// A primitive branch has no properties, so it becomes the tab's
|
|
147
|
+
// single widget rather than an empty tab.
|
|
148
|
+
() => (hasFields(variant) ? childNodes(variant, ctx.path, lowering) : [nodeFor(variantCtx, lowering)])),
|
|
149
|
+
};
|
|
150
|
+
}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Arrays lower to a repeatable section holding the item's own nodes. */
|
|
154
|
+
function arrayNode(ctx, lowering) {
|
|
155
|
+
const rawItems = ctx.schema.items;
|
|
156
|
+
const items = rawItems ? (0, schema_1.mergeAllOf)(lowering.resolve(rawItems), lowering.resolve) : {};
|
|
157
|
+
const itemCtx = context(items, ctx.name, ctx.path, false, lowering);
|
|
158
|
+
const constraints = ctx.schema;
|
|
159
|
+
return {
|
|
160
|
+
type: 'Section',
|
|
161
|
+
key: ctx.path || 'items',
|
|
162
|
+
props: {
|
|
163
|
+
...containerProps(ctx),
|
|
164
|
+
repeatable: true,
|
|
165
|
+
...(constraints.minItems != null ? { minItems: constraints.minItems } : {}),
|
|
166
|
+
...(constraints.maxItems != null ? { maxItems: constraints.maxItems } : {}),
|
|
167
|
+
...(ctx.required ? { required: true } : {}),
|
|
168
|
+
},
|
|
169
|
+
children: guarded(localRef(rawItems), lowering, () => [cycleNode(itemCtx)], () => (hasFields(items) ? childNodes(items, ctx.path, lowering) : [widgetNode(itemCtx, lowering)])),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function nodeFor(ctx, lowering) {
|
|
173
|
+
const schema = ctx.schema;
|
|
174
|
+
const variants = schema.oneOf ?? schema.anyOf;
|
|
175
|
+
if (variants?.length && !ctx.ui.widget)
|
|
176
|
+
return variantNode(ctx, variants, lowering);
|
|
177
|
+
if (ctx.type === 'object' && hasFields(schema) && !ctx.ui.widget) {
|
|
178
|
+
return {
|
|
179
|
+
type: 'Section',
|
|
180
|
+
key: ctx.path || 'group',
|
|
181
|
+
props: containerProps(ctx),
|
|
182
|
+
children: childNodes(schema, ctx.path, lowering),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (ctx.type === 'array' && !ctx.ui.widget)
|
|
186
|
+
return arrayNode(ctx, lowering);
|
|
187
|
+
return widgetNode(ctx, lowering);
|
|
188
|
+
}
|
|
189
|
+
/** Lower an object schema's `properties`, honouring `x-ui.order`. */
|
|
190
|
+
function childNodes(schema, parentPath, lowering) {
|
|
191
|
+
const properties = schema.properties ?? {};
|
|
192
|
+
const required = new Set(schema.required ?? []);
|
|
193
|
+
const entries = Object.entries(properties)
|
|
194
|
+
.map(([name, rawChild], index) => {
|
|
195
|
+
const child = (0, schema_1.mergeAllOf)(lowering.resolve(rawChild), lowering.resolve);
|
|
196
|
+
return { name, child, ref: localRef(rawChild), index, order: (0, schema_1.annotation)(child).order };
|
|
197
|
+
})
|
|
198
|
+
.filter(({ child }) => lowering.includeReadOnly || !child.readOnly)
|
|
199
|
+
.sort((left, right) => {
|
|
200
|
+
if (left.order == null && right.order == null)
|
|
201
|
+
return left.index - right.index;
|
|
202
|
+
if (left.order == null)
|
|
203
|
+
return 1;
|
|
204
|
+
if (right.order == null)
|
|
205
|
+
return -1;
|
|
206
|
+
return left.order - right.order;
|
|
207
|
+
});
|
|
208
|
+
return entries.map(({ name, child, ref }) => {
|
|
209
|
+
const path = parentPath ? `${parentPath}.${name}` : name;
|
|
210
|
+
const ctx = context(child, name, path, required.has(name), lowering);
|
|
211
|
+
return guarded(ref, lowering, () => cycleNode(ctx), () => nodeFor(ctx, lowering));
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/** Lower a schema to a node tree without the document envelope. */
|
|
215
|
+
function schemaToNodes(schema, options = {}) {
|
|
216
|
+
const resolve = (0, schema_1.createResolver)(schema);
|
|
217
|
+
const lowering = {
|
|
218
|
+
rules: (0, rules_1.resolveRules)(options.rules, options.replaceDefaultRules),
|
|
219
|
+
resolve,
|
|
220
|
+
includeReadOnly: options.includeReadOnly ?? true,
|
|
221
|
+
visiting: new Set(),
|
|
222
|
+
};
|
|
223
|
+
const root = (0, schema_1.mergeAllOf)(resolve(schema), resolve);
|
|
224
|
+
if ((0, schema_1.primaryType)(root) === 'object' && hasFields(root) && !root.oneOf && !root.anyOf) {
|
|
225
|
+
return childNodes(root, '', lowering);
|
|
226
|
+
}
|
|
227
|
+
// A non-object root is a single value, so it lowers to one node.
|
|
228
|
+
return [nodeFor(context(root, root.title ?? 'value', 'value', true, lowering), lowering)];
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Lower a JSON Schema to a UI document: a `Page` wrapping a `Form` of the
|
|
232
|
+
* schema's fields. The result is an ordinary document — it can be persisted,
|
|
233
|
+
* hand-edited, and composed over with `composeDocument`.
|
|
234
|
+
*/
|
|
235
|
+
function schemaToDocument(schema, options = {}) {
|
|
236
|
+
const resolve = (0, schema_1.createResolver)(schema);
|
|
237
|
+
const root = (0, schema_1.mergeAllOf)(resolve(schema), resolve);
|
|
238
|
+
const fields = schemaToNodes(schema, options);
|
|
239
|
+
const rootKey = options.rootKey ?? 'page';
|
|
240
|
+
const title = (0, schema_1.annotation)(root).label ?? root.title;
|
|
241
|
+
const children = options.form === false
|
|
242
|
+
? fields
|
|
243
|
+
: [
|
|
244
|
+
{
|
|
245
|
+
type: 'Form',
|
|
246
|
+
key: 'form',
|
|
247
|
+
props: {
|
|
248
|
+
...(options.submitLabel ? { submitLabel: options.submitLabel } : {}),
|
|
249
|
+
},
|
|
250
|
+
children: fields,
|
|
251
|
+
},
|
|
252
|
+
];
|
|
253
|
+
return (0, blocks_schema_1.createDocument)({
|
|
254
|
+
type: 'Page',
|
|
255
|
+
key: rootKey,
|
|
256
|
+
props: {
|
|
257
|
+
...(title ? { title } : {}),
|
|
258
|
+
...(root.description ? { description: root.description } : {}),
|
|
259
|
+
},
|
|
260
|
+
children,
|
|
261
|
+
}, {
|
|
262
|
+
id: options.id ?? root.$id ?? 'document',
|
|
263
|
+
...(title || root.description
|
|
264
|
+
? { meta: { ...(title ? { title } : {}), ...(root.description ? { description: root.description } : {}) } }
|
|
265
|
+
: {}),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** JSON Schema validation keywords → `UINodeConstraints`. */
|
|
2
|
+
/**
|
|
3
|
+
* Exclusive bounds are tightened to the nearest representable inclusive bound
|
|
4
|
+
* for integers (a fractional bound floors/ceils first, so `exclusiveMinimum:
|
|
5
|
+
* 0.5` still admits `1`), and passed through for numbers — the document format
|
|
6
|
+
* only carries inclusive `minValue`/`maxValue`.
|
|
7
|
+
*/
|
|
8
|
+
export function toConstraints(schema) {
|
|
9
|
+
const constraints = {};
|
|
10
|
+
if (schema.minLength != null)
|
|
11
|
+
constraints.minLength = schema.minLength;
|
|
12
|
+
if (schema.maxLength != null)
|
|
13
|
+
constraints.maxLength = schema.maxLength;
|
|
14
|
+
if (schema.pattern)
|
|
15
|
+
constraints.pattern = schema.pattern;
|
|
16
|
+
const isInteger = schema.type === 'integer' || (Array.isArray(schema.type) && schema.type.includes('integer'));
|
|
17
|
+
if (schema.minimum != null)
|
|
18
|
+
constraints.minValue = schema.minimum;
|
|
19
|
+
else if (schema.exclusiveMinimum != null) {
|
|
20
|
+
constraints.minValue = isInteger ? Math.floor(schema.exclusiveMinimum) + 1 : schema.exclusiveMinimum;
|
|
21
|
+
}
|
|
22
|
+
if (schema.maximum != null)
|
|
23
|
+
constraints.maxValue = schema.maximum;
|
|
24
|
+
else if (schema.exclusiveMaximum != null) {
|
|
25
|
+
constraints.maxValue = isInteger ? Math.ceil(schema.exclusiveMaximum) - 1 : schema.exclusiveMaximum;
|
|
26
|
+
}
|
|
27
|
+
return Object.keys(constraints).length > 0 ? constraints : undefined;
|
|
28
|
+
}
|