json-schema-to-blocks 0.3.1 → 0.5.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/convert.js +26 -40
- package/esm/convert.js +26 -40
- package/esm/rules.js +23 -17
- package/package.json +4 -3
- package/rules.d.ts +5 -0
- package/rules.js +23 -17
- package/types.d.ts +17 -38
package/convert.js
CHANGED
|
@@ -11,6 +11,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
11
11
|
exports.schemaToNodes = schemaToNodes;
|
|
12
12
|
exports.schemaToDocument = schemaToDocument;
|
|
13
13
|
const blocks_schema_1 = require("blocks-schema");
|
|
14
|
+
const json_renderer_1 = require("json-renderer");
|
|
14
15
|
const constraints_1 = require("./constraints");
|
|
15
16
|
const rules_1 = require("./rules");
|
|
16
17
|
const schema_1 = require("./schema");
|
|
@@ -30,48 +31,41 @@ function titleize(name) {
|
|
|
30
31
|
return words.join(' ').replace(/^./, (character) => character.toUpperCase());
|
|
31
32
|
}
|
|
32
33
|
function context(schema, name, path, required, lowering) {
|
|
34
|
+
const ui = (0, schema_1.annotation)(schema);
|
|
35
|
+
// The document format only carries scalar defaults, so an object or array
|
|
36
|
+
// default is dropped rather than emitted as an invalid document.
|
|
37
|
+
const raw = schema.default ?? schema.const;
|
|
38
|
+
const constraints = (0, constraints_1.toConstraints)(schema);
|
|
33
39
|
return {
|
|
34
|
-
schema,
|
|
35
40
|
name,
|
|
36
41
|
path,
|
|
37
42
|
required,
|
|
43
|
+
dataType: (0, schema_1.primaryType)(schema),
|
|
44
|
+
...(typeof schema.format === 'string' ? { format: schema.format } : {}),
|
|
45
|
+
label: schema.title ?? titleize(name),
|
|
46
|
+
...(schema.description !== undefined ? { description: schema.description } : {}),
|
|
47
|
+
...(schema.enum ? { enumValues: schema.enum } : {}),
|
|
48
|
+
nullable: (0, schema_1.isNullable)(schema),
|
|
49
|
+
readOnly: schema.readOnly ?? false,
|
|
50
|
+
...(isScalar(raw) ? { defaultValue: raw } : {}),
|
|
51
|
+
...(constraints ? { constraints } : {}),
|
|
52
|
+
hints: ui,
|
|
53
|
+
schema,
|
|
38
54
|
type: (0, schema_1.primaryType)(schema),
|
|
39
|
-
ui
|
|
55
|
+
ui,
|
|
40
56
|
resolve: lowering.resolve,
|
|
41
57
|
};
|
|
42
58
|
}
|
|
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
59
|
function widgetNode(ctx, lowering) {
|
|
53
|
-
const {
|
|
54
|
-
const partial =
|
|
55
|
-
const 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
|
+
const { ui } = ctx;
|
|
61
|
+
const partial = (0, json_renderer_1.applyWidgetRules)(ctx, lowering.rules, 'Input');
|
|
62
|
+
const constraints = { ...ctx.constraints, ...partial.constraints };
|
|
60
63
|
return {
|
|
61
64
|
type: (partial.type ?? 'Input'),
|
|
62
65
|
key: ctx.path || 'field',
|
|
63
66
|
props: {
|
|
64
|
-
|
|
65
|
-
|
|
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 } : {}),
|
|
67
|
+
...(0, json_renderer_1.fieldNodeProps)(ctx),
|
|
68
|
+
...(Object.keys(constraints).length > 0 ? { constraints } : {}),
|
|
75
69
|
...partial.props,
|
|
76
70
|
...ui.props,
|
|
77
71
|
},
|
|
@@ -113,7 +107,7 @@ function cycleNode(ctx) {
|
|
|
113
107
|
key: ctx.path || 'field',
|
|
114
108
|
props: {
|
|
115
109
|
name: ctx.path,
|
|
116
|
-
label: ctx.ui.label ?? ctx.
|
|
110
|
+
label: ctx.ui.label ?? ctx.label,
|
|
117
111
|
...(ctx.required ? { required: true } : {}),
|
|
118
112
|
},
|
|
119
113
|
children: [],
|
|
@@ -123,7 +117,7 @@ function containerProps(ctx) {
|
|
|
123
117
|
const { schema, ui } = ctx;
|
|
124
118
|
return {
|
|
125
119
|
...(ctx.path ? { name: ctx.path } : {}),
|
|
126
|
-
...(ui.label ??
|
|
120
|
+
...(ui.label ?? ctx.label ? { label: ui.label ?? ctx.label } : {}),
|
|
127
121
|
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
128
122
|
...(ui.className ? { className: ui.className } : {}),
|
|
129
123
|
...ui.props,
|
|
@@ -196,15 +190,7 @@ function childNodes(schema, parentPath, lowering) {
|
|
|
196
190
|
return { name, child, ref: localRef(rawChild), index, order: (0, schema_1.annotation)(child).order };
|
|
197
191
|
})
|
|
198
192
|
.filter(({ child }) => lowering.includeReadOnly || !child.readOnly)
|
|
199
|
-
.sort(
|
|
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
|
-
});
|
|
193
|
+
.sort(json_renderer_1.compareFieldOrder);
|
|
208
194
|
return entries.map(({ name, child, ref }) => {
|
|
209
195
|
const path = parentPath ? `${parentPath}.${name}` : name;
|
|
210
196
|
const ctx = context(child, name, path, required.has(name), lowering);
|
package/esm/convert.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* any `blocks-schema` adapter can draw.
|
|
8
8
|
*/
|
|
9
9
|
import { createDocument } from 'blocks-schema';
|
|
10
|
+
import { applyWidgetRules, compareFieldOrder, fieldNodeProps } from 'json-renderer';
|
|
10
11
|
import { toConstraints } from './constraints';
|
|
11
12
|
import { resolveRules } from './rules';
|
|
12
13
|
import { annotation, createResolver, isNullable, mergeAllOf, primaryType } from './schema';
|
|
@@ -26,48 +27,41 @@ function titleize(name) {
|
|
|
26
27
|
return words.join(' ').replace(/^./, (character) => character.toUpperCase());
|
|
27
28
|
}
|
|
28
29
|
function context(schema, name, path, required, lowering) {
|
|
30
|
+
const ui = annotation(schema);
|
|
31
|
+
// The document format only carries scalar defaults, so an object or array
|
|
32
|
+
// default is dropped rather than emitted as an invalid document.
|
|
33
|
+
const raw = schema.default ?? schema.const;
|
|
34
|
+
const constraints = toConstraints(schema);
|
|
29
35
|
return {
|
|
30
|
-
schema,
|
|
31
36
|
name,
|
|
32
37
|
path,
|
|
33
38
|
required,
|
|
39
|
+
dataType: primaryType(schema),
|
|
40
|
+
...(typeof schema.format === 'string' ? { format: schema.format } : {}),
|
|
41
|
+
label: schema.title ?? titleize(name),
|
|
42
|
+
...(schema.description !== undefined ? { description: schema.description } : {}),
|
|
43
|
+
...(schema.enum ? { enumValues: schema.enum } : {}),
|
|
44
|
+
nullable: isNullable(schema),
|
|
45
|
+
readOnly: schema.readOnly ?? false,
|
|
46
|
+
...(isScalar(raw) ? { defaultValue: raw } : {}),
|
|
47
|
+
...(constraints ? { constraints } : {}),
|
|
48
|
+
hints: ui,
|
|
49
|
+
schema,
|
|
34
50
|
type: primaryType(schema),
|
|
35
|
-
ui
|
|
51
|
+
ui,
|
|
36
52
|
resolve: lowering.resolve,
|
|
37
53
|
};
|
|
38
54
|
}
|
|
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
55
|
function widgetNode(ctx, lowering) {
|
|
49
|
-
const {
|
|
50
|
-
const partial =
|
|
51
|
-
const 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
|
+
const { ui } = ctx;
|
|
57
|
+
const partial = applyWidgetRules(ctx, lowering.rules, 'Input');
|
|
58
|
+
const constraints = { ...ctx.constraints, ...partial.constraints };
|
|
56
59
|
return {
|
|
57
60
|
type: (partial.type ?? 'Input'),
|
|
58
61
|
key: ctx.path || 'field',
|
|
59
62
|
props: {
|
|
60
|
-
|
|
61
|
-
|
|
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 } : {}),
|
|
63
|
+
...fieldNodeProps(ctx),
|
|
64
|
+
...(Object.keys(constraints).length > 0 ? { constraints } : {}),
|
|
71
65
|
...partial.props,
|
|
72
66
|
...ui.props,
|
|
73
67
|
},
|
|
@@ -109,7 +103,7 @@ function cycleNode(ctx) {
|
|
|
109
103
|
key: ctx.path || 'field',
|
|
110
104
|
props: {
|
|
111
105
|
name: ctx.path,
|
|
112
|
-
label: ctx.ui.label ?? ctx.
|
|
106
|
+
label: ctx.ui.label ?? ctx.label,
|
|
113
107
|
...(ctx.required ? { required: true } : {}),
|
|
114
108
|
},
|
|
115
109
|
children: [],
|
|
@@ -119,7 +113,7 @@ function containerProps(ctx) {
|
|
|
119
113
|
const { schema, ui } = ctx;
|
|
120
114
|
return {
|
|
121
115
|
...(ctx.path ? { name: ctx.path } : {}),
|
|
122
|
-
...(ui.label ??
|
|
116
|
+
...(ui.label ?? ctx.label ? { label: ui.label ?? ctx.label } : {}),
|
|
123
117
|
...(ui.description ?? schema.description ? { description: ui.description ?? schema.description } : {}),
|
|
124
118
|
...(ui.className ? { className: ui.className } : {}),
|
|
125
119
|
...ui.props,
|
|
@@ -192,15 +186,7 @@ function childNodes(schema, parentPath, lowering) {
|
|
|
192
186
|
return { name, child, ref: localRef(rawChild), index, order: annotation(child).order };
|
|
193
187
|
})
|
|
194
188
|
.filter(({ child }) => lowering.includeReadOnly || !child.readOnly)
|
|
195
|
-
.sort(
|
|
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
|
-
});
|
|
189
|
+
.sort(compareFieldOrder);
|
|
204
190
|
return entries.map(({ name, child, ref }) => {
|
|
205
191
|
const path = parentPath ? `${parentPath}.${name}` : name;
|
|
206
192
|
const ctx = context(child, name, path, required.has(name), lowering);
|
package/esm/rules.js
CHANGED
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
* Default widget rules: the ordered list that decides which node type a schema
|
|
3
3
|
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
4
4
|
* forking the converter.
|
|
5
|
+
*
|
|
6
|
+
* They read the generic field descriptor (`dataType`, `format`, `enumValues`,
|
|
7
|
+
* `constraints`) wherever the decision is source-neutral, so the same rule shape
|
|
8
|
+
* works for a document lowered from database metadata or a task's input schema;
|
|
9
|
+
* only genuinely schema-specific decisions reach for `ctx.schema`.
|
|
5
10
|
*/
|
|
6
|
-
import {
|
|
11
|
+
import { composeWidgetRules } from 'json-renderer';
|
|
7
12
|
/** Formats that map straight onto a dedicated widget. */
|
|
8
13
|
const formatWidgets = {
|
|
9
14
|
'date-time': 'DateTimePicker',
|
|
@@ -36,14 +41,14 @@ const TEXTAREA_MIN_LENGTH = 256;
|
|
|
36
41
|
export const defaultWidgetRules = [
|
|
37
42
|
{
|
|
38
43
|
name: 'annotation-widget',
|
|
39
|
-
match: (ctx) => Boolean(ctx.
|
|
40
|
-
node: (ctx) => ctx.
|
|
44
|
+
match: (ctx) => Boolean(ctx.hints.widget),
|
|
45
|
+
node: (ctx) => ctx.hints.widget,
|
|
41
46
|
},
|
|
42
47
|
{
|
|
43
48
|
name: 'enum',
|
|
44
|
-
match: (ctx) =>
|
|
49
|
+
match: (ctx) => Boolean(ctx.enumValues?.length),
|
|
45
50
|
node: (ctx) => {
|
|
46
|
-
const values = ctx.
|
|
51
|
+
const values = ctx.enumValues ?? [];
|
|
47
52
|
return {
|
|
48
53
|
// A short enum reads better as radios than as a collapsed select.
|
|
49
54
|
type: values.length <= 3 ? 'RadioGroup' : 'Select',
|
|
@@ -53,25 +58,25 @@ export const defaultWidgetRules = [
|
|
|
53
58
|
},
|
|
54
59
|
{
|
|
55
60
|
name: 'boolean',
|
|
56
|
-
match: (ctx) => ctx.
|
|
61
|
+
match: (ctx) => ctx.dataType === 'boolean',
|
|
57
62
|
node: 'Switch',
|
|
58
63
|
},
|
|
59
64
|
{
|
|
60
65
|
name: 'number',
|
|
61
|
-
match: (ctx) => ctx.
|
|
66
|
+
match: (ctx) => ctx.dataType === 'number' || ctx.dataType === 'integer',
|
|
62
67
|
node: (ctx) => ({
|
|
63
68
|
type: 'NumberInput',
|
|
64
69
|
props: {
|
|
65
|
-
...(ctx.
|
|
70
|
+
...(ctx.dataType === 'integer' ? { step: 1 } : {}),
|
|
66
71
|
...(typeof ctx.schema.multipleOf === 'number' ? { step: ctx.schema.multipleOf } : {}),
|
|
67
72
|
},
|
|
68
73
|
}),
|
|
69
74
|
},
|
|
70
75
|
{
|
|
71
76
|
name: 'string-format',
|
|
72
|
-
match: (ctx) => ctx.
|
|
77
|
+
match: (ctx) => ctx.dataType === 'string' && typeof ctx.format === 'string',
|
|
73
78
|
node: (ctx) => {
|
|
74
|
-
const format = ctx.
|
|
79
|
+
const format = ctx.format;
|
|
75
80
|
const type = formatWidgets[format] ?? 'Input';
|
|
76
81
|
const inputType = inputTypes[format];
|
|
77
82
|
return { type, ...(inputType ? { props: { inputType } } : {}) };
|
|
@@ -79,31 +84,32 @@ export const defaultWidgetRules = [
|
|
|
79
84
|
},
|
|
80
85
|
{
|
|
81
86
|
name: 'string-long',
|
|
82
|
-
match: (ctx) =>
|
|
87
|
+
match: (ctx) => {
|
|
88
|
+
const maxLength = ctx.constraints?.maxLength;
|
|
89
|
+
return ctx.dataType === 'string' && (maxLength == null || maxLength >= TEXTAREA_MIN_LENGTH);
|
|
90
|
+
},
|
|
83
91
|
node: 'Textarea',
|
|
84
92
|
},
|
|
85
93
|
{
|
|
86
94
|
name: 'string',
|
|
87
|
-
match: (ctx) => ctx.
|
|
95
|
+
match: (ctx) => ctx.dataType === 'string',
|
|
88
96
|
node: 'Input',
|
|
89
97
|
},
|
|
90
98
|
{
|
|
91
99
|
name: 'structural',
|
|
92
100
|
// Objects and arrays only reach the rules when they carry no fields to
|
|
93
101
|
// lower, so raw JSON is the honest editor for them.
|
|
94
|
-
match: (ctx) => ctx.
|
|
102
|
+
match: (ctx) => ctx.dataType === 'object' || ctx.dataType === 'array',
|
|
95
103
|
node: 'JsonEditor',
|
|
96
104
|
},
|
|
97
105
|
{
|
|
98
106
|
name: 'unresolved',
|
|
99
107
|
// An unresolved `$ref` or a schema with no lowerable type still needs an
|
|
100
108
|
// editable surface, so it falls back to raw JSON rather than vanishing.
|
|
101
|
-
match: (ctx) =>
|
|
109
|
+
match: (ctx) => ctx.dataType === undefined,
|
|
102
110
|
node: 'JsonEditor',
|
|
103
111
|
},
|
|
104
112
|
];
|
|
105
113
|
export function resolveRules(rules, replaceDefaults) {
|
|
106
|
-
|
|
107
|
-
return rules ?? [];
|
|
108
|
-
return rules?.length ? [...rules, ...defaultWidgetRules] : defaultWidgetRules;
|
|
114
|
+
return composeWidgetRules(defaultWidgetRules, rules, replaceDefaults);
|
|
109
115
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-schema-to-blocks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Lower JSON Schema into blocks-schema UI documents: composable widget rules, constraints, and x-ui overrides",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"renderer"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"blocks-schema": "^0.
|
|
41
|
+
"blocks-schema": "^0.5.1",
|
|
42
|
+
"json-renderer": "^0.4.0"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"makage": "^0.6.0",
|
|
@@ -48,5 +49,5 @@
|
|
|
48
49
|
"engines": {
|
|
49
50
|
"node": ">=24.0.0"
|
|
50
51
|
},
|
|
51
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "70333e34de6ec02829cca256e9c6183b35bc12e7"
|
|
52
53
|
}
|
package/rules.d.ts
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* Default widget rules: the ordered list that decides which node type a schema
|
|
3
3
|
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
4
4
|
* forking the converter.
|
|
5
|
+
*
|
|
6
|
+
* They read the generic field descriptor (`dataType`, `format`, `enumValues`,
|
|
7
|
+
* `constraints`) wherever the decision is source-neutral, so the same rule shape
|
|
8
|
+
* works for a document lowered from database metadata or a task's input schema;
|
|
9
|
+
* only genuinely schema-specific decisions reach for `ctx.schema`.
|
|
5
10
|
*/
|
|
6
11
|
import type { WidgetRule } from './types';
|
|
7
12
|
export declare const defaultWidgetRules: WidgetRule[];
|
package/rules.js
CHANGED
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
* Default widget rules: the ordered list that decides which node type a schema
|
|
4
4
|
* position lowers to. Rules are data, so an app prepends its own instead of
|
|
5
5
|
* forking the converter.
|
|
6
|
+
*
|
|
7
|
+
* They read the generic field descriptor (`dataType`, `format`, `enumValues`,
|
|
8
|
+
* `constraints`) wherever the decision is source-neutral, so the same rule shape
|
|
9
|
+
* works for a document lowered from database metadata or a task's input schema;
|
|
10
|
+
* only genuinely schema-specific decisions reach for `ctx.schema`.
|
|
6
11
|
*/
|
|
7
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
13
|
exports.defaultWidgetRules = void 0;
|
|
9
14
|
exports.resolveRules = resolveRules;
|
|
10
|
-
const
|
|
15
|
+
const json_renderer_1 = require("json-renderer");
|
|
11
16
|
/** Formats that map straight onto a dedicated widget. */
|
|
12
17
|
const formatWidgets = {
|
|
13
18
|
'date-time': 'DateTimePicker',
|
|
@@ -40,14 +45,14 @@ const TEXTAREA_MIN_LENGTH = 256;
|
|
|
40
45
|
exports.defaultWidgetRules = [
|
|
41
46
|
{
|
|
42
47
|
name: 'annotation-widget',
|
|
43
|
-
match: (ctx) => Boolean(ctx.
|
|
44
|
-
node: (ctx) => ctx.
|
|
48
|
+
match: (ctx) => Boolean(ctx.hints.widget),
|
|
49
|
+
node: (ctx) => ctx.hints.widget,
|
|
45
50
|
},
|
|
46
51
|
{
|
|
47
52
|
name: 'enum',
|
|
48
|
-
match: (ctx) =>
|
|
53
|
+
match: (ctx) => Boolean(ctx.enumValues?.length),
|
|
49
54
|
node: (ctx) => {
|
|
50
|
-
const values = ctx.
|
|
55
|
+
const values = ctx.enumValues ?? [];
|
|
51
56
|
return {
|
|
52
57
|
// A short enum reads better as radios than as a collapsed select.
|
|
53
58
|
type: values.length <= 3 ? 'RadioGroup' : 'Select',
|
|
@@ -57,25 +62,25 @@ exports.defaultWidgetRules = [
|
|
|
57
62
|
},
|
|
58
63
|
{
|
|
59
64
|
name: 'boolean',
|
|
60
|
-
match: (ctx) => ctx.
|
|
65
|
+
match: (ctx) => ctx.dataType === 'boolean',
|
|
61
66
|
node: 'Switch',
|
|
62
67
|
},
|
|
63
68
|
{
|
|
64
69
|
name: 'number',
|
|
65
|
-
match: (ctx) => ctx.
|
|
70
|
+
match: (ctx) => ctx.dataType === 'number' || ctx.dataType === 'integer',
|
|
66
71
|
node: (ctx) => ({
|
|
67
72
|
type: 'NumberInput',
|
|
68
73
|
props: {
|
|
69
|
-
...(ctx.
|
|
74
|
+
...(ctx.dataType === 'integer' ? { step: 1 } : {}),
|
|
70
75
|
...(typeof ctx.schema.multipleOf === 'number' ? { step: ctx.schema.multipleOf } : {}),
|
|
71
76
|
},
|
|
72
77
|
}),
|
|
73
78
|
},
|
|
74
79
|
{
|
|
75
80
|
name: 'string-format',
|
|
76
|
-
match: (ctx) => ctx.
|
|
81
|
+
match: (ctx) => ctx.dataType === 'string' && typeof ctx.format === 'string',
|
|
77
82
|
node: (ctx) => {
|
|
78
|
-
const format = ctx.
|
|
83
|
+
const format = ctx.format;
|
|
79
84
|
const type = formatWidgets[format] ?? 'Input';
|
|
80
85
|
const inputType = inputTypes[format];
|
|
81
86
|
return { type, ...(inputType ? { props: { inputType } } : {}) };
|
|
@@ -83,31 +88,32 @@ exports.defaultWidgetRules = [
|
|
|
83
88
|
},
|
|
84
89
|
{
|
|
85
90
|
name: 'string-long',
|
|
86
|
-
match: (ctx) =>
|
|
91
|
+
match: (ctx) => {
|
|
92
|
+
const maxLength = ctx.constraints?.maxLength;
|
|
93
|
+
return ctx.dataType === 'string' && (maxLength == null || maxLength >= TEXTAREA_MIN_LENGTH);
|
|
94
|
+
},
|
|
87
95
|
node: 'Textarea',
|
|
88
96
|
},
|
|
89
97
|
{
|
|
90
98
|
name: 'string',
|
|
91
|
-
match: (ctx) => ctx.
|
|
99
|
+
match: (ctx) => ctx.dataType === 'string',
|
|
92
100
|
node: 'Input',
|
|
93
101
|
},
|
|
94
102
|
{
|
|
95
103
|
name: 'structural',
|
|
96
104
|
// Objects and arrays only reach the rules when they carry no fields to
|
|
97
105
|
// lower, so raw JSON is the honest editor for them.
|
|
98
|
-
match: (ctx) => ctx.
|
|
106
|
+
match: (ctx) => ctx.dataType === 'object' || ctx.dataType === 'array',
|
|
99
107
|
node: 'JsonEditor',
|
|
100
108
|
},
|
|
101
109
|
{
|
|
102
110
|
name: 'unresolved',
|
|
103
111
|
// An unresolved `$ref` or a schema with no lowerable type still needs an
|
|
104
112
|
// editable surface, so it falls back to raw JSON rather than vanishing.
|
|
105
|
-
match: (ctx) =>
|
|
113
|
+
match: (ctx) => ctx.dataType === undefined,
|
|
106
114
|
node: 'JsonEditor',
|
|
107
115
|
},
|
|
108
116
|
];
|
|
109
117
|
function resolveRules(rules, replaceDefaults) {
|
|
110
|
-
|
|
111
|
-
return rules ?? [];
|
|
112
|
-
return rules?.length ? [...rules, ...exports.defaultWidgetRules] : exports.defaultWidgetRules;
|
|
118
|
+
return (0, json_renderer_1.composeWidgetRules)(exports.defaultWidgetRules, rules, replaceDefaults);
|
|
113
119
|
}
|
package/types.d.ts
CHANGED
|
@@ -5,23 +5,14 @@
|
|
|
5
5
|
* along untouched so a schema is never rejected for being richer than the
|
|
6
6
|
* lowering rules.
|
|
7
7
|
*/
|
|
8
|
-
import type { UINode,
|
|
8
|
+
import type { UINode, UINodeType } from 'blocks-schema';
|
|
9
|
+
import type { FieldDescriptor, FieldHints, PartialNode as CorePartialNode, WidgetRule as CoreWidgetRule } from 'json-renderer';
|
|
9
10
|
export type JSONSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
description?: string;
|
|
16
|
-
placeholder?: string;
|
|
17
|
-
hidden?: boolean;
|
|
18
|
-
disabled?: boolean;
|
|
19
|
-
className?: string;
|
|
20
|
-
/** Sort weight within its group; lower comes first, unset keeps schema order. */
|
|
21
|
-
order?: number;
|
|
22
|
-
/** Extra props merged onto the produced node. */
|
|
23
|
-
props?: Record<string, unknown>;
|
|
24
|
-
}
|
|
11
|
+
/**
|
|
12
|
+
* UI hints authored inline on a schema, under the `x-ui` extension keyword —
|
|
13
|
+
* the generic field hints, narrowed to the Constructive node vocabulary.
|
|
14
|
+
*/
|
|
15
|
+
export type UIAnnotation = FieldHints<UINodeType>;
|
|
25
16
|
export interface JSONSchema {
|
|
26
17
|
$id?: string;
|
|
27
18
|
$ref?: string;
|
|
@@ -56,19 +47,18 @@ export interface JSONSchema {
|
|
|
56
47
|
'x-ui'?: UIAnnotation;
|
|
57
48
|
[keyword: string]: unknown;
|
|
58
49
|
}
|
|
59
|
-
/**
|
|
60
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Everything a rule needs to decide on one schema position: the generic field
|
|
52
|
+
* descriptor (`dataType`, `format`, `enumValues`, `constraints`, `hints`, …) plus
|
|
53
|
+
* the JSON-Schema-specific facts. Rules written against the descriptor fields
|
|
54
|
+
* work for any document source; rules that need the raw keywords read `schema`.
|
|
55
|
+
*/
|
|
56
|
+
export interface FieldContext extends FieldDescriptor<UINodeType> {
|
|
61
57
|
/** The schema at this position, with `$ref`s already resolved. */
|
|
62
58
|
schema: JSONSchema;
|
|
63
|
-
/** Property name, or `''` for the root schema. */
|
|
64
|
-
name: string;
|
|
65
|
-
/** Dot path from the root, e.g. `billing.address.city`. */
|
|
66
|
-
path: string;
|
|
67
|
-
/** Declared as required by the parent's `required` list. */
|
|
68
|
-
required: boolean;
|
|
69
59
|
/** Normalized primary `type`, or `undefined` when the schema omits it. */
|
|
70
60
|
type?: JSONSchemaType;
|
|
71
|
-
/** Merged `x-ui` annotation for this position
|
|
61
|
+
/** Merged `x-ui` annotation for this position; the same object as `hints`. */
|
|
72
62
|
ui: UIAnnotation;
|
|
73
63
|
/** Resolve a `$ref` against the root schema's definitions. */
|
|
74
64
|
resolve: (schema: JSONSchema) => JSONSchema;
|
|
@@ -78,20 +68,9 @@ export interface FieldContext {
|
|
|
78
68
|
* order and the first match wins, so app-specific rules are prepended rather
|
|
79
69
|
* than replacing the defaults.
|
|
80
70
|
*/
|
|
81
|
-
export
|
|
82
|
-
/** Identifies the rule so a consumer can replace exactly one default. */
|
|
83
|
-
name: string;
|
|
84
|
-
match: (ctx: FieldContext) => boolean;
|
|
85
|
-
/** Node type to render, or a partial node merged over the derived one. */
|
|
86
|
-
node: UINodeType | ((ctx: FieldContext) => UINodeType | PartialNode);
|
|
87
|
-
}
|
|
71
|
+
export type WidgetRule = CoreWidgetRule<FieldContext, UINodeType, UINode>;
|
|
88
72
|
/** A rule's contribution to the node built for a schema position. */
|
|
89
|
-
export
|
|
90
|
-
type?: UINodeType;
|
|
91
|
-
props?: Record<string, unknown>;
|
|
92
|
-
constraints?: UINodeConstraints;
|
|
93
|
-
children?: UINode[];
|
|
94
|
-
}
|
|
73
|
+
export type PartialNode = CorePartialNode<UINodeType, UINode>;
|
|
95
74
|
export interface ConvertOptions {
|
|
96
75
|
/** Document id; defaults to the schema `$id` or `'document'`. */
|
|
97
76
|
id?: string;
|