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/esm/convert.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
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 { createDocument } from 'blocks-schema';
|
|
10
|
+
import { toConstraints } from './constraints';
|
|
11
|
+
import { resolveRules } from './rules';
|
|
12
|
+
import { annotation, createResolver, isNullable, mergeAllOf, primaryType } from './schema';
|
|
13
|
+
/**
|
|
14
|
+
* `first_name`/`shippingCity` → `First name`. Sentence case, not title case,
|
|
15
|
+
* and all-caps words are left alone so acronyms survive (`apiURL` → `Api URL`).
|
|
16
|
+
*/
|
|
17
|
+
function titleize(name) {
|
|
18
|
+
if (!name)
|
|
19
|
+
return '';
|
|
20
|
+
const words = name
|
|
21
|
+
.replace(/[_-]+/g, ' ')
|
|
22
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
23
|
+
.trim()
|
|
24
|
+
.split(/\s+/)
|
|
25
|
+
.map((word) => (word === word.toUpperCase() ? word : word.toLowerCase()));
|
|
26
|
+
return words.join(' ').replace(/^./, (character) => character.toUpperCase());
|
|
27
|
+
}
|
|
28
|
+
function context(schema, name, path, required, lowering) {
|
|
29
|
+
return {
|
|
30
|
+
schema,
|
|
31
|
+
name,
|
|
32
|
+
path,
|
|
33
|
+
required,
|
|
34
|
+
type: primaryType(schema),
|
|
35
|
+
ui: annotation(schema),
|
|
36
|
+
resolve: lowering.resolve,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function applyRules(ctx, rules) {
|
|
40
|
+
for (const rule of rules) {
|
|
41
|
+
if (!rule.match(ctx))
|
|
42
|
+
continue;
|
|
43
|
+
const result = typeof rule.node === 'function' ? rule.node(ctx) : rule.node;
|
|
44
|
+
return typeof result === 'string' ? { type: result } : result;
|
|
45
|
+
}
|
|
46
|
+
return { type: 'Input' };
|
|
47
|
+
}
|
|
48
|
+
function widgetNode(ctx, lowering) {
|
|
49
|
+
const { schema, ui } = ctx;
|
|
50
|
+
const partial = applyRules(ctx, lowering.rules);
|
|
51
|
+
const constraints = { ...toConstraints(schema), ...partial.constraints };
|
|
52
|
+
// The document format only carries scalar defaults, so an object or array
|
|
53
|
+
// default is dropped rather than emitted as an invalid document.
|
|
54
|
+
const raw = schema.default ?? schema.const;
|
|
55
|
+
const defaultValue = isScalar(raw) ? raw : undefined;
|
|
56
|
+
return {
|
|
57
|
+
type: (partial.type ?? 'Input'),
|
|
58
|
+
key: ctx.path || 'field',
|
|
59
|
+
props: {
|
|
60
|
+
name: ctx.path,
|
|
61
|
+
label: ui.label ?? schema.title ?? titleize(ctx.name),
|
|
62
|
+
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
63
|
+
...(ui.placeholder ? { placeholder: ui.placeholder } : {}),
|
|
64
|
+
...(ctx.required ? { required: true } : {}),
|
|
65
|
+
...(ui.hidden ? { hidden: true } : {}),
|
|
66
|
+
...(ui.disabled || schema.readOnly ? { disabled: true } : {}),
|
|
67
|
+
...(ui.className ? { className: ui.className } : {}),
|
|
68
|
+
...(isNullable(schema) ? { nullable: true } : {}),
|
|
69
|
+
...(defaultValue !== undefined ? { defaultValue } : {}),
|
|
70
|
+
...(constraints && Object.keys(constraints).length > 0 ? { constraints } : {}),
|
|
71
|
+
...partial.props,
|
|
72
|
+
...ui.props,
|
|
73
|
+
},
|
|
74
|
+
children: partial.children ?? [],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function isScalar(value) {
|
|
78
|
+
return value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
|
|
79
|
+
}
|
|
80
|
+
function localRef(schema) {
|
|
81
|
+
const ref = schema?.$ref;
|
|
82
|
+
return typeof ref === 'string' && ref.startsWith('#/') ? ref : undefined;
|
|
83
|
+
}
|
|
84
|
+
/** An object schema only lowers to a group when it has fields to put in it. */
|
|
85
|
+
function hasFields(schema) {
|
|
86
|
+
return Object.keys(schema.properties ?? {}).length > 0;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A `$defs` entry that references itself (a tree node, a linked list) would
|
|
90
|
+
* lower forever, so re-entering the same ref on one path stops at a terminal
|
|
91
|
+
* JsonEditor instead.
|
|
92
|
+
*/
|
|
93
|
+
function guarded(ref, lowering, onCycle, lower) {
|
|
94
|
+
if (!ref)
|
|
95
|
+
return lower();
|
|
96
|
+
if (lowering.visiting.has(ref))
|
|
97
|
+
return onCycle();
|
|
98
|
+
lowering.visiting.add(ref);
|
|
99
|
+
try {
|
|
100
|
+
return lower();
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
lowering.visiting.delete(ref);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function cycleNode(ctx) {
|
|
107
|
+
return {
|
|
108
|
+
type: 'JsonEditor',
|
|
109
|
+
key: ctx.path || 'field',
|
|
110
|
+
props: {
|
|
111
|
+
name: ctx.path,
|
|
112
|
+
label: ctx.ui.label ?? ctx.schema.title ?? titleize(ctx.name),
|
|
113
|
+
...(ctx.required ? { required: true } : {}),
|
|
114
|
+
},
|
|
115
|
+
children: [],
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function containerProps(ctx) {
|
|
119
|
+
const { schema, ui } = ctx;
|
|
120
|
+
return {
|
|
121
|
+
...(ctx.path ? { name: ctx.path } : {}),
|
|
122
|
+
...(ui.label ?? schema.title ?? ctx.name ? { label: ui.label ?? schema.title ?? titleize(ctx.name) } : {}),
|
|
123
|
+
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
124
|
+
...(ui.className ? { className: ui.className } : {}),
|
|
125
|
+
...ui.props,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Variant unions become tabs: one tab per branch, the branch's fields inside. */
|
|
129
|
+
function variantNode(ctx, variants, lowering) {
|
|
130
|
+
return {
|
|
131
|
+
type: 'Tabs',
|
|
132
|
+
key: ctx.path || 'variants',
|
|
133
|
+
props: containerProps(ctx),
|
|
134
|
+
children: variants.map((rawVariant, index) => {
|
|
135
|
+
const variant = mergeAllOf(lowering.resolve(rawVariant), lowering.resolve);
|
|
136
|
+
const variantCtx = context(variant, ctx.name, ctx.path, ctx.required, lowering);
|
|
137
|
+
return {
|
|
138
|
+
type: 'Tab',
|
|
139
|
+
key: `${ctx.path || 'variants'}.variant${index}`,
|
|
140
|
+
props: { label: annotation(variant).label ?? variant.title ?? `Option ${index + 1}` },
|
|
141
|
+
children: guarded(localRef(rawVariant), lowering, () => [cycleNode(variantCtx)],
|
|
142
|
+
// A primitive branch has no properties, so it becomes the tab's
|
|
143
|
+
// single widget rather than an empty tab.
|
|
144
|
+
() => (hasFields(variant) ? childNodes(variant, ctx.path, lowering) : [nodeFor(variantCtx, lowering)])),
|
|
145
|
+
};
|
|
146
|
+
}),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** Arrays lower to a repeatable section holding the item's own nodes. */
|
|
150
|
+
function arrayNode(ctx, lowering) {
|
|
151
|
+
const rawItems = ctx.schema.items;
|
|
152
|
+
const items = rawItems ? mergeAllOf(lowering.resolve(rawItems), lowering.resolve) : {};
|
|
153
|
+
const itemCtx = context(items, ctx.name, ctx.path, false, lowering);
|
|
154
|
+
const constraints = ctx.schema;
|
|
155
|
+
return {
|
|
156
|
+
type: 'Section',
|
|
157
|
+
key: ctx.path || 'items',
|
|
158
|
+
props: {
|
|
159
|
+
...containerProps(ctx),
|
|
160
|
+
repeatable: true,
|
|
161
|
+
...(constraints.minItems != null ? { minItems: constraints.minItems } : {}),
|
|
162
|
+
...(constraints.maxItems != null ? { maxItems: constraints.maxItems } : {}),
|
|
163
|
+
...(ctx.required ? { required: true } : {}),
|
|
164
|
+
},
|
|
165
|
+
children: guarded(localRef(rawItems), lowering, () => [cycleNode(itemCtx)], () => (hasFields(items) ? childNodes(items, ctx.path, lowering) : [widgetNode(itemCtx, lowering)])),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function nodeFor(ctx, lowering) {
|
|
169
|
+
const schema = ctx.schema;
|
|
170
|
+
const variants = schema.oneOf ?? schema.anyOf;
|
|
171
|
+
if (variants?.length && !ctx.ui.widget)
|
|
172
|
+
return variantNode(ctx, variants, lowering);
|
|
173
|
+
if (ctx.type === 'object' && hasFields(schema) && !ctx.ui.widget) {
|
|
174
|
+
return {
|
|
175
|
+
type: 'Section',
|
|
176
|
+
key: ctx.path || 'group',
|
|
177
|
+
props: containerProps(ctx),
|
|
178
|
+
children: childNodes(schema, ctx.path, lowering),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
if (ctx.type === 'array' && !ctx.ui.widget)
|
|
182
|
+
return arrayNode(ctx, lowering);
|
|
183
|
+
return widgetNode(ctx, lowering);
|
|
184
|
+
}
|
|
185
|
+
/** Lower an object schema's `properties`, honouring `x-ui.order`. */
|
|
186
|
+
function childNodes(schema, parentPath, lowering) {
|
|
187
|
+
const properties = schema.properties ?? {};
|
|
188
|
+
const required = new Set(schema.required ?? []);
|
|
189
|
+
const entries = Object.entries(properties)
|
|
190
|
+
.map(([name, rawChild], index) => {
|
|
191
|
+
const child = mergeAllOf(lowering.resolve(rawChild), lowering.resolve);
|
|
192
|
+
return { name, child, ref: localRef(rawChild), index, order: annotation(child).order };
|
|
193
|
+
})
|
|
194
|
+
.filter(({ child }) => lowering.includeReadOnly || !child.readOnly)
|
|
195
|
+
.sort((left, right) => {
|
|
196
|
+
if (left.order == null && right.order == null)
|
|
197
|
+
return left.index - right.index;
|
|
198
|
+
if (left.order == null)
|
|
199
|
+
return 1;
|
|
200
|
+
if (right.order == null)
|
|
201
|
+
return -1;
|
|
202
|
+
return left.order - right.order;
|
|
203
|
+
});
|
|
204
|
+
return entries.map(({ name, child, ref }) => {
|
|
205
|
+
const path = parentPath ? `${parentPath}.${name}` : name;
|
|
206
|
+
const ctx = context(child, name, path, required.has(name), lowering);
|
|
207
|
+
return guarded(ref, lowering, () => cycleNode(ctx), () => nodeFor(ctx, lowering));
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** Lower a schema to a node tree without the document envelope. */
|
|
211
|
+
export function schemaToNodes(schema, options = {}) {
|
|
212
|
+
const resolve = createResolver(schema);
|
|
213
|
+
const lowering = {
|
|
214
|
+
rules: resolveRules(options.rules, options.replaceDefaultRules),
|
|
215
|
+
resolve,
|
|
216
|
+
includeReadOnly: options.includeReadOnly ?? true,
|
|
217
|
+
visiting: new Set(),
|
|
218
|
+
};
|
|
219
|
+
const root = mergeAllOf(resolve(schema), resolve);
|
|
220
|
+
if (primaryType(root) === 'object' && hasFields(root) && !root.oneOf && !root.anyOf) {
|
|
221
|
+
return childNodes(root, '', lowering);
|
|
222
|
+
}
|
|
223
|
+
// A non-object root is a single value, so it lowers to one node.
|
|
224
|
+
return [nodeFor(context(root, root.title ?? 'value', 'value', true, lowering), lowering)];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Lower a JSON Schema to a UI document: a `Page` wrapping a `Form` of the
|
|
228
|
+
* schema's fields. The result is an ordinary document — it can be persisted,
|
|
229
|
+
* hand-edited, and composed over with `composeDocument`.
|
|
230
|
+
*/
|
|
231
|
+
export function schemaToDocument(schema, options = {}) {
|
|
232
|
+
const resolve = createResolver(schema);
|
|
233
|
+
const root = mergeAllOf(resolve(schema), resolve);
|
|
234
|
+
const fields = schemaToNodes(schema, options);
|
|
235
|
+
const rootKey = options.rootKey ?? 'page';
|
|
236
|
+
const title = annotation(root).label ?? root.title;
|
|
237
|
+
const children = options.form === false
|
|
238
|
+
? fields
|
|
239
|
+
: [
|
|
240
|
+
{
|
|
241
|
+
type: 'Form',
|
|
242
|
+
key: 'form',
|
|
243
|
+
props: {
|
|
244
|
+
...(options.submitLabel ? { submitLabel: options.submitLabel } : {}),
|
|
245
|
+
},
|
|
246
|
+
children: fields,
|
|
247
|
+
},
|
|
248
|
+
];
|
|
249
|
+
return createDocument({
|
|
250
|
+
type: 'Page',
|
|
251
|
+
key: rootKey,
|
|
252
|
+
props: {
|
|
253
|
+
...(title ? { title } : {}),
|
|
254
|
+
...(root.description ? { description: root.description } : {}),
|
|
255
|
+
},
|
|
256
|
+
children,
|
|
257
|
+
}, {
|
|
258
|
+
id: options.id ?? root.$id ?? 'document',
|
|
259
|
+
...(title || root.description
|
|
260
|
+
? { meta: { ...(title ? { title } : {}), ...(root.description ? { description: root.description } : {}) } }
|
|
261
|
+
: {}),
|
|
262
|
+
});
|
|
263
|
+
}
|
package/esm/index.js
ADDED
package/esm/rules.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default widget rules: the ordered list that decides which node type a schema
|
|
3
|
+
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
4
|
+
* forking the converter.
|
|
5
|
+
*/
|
|
6
|
+
import { primaryType } from './schema';
|
|
7
|
+
/** Formats that map straight onto a dedicated widget. */
|
|
8
|
+
const formatWidgets = {
|
|
9
|
+
'date-time': 'DateTimePicker',
|
|
10
|
+
date: 'DatePicker',
|
|
11
|
+
time: 'TimePicker',
|
|
12
|
+
email: 'Input',
|
|
13
|
+
uri: 'Input',
|
|
14
|
+
url: 'Input',
|
|
15
|
+
hostname: 'Input',
|
|
16
|
+
uuid: 'Input',
|
|
17
|
+
password: 'Input',
|
|
18
|
+
phone: 'PhoneInput',
|
|
19
|
+
tel: 'PhoneInput',
|
|
20
|
+
markdown: 'MarkdownEditor',
|
|
21
|
+
code: 'CodeEditor',
|
|
22
|
+
json: 'JsonEditor',
|
|
23
|
+
'data-url': 'FileUpload',
|
|
24
|
+
binary: 'FileUpload',
|
|
25
|
+
textarea: 'Textarea',
|
|
26
|
+
};
|
|
27
|
+
/** `format` values that also set the HTML input type. */
|
|
28
|
+
const inputTypes = {
|
|
29
|
+
email: 'email',
|
|
30
|
+
uri: 'url',
|
|
31
|
+
url: 'url',
|
|
32
|
+
password: 'password',
|
|
33
|
+
};
|
|
34
|
+
/** Long free text is a Textarea; the cutoff is a heuristic, overridable by `x-ui`. */
|
|
35
|
+
const TEXTAREA_MIN_LENGTH = 256;
|
|
36
|
+
export const defaultWidgetRules = [
|
|
37
|
+
{
|
|
38
|
+
name: 'annotation-widget',
|
|
39
|
+
match: (ctx) => Boolean(ctx.ui.widget),
|
|
40
|
+
node: (ctx) => ctx.ui.widget,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'enum',
|
|
44
|
+
match: (ctx) => Array.isArray(ctx.schema.enum) && ctx.schema.enum.length > 0,
|
|
45
|
+
node: (ctx) => {
|
|
46
|
+
const values = ctx.schema.enum ?? [];
|
|
47
|
+
return {
|
|
48
|
+
// A short enum reads better as radios than as a collapsed select.
|
|
49
|
+
type: values.length <= 3 ? 'RadioGroup' : 'Select',
|
|
50
|
+
props: { options: values.map((value) => ({ label: String(value), value })) },
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'boolean',
|
|
56
|
+
match: (ctx) => ctx.type === 'boolean',
|
|
57
|
+
node: 'Switch',
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'number',
|
|
61
|
+
match: (ctx) => ctx.type === 'number' || ctx.type === 'integer',
|
|
62
|
+
node: (ctx) => ({
|
|
63
|
+
type: 'NumberInput',
|
|
64
|
+
props: {
|
|
65
|
+
...(ctx.type === 'integer' ? { step: 1 } : {}),
|
|
66
|
+
...(typeof ctx.schema.multipleOf === 'number' ? { step: ctx.schema.multipleOf } : {}),
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'string-format',
|
|
72
|
+
match: (ctx) => ctx.type === 'string' && typeof ctx.schema.format === 'string',
|
|
73
|
+
node: (ctx) => {
|
|
74
|
+
const format = ctx.schema.format;
|
|
75
|
+
const type = formatWidgets[format] ?? 'Input';
|
|
76
|
+
const inputType = inputTypes[format];
|
|
77
|
+
return { type, ...(inputType ? { props: { inputType } } : {}) };
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: 'string-long',
|
|
82
|
+
match: (ctx) => ctx.type === 'string' && (ctx.schema.maxLength == null || ctx.schema.maxLength >= TEXTAREA_MIN_LENGTH),
|
|
83
|
+
node: 'Textarea',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'string',
|
|
87
|
+
match: (ctx) => ctx.type === 'string',
|
|
88
|
+
node: 'Input',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: 'structural',
|
|
92
|
+
// Objects and arrays only reach the rules when they carry no fields to
|
|
93
|
+
// lower, so raw JSON is the honest editor for them.
|
|
94
|
+
match: (ctx) => ctx.type === 'object' || ctx.type === 'array',
|
|
95
|
+
node: 'JsonEditor',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'unresolved',
|
|
99
|
+
// An unresolved `$ref` or a schema with no lowerable type still needs an
|
|
100
|
+
// editable surface, so it falls back to raw JSON rather than vanishing.
|
|
101
|
+
match: (ctx) => primaryType(ctx.schema) === undefined,
|
|
102
|
+
node: 'JsonEditor',
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
export function resolveRules(rules, replaceDefaults) {
|
|
106
|
+
if (replaceDefaults)
|
|
107
|
+
return rules ?? [];
|
|
108
|
+
return rules?.length ? [...rules, ...defaultWidgetRules] : defaultWidgetRules;
|
|
109
|
+
}
|
package/esm/schema.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** Schema-shape helpers shared by the rules and the converter. */
|
|
2
|
+
/**
|
|
3
|
+
* A union with `null` is how nullability is spelled in JSON Schema; the primary
|
|
4
|
+
* type is the non-null member so `['string', 'null']` still lowers to an Input.
|
|
5
|
+
*/
|
|
6
|
+
export function primaryType(schema) {
|
|
7
|
+
const { type } = schema;
|
|
8
|
+
if (Array.isArray(type))
|
|
9
|
+
return type.find((entry) => entry !== 'null');
|
|
10
|
+
if (type)
|
|
11
|
+
return type;
|
|
12
|
+
// A schema may describe its type only through structural keywords.
|
|
13
|
+
if (schema.properties)
|
|
14
|
+
return 'object';
|
|
15
|
+
if (schema.items)
|
|
16
|
+
return 'array';
|
|
17
|
+
if (schema.enum || schema.const !== undefined)
|
|
18
|
+
return 'string';
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
export function isNullable(schema) {
|
|
22
|
+
return Array.isArray(schema.type) ? schema.type.includes('null') : false;
|
|
23
|
+
}
|
|
24
|
+
export function annotation(schema) {
|
|
25
|
+
return schema['x-ui'] ?? {};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Resolve local `$ref`s (`#/$defs/x`, `#/definitions/x`) against the root.
|
|
29
|
+
* Remote refs are left alone: a resolver would need IO, and an unresolved ref
|
|
30
|
+
* lowers to a JsonEditor rather than failing the whole document.
|
|
31
|
+
*/
|
|
32
|
+
export function createResolver(root) {
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
function resolve(schema) {
|
|
35
|
+
const ref = schema.$ref;
|
|
36
|
+
if (!ref || !ref.startsWith('#/'))
|
|
37
|
+
return schema;
|
|
38
|
+
if (seen.has(ref))
|
|
39
|
+
return schema;
|
|
40
|
+
const target = pointer(root, ref);
|
|
41
|
+
if (!target)
|
|
42
|
+
return schema;
|
|
43
|
+
seen.add(ref);
|
|
44
|
+
try {
|
|
45
|
+
// Sibling keywords win over the referenced schema, per JSON Schema 2019-09.
|
|
46
|
+
const { $ref: _ref, ...rest } = schema;
|
|
47
|
+
return resolve({ ...target, ...rest });
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
seen.delete(ref);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return resolve;
|
|
54
|
+
}
|
|
55
|
+
function pointer(root, ref) {
|
|
56
|
+
let current = root;
|
|
57
|
+
for (const rawSegment of ref.slice(2).split('/')) {
|
|
58
|
+
const segment = rawSegment.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
59
|
+
if (!current || typeof current !== 'object')
|
|
60
|
+
return undefined;
|
|
61
|
+
current = current[segment];
|
|
62
|
+
}
|
|
63
|
+
return current && typeof current === 'object' ? current : undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Flatten `allOf` into its parent. Composition-by-intersection has no UI
|
|
67
|
+
* meaning: the fields of every member belong to the same group.
|
|
68
|
+
*/
|
|
69
|
+
export function mergeAllOf(schema, resolve) {
|
|
70
|
+
if (!schema.allOf?.length)
|
|
71
|
+
return schema;
|
|
72
|
+
const { allOf, ...base } = schema;
|
|
73
|
+
let merged = base;
|
|
74
|
+
for (const member of allOf) {
|
|
75
|
+
const resolved = mergeAllOf(resolve(member), resolve);
|
|
76
|
+
merged = {
|
|
77
|
+
...resolved,
|
|
78
|
+
...merged,
|
|
79
|
+
properties: { ...resolved.properties, ...merged.properties },
|
|
80
|
+
required: [...(resolved.required ?? []), ...(merged.required ?? [])],
|
|
81
|
+
'x-ui': { ...annotation(resolved), ...annotation(merged) },
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!merged.required?.length)
|
|
85
|
+
delete merged.required;
|
|
86
|
+
if (!merged.properties || Object.keys(merged.properties).length === 0)
|
|
87
|
+
delete merged.properties;
|
|
88
|
+
return merged;
|
|
89
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for lowering JSON Schema into `blocks-schema` UI documents.
|
|
3
|
+
*
|
|
4
|
+
* Only the keywords this package lowers are modelled; anything else is carried
|
|
5
|
+
* along untouched so a schema is never rejected for being richer than the
|
|
6
|
+
* lowering rules.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./constraints"), exports);
|
|
18
|
+
__exportStar(require("./convert"), exports);
|
|
19
|
+
__exportStar(require("./rules"), exports);
|
|
20
|
+
__exportStar(require("./schema"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "json-schema-to-blocks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lower JSON Schema into blocks-schema UI documents: composable widget rules, constraints, and x-ui overrides",
|
|
5
|
+
"private": false,
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://constructive-io.github.io/blocks/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/constructive-io/blocks.git",
|
|
11
|
+
"directory": "packages/json-schema-to-blocks"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/constructive-io/blocks/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"directory": "dist"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"types": "index.d.ts",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "makage clean && makage build-ts && makage assets",
|
|
26
|
+
"build:dev": "makage clean && makage build-ts --dev && makage assets",
|
|
27
|
+
"lint:types": "tsc --noEmit -p tsconfig.lint.json",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"clean": "makage clean"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"json-schema",
|
|
34
|
+
"form",
|
|
35
|
+
"ui-schema",
|
|
36
|
+
"blocks",
|
|
37
|
+
"renderer"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"blocks-schema": "workspace:^"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"makage": "^0.6.0",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"vitest": "^3.2.4"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=24.0.0"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/rules.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default widget rules: the ordered list that decides which node type a schema
|
|
3
|
+
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
4
|
+
* forking the converter.
|
|
5
|
+
*/
|
|
6
|
+
import type { WidgetRule } from './types';
|
|
7
|
+
export declare const defaultWidgetRules: WidgetRule[];
|
|
8
|
+
export declare function resolveRules(rules: WidgetRule[] | undefined, replaceDefaults: boolean | undefined): WidgetRule[];
|
package/rules.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Default widget rules: the ordered list that decides which node type a schema
|
|
4
|
+
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
5
|
+
* forking the converter.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.defaultWidgetRules = void 0;
|
|
9
|
+
exports.resolveRules = resolveRules;
|
|
10
|
+
const schema_1 = require("./schema");
|
|
11
|
+
/** Formats that map straight onto a dedicated widget. */
|
|
12
|
+
const formatWidgets = {
|
|
13
|
+
'date-time': 'DateTimePicker',
|
|
14
|
+
date: 'DatePicker',
|
|
15
|
+
time: 'TimePicker',
|
|
16
|
+
email: 'Input',
|
|
17
|
+
uri: 'Input',
|
|
18
|
+
url: 'Input',
|
|
19
|
+
hostname: 'Input',
|
|
20
|
+
uuid: 'Input',
|
|
21
|
+
password: 'Input',
|
|
22
|
+
phone: 'PhoneInput',
|
|
23
|
+
tel: 'PhoneInput',
|
|
24
|
+
markdown: 'MarkdownEditor',
|
|
25
|
+
code: 'CodeEditor',
|
|
26
|
+
json: 'JsonEditor',
|
|
27
|
+
'data-url': 'FileUpload',
|
|
28
|
+
binary: 'FileUpload',
|
|
29
|
+
textarea: 'Textarea',
|
|
30
|
+
};
|
|
31
|
+
/** `format` values that also set the HTML input type. */
|
|
32
|
+
const inputTypes = {
|
|
33
|
+
email: 'email',
|
|
34
|
+
uri: 'url',
|
|
35
|
+
url: 'url',
|
|
36
|
+
password: 'password',
|
|
37
|
+
};
|
|
38
|
+
/** Long free text is a Textarea; the cutoff is a heuristic, overridable by `x-ui`. */
|
|
39
|
+
const TEXTAREA_MIN_LENGTH = 256;
|
|
40
|
+
exports.defaultWidgetRules = [
|
|
41
|
+
{
|
|
42
|
+
name: 'annotation-widget',
|
|
43
|
+
match: (ctx) => Boolean(ctx.ui.widget),
|
|
44
|
+
node: (ctx) => ctx.ui.widget,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'enum',
|
|
48
|
+
match: (ctx) => Array.isArray(ctx.schema.enum) && ctx.schema.enum.length > 0,
|
|
49
|
+
node: (ctx) => {
|
|
50
|
+
const values = ctx.schema.enum ?? [];
|
|
51
|
+
return {
|
|
52
|
+
// A short enum reads better as radios than as a collapsed select.
|
|
53
|
+
type: values.length <= 3 ? 'RadioGroup' : 'Select',
|
|
54
|
+
props: { options: values.map((value) => ({ label: String(value), value })) },
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'boolean',
|
|
60
|
+
match: (ctx) => ctx.type === 'boolean',
|
|
61
|
+
node: 'Switch',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'number',
|
|
65
|
+
match: (ctx) => ctx.type === 'number' || ctx.type === 'integer',
|
|
66
|
+
node: (ctx) => ({
|
|
67
|
+
type: 'NumberInput',
|
|
68
|
+
props: {
|
|
69
|
+
...(ctx.type === 'integer' ? { step: 1 } : {}),
|
|
70
|
+
...(typeof ctx.schema.multipleOf === 'number' ? { step: ctx.schema.multipleOf } : {}),
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'string-format',
|
|
76
|
+
match: (ctx) => ctx.type === 'string' && typeof ctx.schema.format === 'string',
|
|
77
|
+
node: (ctx) => {
|
|
78
|
+
const format = ctx.schema.format;
|
|
79
|
+
const type = formatWidgets[format] ?? 'Input';
|
|
80
|
+
const inputType = inputTypes[format];
|
|
81
|
+
return { type, ...(inputType ? { props: { inputType } } : {}) };
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'string-long',
|
|
86
|
+
match: (ctx) => ctx.type === 'string' && (ctx.schema.maxLength == null || ctx.schema.maxLength >= TEXTAREA_MIN_LENGTH),
|
|
87
|
+
node: 'Textarea',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'string',
|
|
91
|
+
match: (ctx) => ctx.type === 'string',
|
|
92
|
+
node: 'Input',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: 'structural',
|
|
96
|
+
// Objects and arrays only reach the rules when they carry no fields to
|
|
97
|
+
// lower, so raw JSON is the honest editor for them.
|
|
98
|
+
match: (ctx) => ctx.type === 'object' || ctx.type === 'array',
|
|
99
|
+
node: 'JsonEditor',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: 'unresolved',
|
|
103
|
+
// An unresolved `$ref` or a schema with no lowerable type still needs an
|
|
104
|
+
// editable surface, so it falls back to raw JSON rather than vanishing.
|
|
105
|
+
match: (ctx) => (0, schema_1.primaryType)(ctx.schema) === undefined,
|
|
106
|
+
node: 'JsonEditor',
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
function resolveRules(rules, replaceDefaults) {
|
|
110
|
+
if (replaceDefaults)
|
|
111
|
+
return rules ?? [];
|
|
112
|
+
return rules?.length ? [...rules, ...exports.defaultWidgetRules] : exports.defaultWidgetRules;
|
|
113
|
+
}
|