@zeyos/zx 5.6.0 → 5.7.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/CHANGELOG.md +154 -0
- package/README.md +73 -7
- package/bin/zx-css.js +328 -0
- package/dist/types/components/account-menu/account-menu.d.ts +4 -2
- package/dist/types/components/activity-list/activity-list.d.ts +14 -8
- package/dist/types/components/agent-thread/ag-ui.d.ts +340 -0
- package/dist/types/components/agent-thread/agent-thread.d.ts +142 -14
- package/dist/types/components/agent-thread/ui-message-stream.d.ts +401 -0
- package/dist/types/components/app-sidebar/app-sidebar.d.ts +14 -8
- package/dist/types/components/breadcrumb/breadcrumb.d.ts +3 -3
- package/dist/types/components/calendar/calendar-model.d.ts +9 -2
- package/dist/types/components/calendar/calendar.d.ts +12 -4
- package/dist/types/components/chart/chart.d.ts +25 -13
- package/dist/types/components/code-editor/code-editor.d.ts +7 -2
- package/dist/types/components/composer/composer.d.ts +206 -4
- package/dist/types/components/composer/queue.d.ts +76 -0
- package/dist/types/components/copy/copy-button.d.ts +6 -4
- package/dist/types/components/copy/copy-input.d.ts +10 -6
- package/dist/types/components/datebox/date-range-box.d.ts +33 -0
- package/dist/types/components/datebox/placeholder.d.ts +7 -0
- package/dist/types/components/dock/dock.d.ts +12 -3
- package/dist/types/components/entity-ref/entity-ref.d.ts +5 -3
- package/dist/types/components/field-upload/field-upload.d.ts +3 -2
- package/dist/types/components/file-item/file-item.d.ts +3 -2
- package/dist/types/components/file-list/file-list.d.ts +14 -8
- package/dist/types/components/filter/filter-model.d.ts +31 -1
- package/dist/types/components/filter/filter.d.ts +20 -2
- package/dist/types/components/finder/finder.d.ts +14 -6
- package/dist/types/components/flag/flag.d.ts +99 -0
- package/dist/types/components/grid/grid.d.ts +1 -1
- package/dist/types/components/kanban-view/kanban-view.d.ts +8 -66
- package/dist/types/components/launcher/launcher.d.ts +2 -0
- package/dist/types/components/multi-value-editor/multi-value-editor.d.ts +3 -2
- package/dist/types/components/number-field/number-field.d.ts +17 -6
- package/dist/types/components/prompt-starters/prompt-starters.d.ts +107 -0
- package/dist/types/components/prose/prose.d.ts +9 -6
- package/dist/types/components/questionnaire/conditions.d.ts +270 -0
- package/dist/types/components/questionnaire/items.d.ts +52 -30
- package/dist/types/components/questionnaire/questionnaire.d.ts +44 -4
- package/dist/types/components/rating/rating.d.ts +5 -3
- package/dist/types/components/select/country.d.ts +106 -0
- package/dist/types/components/select/field-adapter.d.ts +2 -1
- package/dist/types/components/select/locale.d.ts +102 -0
- package/dist/types/components/select/select.d.ts +41 -2
- package/dist/types/components/slider/slider.d.ts +1 -0
- package/dist/types/components/sort-control/sort-control.d.ts +7 -3
- package/dist/types/components/source-list/source-list.d.ts +7 -5
- package/dist/types/components/split-view/split-view.d.ts +5 -3
- package/dist/types/components/surface/catalog.d.ts +260 -0
- package/dist/types/components/surface/elicit.d.ts +81 -0
- package/dist/types/components/surface/mcp.d.ts +165 -0
- package/dist/types/components/surface/schema.d.ts +314 -0
- package/dist/types/components/surface/stream.d.ts +118 -0
- package/dist/types/components/surface/surface.d.ts +718 -0
- package/dist/types/components/surface/text.d.ts +277 -0
- package/dist/types/components/surface/validate.d.ts +339 -0
- package/dist/types/components/table/table.d.ts +55 -3
- package/dist/types/components/table/window.d.ts +203 -0
- package/dist/types/components/toolbar/toolbar.d.ts +10 -6
- package/dist/types/components/tooltip/tooltip.d.ts +2 -2
- package/dist/types/components/tree/tree.d.ts +20 -13
- package/dist/types/core/component.d.ts +10 -0
- package/dist/types/core/date.d.ts +8 -1
- package/dist/types/core/dom.d.ts +1 -0
- package/dist/types/core/http.d.ts +10 -0
- package/dist/types/core/i18n.d.ts +38 -0
- package/dist/types/core/messages.d.ts +344 -1
- package/dist/types/core/storage.d.ts +7 -2
- package/dist/types/elements/define.d.ts +21 -46
- package/dist/types/elements/definitions.d.ts +147 -0
- package/dist/types/elements/kit.d.ts +7 -0
- package/dist/types/elements/reflect.d.ts +6 -3
- package/dist/types/index.d.ts +10 -1
- package/dist/types/internal/date-locale.d.ts +7 -0
- package/dist/types/internal/json-patch.d.ts +146 -0
- package/dist/types/locales/de.d.ts +13 -0
- package/dist/types/locales/en.d.ts +2 -0
- package/dist/types/locales/index.d.ts +823 -0
- package/dist/types/zeyos/view.d.ts +8 -1
- package/dist/zx-zeyos.esm.js +16802 -15180
- package/dist/zx-zeyos.esm.min.js +5 -5
- package/dist/zx.core.css +1276 -0
- package/dist/zx.core.min.css +1 -0
- package/dist/zx.css +498 -134
- package/dist/zx.esm.js +40778 -29654
- package/dist/zx.esm.min.js +36 -23
- package/dist/zx.global.js +40778 -29654
- package/dist/zx.global.min.js +36 -23
- package/dist/zx.layered.css +17302 -0
- package/dist/zx.layered.min.css +1 -0
- package/dist/zx.min.css +1 -1
- package/docs/agent-catalog.json +12501 -0
- package/docs/agent-surfaces.md +340 -0
- package/docs/api.json +851 -121
- package/docs/llms.md +1018 -71
- package/docs/llms.txt +30 -9
- package/package.json +29 -6
- package/src/components/account-menu/account-menu.js +6 -3
- package/src/components/activity-item/activity-item.js +2 -1
- package/src/components/activity-list/activity-list.js +16 -10
- package/src/components/agent-thread/ag-ui.js +1439 -0
- package/src/components/agent-thread/agent-thread.js +359 -54
- package/src/components/agent-thread/ui-message-stream.js +1281 -0
- package/src/components/app-sidebar/app-sidebar.js +18 -8
- package/src/components/approval-card/approval-card.js +3 -2
- package/src/components/block-view/block-view.js +7 -6
- package/src/components/breadcrumb/breadcrumb.js +8 -5
- package/src/components/calendar/calendar-model.js +8 -2
- package/src/components/calendar/calendar.js +53 -18
- package/src/components/chart/chart.js +54 -23
- package/src/components/code-editor/code-editor.js +45 -8
- package/src/components/code-editor/field-adapter.js +1 -1
- package/src/components/composer/composer.css +80 -1
- package/src/components/composer/composer.js +390 -16
- package/src/components/composer/queue.js +78 -0
- package/src/components/context-menu/context-menu.js +12 -1
- package/src/components/copy/copy-button.js +9 -7
- package/src/components/copy/copy-input.js +10 -6
- package/src/components/date-picker/date-picker.js +2 -1
- package/src/components/date-picker/date-range-picker.js +8 -7
- package/src/components/datebox/date-range-box.js +3 -9
- package/src/components/datebox/datebox.js +18 -21
- package/src/components/datebox/placeholder.js +18 -0
- package/src/components/dialog/dialog.js +1 -1
- package/src/components/dock/dock.js +34 -13
- package/src/components/entity-ref/entity-ref.js +16 -5
- package/src/components/field-upload/field-upload.js +81 -38
- package/src/components/file-item/file-item.js +70 -40
- package/src/components/file-list/file-list.js +15 -9
- package/src/components/filter/filter-model.js +46 -8
- package/src/components/filter/filter.css +3 -3
- package/src/components/filter/filter.js +29 -10
- package/src/components/filter-panel/filter-panel.js +13 -5
- package/src/components/finder/finder.js +50 -26
- package/src/components/flag/flag.css +40 -0
- package/src/components/flag/flag.js +177 -0
- package/src/components/form/form.js +1 -1
- package/src/components/grid/grid.js +22 -8
- package/src/components/kanban-view/kanban-view.js +51 -81
- package/src/components/launcher/launcher.js +17 -2
- package/src/components/list-toolbar/list-toolbar.js +2 -1
- package/src/components/master-panel/master-panel.js +1 -1
- package/src/components/menu-button/menu-button.js +4 -0
- package/src/components/message/message.js +3 -0
- package/src/components/multi-value-editor/multi-value-editor.js +10 -9
- package/src/components/navigation-bar/navigation-bar.js +3 -3
- package/src/components/number-field/number-field.js +29 -13
- package/src/components/prompt-starters/prompt-starters.css +91 -0
- package/src/components/prompt-starters/prompt-starters.js +206 -0
- package/src/components/prose/prose.js +14 -10
- package/src/components/questionnaire/conditions.js +666 -0
- package/src/components/questionnaire/items.js +150 -41
- package/src/components/questionnaire/questionnaire.js +126 -32
- package/src/components/rating/rating.js +5 -4
- package/src/components/run-status/run-status.js +2 -2
- package/src/components/select/country.js +136 -0
- package/src/components/select/field-adapter.js +7 -2
- package/src/components/select/locale.js +219 -0
- package/src/components/select/select.css +20 -22
- package/src/components/select/select.js +94 -12
- package/src/components/sheet/sheet-stack.js +10 -1
- package/src/components/slider/slider.js +7 -2
- package/src/components/sort-control/sort-control.js +15 -5
- package/src/components/source-list/source-list.js +7 -4
- package/src/components/split-view/split-view.js +5 -3
- package/src/components/surface/catalog.js +1334 -0
- package/src/components/surface/elicit.js +107 -0
- package/src/components/surface/mcp.js +243 -0
- package/src/components/surface/schema.js +644 -0
- package/src/components/surface/stream.js +245 -0
- package/src/components/surface/surface.css +308 -0
- package/src/components/surface/surface.js +2291 -0
- package/src/components/surface/text.js +513 -0
- package/src/components/surface/validate.js +2218 -0
- package/src/components/tabbox/tabbox.js +9 -0
- package/src/components/table/table.css +63 -3
- package/src/components/table/table.js +167 -28
- package/src/components/table/window.js +317 -0
- package/src/components/toolbar/toolbar.js +12 -7
- package/src/components/tooltip/tooltip.css +2 -3
- package/src/components/tooltip/tooltip.js +32 -5
- package/src/components/tree/tree.js +53 -23
- package/src/core/component.js +25 -1
- package/src/core/date.js +62 -16
- package/src/core/dom.js +22 -2
- package/src/core/format.js +15 -13
- package/src/core/http.js +29 -20
- package/src/core/i18n.js +83 -10
- package/src/core/keyboard.js +79 -8
- package/src/core/messages.js +376 -2
- package/src/core/overlay-host.js +1 -0
- package/src/core/storage.js +59 -6
- package/src/elements/define.js +178 -130
- package/src/elements/definitions.js +229 -0
- package/src/elements/kit.js +444 -0
- package/src/elements/reflect.js +8 -3
- package/src/index.js +10 -1
- package/src/internal/date-locale.js +11 -0
- package/src/internal/json-patch.js +405 -0
- package/src/locales/de.js +911 -0
- package/src/locales/en.js +2 -0
- package/src/locales/index.js +7 -0
- package/src/zeyos/calendar.js +2 -1
- package/src/zeyos/form.js +2 -1
- package/src/zeyos/launcher.js +7 -6
- package/src/zeyos/module-bar.js +2 -1
- package/src/zeyos/schema.js +37 -23
- package/src/zeyos/select.js +4 -3
- package/src/zeyos/table.js +2 -13
- package/src/zeyos/view.js +57 -17
- package/styles/core.css +11 -0
- package/styles/tokens/semantic.css +1 -0
- package/styles/zeyos.css +2 -0
- package/styles/zx.css +4 -8
- package/styles/zx.layered.css +1 -0
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* The input schemas of a surface: a restricted JSON Schema, turned into Fieldset fields and used
|
|
4
|
+
* to check what a person submits.
|
|
5
|
+
*
|
|
6
|
+
* The subset is the one MCP elicitation's form mode uses: one flat object with `required`, whose
|
|
7
|
+
* properties are `string`, `number`, `integer` or `boolean` (a `string` may offer `enum` or `oneOf`
|
|
8
|
+
* of `{const, title}`, and carry `format` `date`, `date-time`, `time`, `email` or `uri`), or a
|
|
9
|
+
* multiple choice: an `array` whose `items` are such an `enum` or `anyOf` of `{const, title}`.
|
|
10
|
+
* `minimum`/`maximum`, `minLength`/`maxLength`, `minItems`/`maxItems`, `default`, `title` and
|
|
11
|
+
* `description` complete it. An `x-zx` member may name the Field type that draws a property and
|
|
12
|
+
* carry its `visible` condition. There are no patterns, no nesting and no secret inputs.
|
|
13
|
+
*
|
|
14
|
+
* The module touches no DOM, so it runs unchanged in a browser and on a server.
|
|
15
|
+
*/
|
|
16
|
+
import { checkRules, isEmptyAnswer, ruleMessage } from '../questionnaire/conditions.js';
|
|
17
|
+
|
|
18
|
+
/** @typedef {import('../questionnaire/conditions.js').RuleMessage} RuleMessage */
|
|
19
|
+
/** @typedef {import('../questionnaire/conditions.js').Condition} Condition */
|
|
20
|
+
/** @typedef {import('../questionnaire/conditions.js').ConditionProblem} ConditionProblem */
|
|
21
|
+
|
|
22
|
+
/** @typedef {'date'|'date-time'|'time'|'email'|'uri'} SchemaFormat */
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One choice of an `enum` written with its title.
|
|
26
|
+
* @typedef {{const: string, title: string}} SchemaChoice
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The surface's own hint on a property.
|
|
31
|
+
* @typedef {Object} SchemaHint
|
|
32
|
+
* @property {string} [field] The Field type that draws the property; see `FIELD_TYPES`.
|
|
33
|
+
* @property {Condition} [visible] Shows the property only while the condition holds.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* One property of an input schema.
|
|
38
|
+
* @typedef {Object} SchemaProperty
|
|
39
|
+
* @property {'string'|'number'|'integer'|'boolean'|'array'} type What the value is.
|
|
40
|
+
* @property {string} [title] The label. Every property should have one.
|
|
41
|
+
* @property {string} [description] A supporting line under the field.
|
|
42
|
+
* @property {unknown} [default] The value the field starts with.
|
|
43
|
+
* @property {string[]} [enum] The allowed values of a `string`, shown as they are.
|
|
44
|
+
* @property {SchemaChoice[]} [oneOf] The allowed values of a `string`, each with a title.
|
|
45
|
+
* @property {SchemaFormat} [format] A named format of a `string`.
|
|
46
|
+
* @property {number} [minimum] Lowest number.
|
|
47
|
+
* @property {number} [maximum] Highest number.
|
|
48
|
+
* @property {number} [minLength] Fewest characters.
|
|
49
|
+
* @property {number} [maxLength] Most characters.
|
|
50
|
+
* @property {{type?: 'string', enum?: string[], anyOf?: SchemaChoice[]}} [items] The choices of
|
|
51
|
+
* an `array`.
|
|
52
|
+
* @property {number} [minItems] Fewest choices of an `array`.
|
|
53
|
+
* @property {number} [maxItems] Most choices of an `array`.
|
|
54
|
+
* @property {SchemaHint} [x-zx] The surface's own hint.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* An input schema: one flat object.
|
|
59
|
+
* @typedef {Object} InputSchema
|
|
60
|
+
* @property {'object'} type Always `object`.
|
|
61
|
+
* @property {Record<string, SchemaProperty>} properties The inputs, in the order they are shown.
|
|
62
|
+
* @property {string[]} [required] The inputs that need a value.
|
|
63
|
+
* @property {string} [$schema] The JSON Schema dialect; ignored.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A construct outside the subset, or a malformed one. `path` is a JSON Pointer into the schema.
|
|
68
|
+
* @typedef {{path: string, message: string}} SchemaProblem
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The options of one Fieldset field, as `schemaToFields()` returns them.
|
|
73
|
+
* @typedef {Object} SchemaField
|
|
74
|
+
* @property {string} type Registered Field type.
|
|
75
|
+
* @property {string} label Visible label: the title, or the property name.
|
|
76
|
+
* @property {string} [description] Supporting line.
|
|
77
|
+
* @property {boolean} required Whether a value is needed.
|
|
78
|
+
* @property {unknown} [value] The starting value.
|
|
79
|
+
* @property {string} [placeholder] Placeholder text.
|
|
80
|
+
* @property {Record<string, string>} [options] Values and labels of a `select` or `optionlist`.
|
|
81
|
+
* @property {Record<string, unknown>} [props] Properties of the control.
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A value that failed its schema.
|
|
86
|
+
* @typedef {{name: string, message: string}} ValueProblem
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* What `checkValue()` returns.
|
|
91
|
+
* @typedef {Object} CheckedValues
|
|
92
|
+
* @property {Record<string, string|number|boolean|string[]>} values The submitted values as JSON:
|
|
93
|
+
* every visible property that has a value, in schema order. Hidden properties, empty optional
|
|
94
|
+
* ones and names outside the schema are left out.
|
|
95
|
+
* @property {ValueProblem[]} errors One entry per invalid property, in schema order.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/** The named string formats. */
|
|
99
|
+
export const SCHEMA_FORMATS = Object.freeze(/** @type {SchemaFormat[]} */ (['date', 'date-time', 'time', 'email', 'uri']));
|
|
100
|
+
|
|
101
|
+
/** Most properties one schema may declare. */
|
|
102
|
+
export const MAX_SCHEMA_PROPERTIES = 50;
|
|
103
|
+
|
|
104
|
+
/** Most choices one property may offer. */
|
|
105
|
+
export const MAX_SCHEMA_CHOICES = 64;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The Field types a property may name in `x-zx.field`, by what the property is. The first is the
|
|
109
|
+
* default. No kind offers a password, hidden, HTML, upload or custom field.
|
|
110
|
+
*/
|
|
111
|
+
export const FIELD_TYPES = Object.freeze({
|
|
112
|
+
text: Object.freeze(['text', 'textarea']),
|
|
113
|
+
date: Object.freeze(['date']),
|
|
114
|
+
'date-time': Object.freeze(['datetime']),
|
|
115
|
+
time: Object.freeze(['text']),
|
|
116
|
+
email: Object.freeze(['text']),
|
|
117
|
+
uri: Object.freeze(['text']),
|
|
118
|
+
choice: Object.freeze(['select', 'optionlist', 'zxselect']),
|
|
119
|
+
number: Object.freeze(['number', 'slider', 'rating']),
|
|
120
|
+
boolean: Object.freeze(['checkbox', 'toggle']),
|
|
121
|
+
multiple: Object.freeze(['checklist', 'tagpicker'])
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
/** Highest `maximum` a `rating` field may have. */
|
|
125
|
+
const MAX_RATING = 10;
|
|
126
|
+
|
|
127
|
+
/** Names a property may not have. */
|
|
128
|
+
const FORBIDDEN_NAMES = new Set(['__proto__', 'constructor', 'prototype']);
|
|
129
|
+
|
|
130
|
+
/** What a property name looks like: it becomes a key of the reply's `values`. */
|
|
131
|
+
const NAME = /^(?!(?:__proto__|constructor|prototype)$)[A-Za-z_][A-Za-z0-9_.-]{0,63}$/;
|
|
132
|
+
|
|
133
|
+
const TOP_KEYS = new Set(['type', 'properties', 'required', '$schema']);
|
|
134
|
+
const COMMON_KEYS = ['type', 'title', 'description', 'default', 'x-zx'];
|
|
135
|
+
const KEYS = Object.freeze({
|
|
136
|
+
string: new Set([...COMMON_KEYS, 'enum', 'oneOf', 'format', 'minLength', 'maxLength']),
|
|
137
|
+
number: new Set([...COMMON_KEYS, 'minimum', 'maximum']),
|
|
138
|
+
integer: new Set([...COMMON_KEYS, 'minimum', 'maximum']),
|
|
139
|
+
boolean: new Set(COMMON_KEYS),
|
|
140
|
+
array: new Set([...COMMON_KEYS, 'items', 'minItems', 'maxItems'])
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Lists the constructs of a schema that lie outside the subset, and the malformed ones. An empty
|
|
145
|
+
* list means `schemaToFields()` and `checkValue()` can take the schema. An `x-zx.visible`
|
|
146
|
+
* condition is only checked for being an object here; its grammar and the inputs it may name
|
|
147
|
+
* belong to the surface that holds the schema.
|
|
148
|
+
* @param {unknown} schema Schema to check.
|
|
149
|
+
* @returns {SchemaProblem[]}
|
|
150
|
+
*/
|
|
151
|
+
export function unsupported(schema) {
|
|
152
|
+
/** @type {SchemaProblem[]} */
|
|
153
|
+
const problems = [];
|
|
154
|
+
if (!isObject(schema)) return [{ path: '', message: 'A schema must be an object' }];
|
|
155
|
+
for (const key of Object.keys(schema)) {
|
|
156
|
+
if (!TOP_KEYS.has(key)) problems.push({ path: `/${escape(key)}`, message: `"${key}" is not supported in an input schema` });
|
|
157
|
+
}
|
|
158
|
+
if (schema.type !== 'object') problems.push({ path: '/type', message: 'An input schema must be of type "object"' });
|
|
159
|
+
if (Object.hasOwn(schema, '$schema') && typeof schema.$schema !== 'string') {
|
|
160
|
+
problems.push({ path: '/$schema', message: '"$schema" must be a string' });
|
|
161
|
+
}
|
|
162
|
+
const properties = schema.properties;
|
|
163
|
+
if (!isObject(properties)) {
|
|
164
|
+
problems.push({ path: '/properties', message: 'An input schema needs "properties"' });
|
|
165
|
+
return problems;
|
|
166
|
+
}
|
|
167
|
+
const names = Object.keys(properties);
|
|
168
|
+
if (names.length > MAX_SCHEMA_PROPERTIES) {
|
|
169
|
+
problems.push({ path: '/properties', message: `An input schema may have at most ${MAX_SCHEMA_PROPERTIES} properties` });
|
|
170
|
+
}
|
|
171
|
+
for (const name of names) {
|
|
172
|
+
const path = `/properties/${escape(name)}`;
|
|
173
|
+
if (FORBIDDEN_NAMES.has(name) || !NAME.test(name)) {
|
|
174
|
+
problems.push({ path, message: `"${name}" is not a usable property name` });
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
for (const problem of propertyProblems(properties[name])) problems.push({ path: path + problem.path, message: problem.message });
|
|
178
|
+
}
|
|
179
|
+
if (Object.hasOwn(schema, 'required')) {
|
|
180
|
+
const required = schema.required;
|
|
181
|
+
if (!Array.isArray(required) || !required.every((name) => typeof name === 'string')) {
|
|
182
|
+
problems.push({ path: '/required', message: '"required" must be a list of property names' });
|
|
183
|
+
} else {
|
|
184
|
+
required.forEach((name, index) => {
|
|
185
|
+
if (!Object.hasOwn(properties, name)) problems.push({ path: `/required/${index}`, message: `"${name}" is not a property` });
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return problems;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Lists what is wrong with one property. Paths are relative to the property.
|
|
194
|
+
* @param {unknown} property
|
|
195
|
+
* @returns {SchemaProblem[]}
|
|
196
|
+
*/
|
|
197
|
+
export function propertyProblems(property) {
|
|
198
|
+
/** @type {SchemaProblem[]} */
|
|
199
|
+
const problems = [];
|
|
200
|
+
if (!isObject(property)) return [{ path: '', message: 'A property must be an object' }];
|
|
201
|
+
const type = property.type;
|
|
202
|
+
if (typeof type !== 'string' || !Object.hasOwn(KEYS, type)) {
|
|
203
|
+
return [{ path: '/type', message: Array.isArray(type)
|
|
204
|
+
? 'A property has exactly one type'
|
|
205
|
+
: `Type ${JSON.stringify(type)} is not supported; use string, number, integer, boolean or a multiple-choice array` }];
|
|
206
|
+
}
|
|
207
|
+
const allowed = KEYS[/** @type {keyof typeof KEYS} */ (type)];
|
|
208
|
+
for (const key of Object.keys(property)) {
|
|
209
|
+
if (!allowed.has(key)) problems.push({ path: `/${escape(key)}`, message: `"${key}" is not supported on a ${type} property` });
|
|
210
|
+
}
|
|
211
|
+
for (const key of ['title', 'description']) {
|
|
212
|
+
if (Object.hasOwn(property, key) && typeof property[key] !== 'string') problems.push({ path: `/${key}`, message: `"${key}" must be a string` });
|
|
213
|
+
}
|
|
214
|
+
for (const key of ['minLength', 'maxLength', 'minItems', 'maxItems']) {
|
|
215
|
+
if (Object.hasOwn(property, key) && !(Number.isInteger(property[key]) && /** @type {number} */ (property[key]) >= 0)) {
|
|
216
|
+
problems.push({ path: `/${key}`, message: `"${key}" must be a whole number of zero or more` });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const key of ['minimum', 'maximum']) {
|
|
220
|
+
if (Object.hasOwn(property, key) && !finite(property[key])) problems.push({ path: `/${key}`, message: `"${key}" must be a finite number` });
|
|
221
|
+
}
|
|
222
|
+
if (finite(property.minimum) && finite(property.maximum) && property.minimum > property.maximum) {
|
|
223
|
+
problems.push({ path: '/minimum', message: '"minimum" is greater than "maximum"' });
|
|
224
|
+
}
|
|
225
|
+
if (type === 'string') {
|
|
226
|
+
const hasEnum = Object.hasOwn(property, 'enum');
|
|
227
|
+
const hasOneOf = Object.hasOwn(property, 'oneOf');
|
|
228
|
+
if (hasEnum && hasOneOf) problems.push({ path: '/oneOf', message: 'Use either "enum" or "oneOf", not both' });
|
|
229
|
+
if (hasEnum) problems.push(...enumProblems(property.enum, '/enum'));
|
|
230
|
+
if (hasOneOf) problems.push(...choiceProblems(property.oneOf, '/oneOf'));
|
|
231
|
+
if (Object.hasOwn(property, 'format')) {
|
|
232
|
+
if (!SCHEMA_FORMATS.includes(/** @type {SchemaFormat} */ (property.format))) {
|
|
233
|
+
problems.push({ path: '/format', message: `"format" must be one of ${SCHEMA_FORMATS.join(', ')}` });
|
|
234
|
+
} else if (hasEnum || hasOneOf) {
|
|
235
|
+
problems.push({ path: '/format', message: 'A property with choices takes no "format"' });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (type === 'array') {
|
|
240
|
+
const items = property.items;
|
|
241
|
+
if (!isObject(items)) {
|
|
242
|
+
problems.push({ path: '/items', message: 'A multiple choice needs "items" with "enum" or "anyOf"' });
|
|
243
|
+
} else {
|
|
244
|
+
for (const key of Object.keys(items)) {
|
|
245
|
+
if (key !== 'type' && key !== 'enum' && key !== 'anyOf') problems.push({ path: `/items/${escape(key)}`, message: `"${key}" is not supported in "items"` });
|
|
246
|
+
}
|
|
247
|
+
if (Object.hasOwn(items, 'type') && items.type !== 'string') problems.push({ path: '/items/type', message: 'Choices are strings' });
|
|
248
|
+
if (Object.hasOwn(items, 'enum') === Object.hasOwn(items, 'anyOf')) {
|
|
249
|
+
problems.push({ path: '/items', message: '"items" needs exactly one of "enum" and "anyOf"' });
|
|
250
|
+
} else if (Object.hasOwn(items, 'enum')) {
|
|
251
|
+
problems.push(...enumProblems(items.enum, '/items/enum'));
|
|
252
|
+
} else {
|
|
253
|
+
problems.push(...choiceProblems(items.anyOf, '/items/anyOf'));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (Object.hasOwn(property, 'default')) {
|
|
258
|
+
const fault = defaultProblem(/** @type {SchemaProperty} */ (property), property.default);
|
|
259
|
+
if (fault) problems.push({ path: '/default', message: fault });
|
|
260
|
+
}
|
|
261
|
+
if (Object.hasOwn(property, 'x-zx')) {
|
|
262
|
+
const hint = property['x-zx'];
|
|
263
|
+
if (!isObject(hint)) {
|
|
264
|
+
problems.push({ path: '/x-zx', message: '"x-zx" must be an object' });
|
|
265
|
+
} else {
|
|
266
|
+
for (const key of Object.keys(hint)) {
|
|
267
|
+
if (key !== 'field' && key !== 'visible') problems.push({ path: `/x-zx/${escape(key)}`, message: `"${key}" is not supported in "x-zx"` });
|
|
268
|
+
}
|
|
269
|
+
if (Object.hasOwn(hint, 'visible') && !isObject(hint.visible)) {
|
|
270
|
+
problems.push({ path: '/x-zx/visible', message: '"visible" must be a condition' });
|
|
271
|
+
}
|
|
272
|
+
if (Object.hasOwn(hint, 'field')) {
|
|
273
|
+
const fault = fieldProblem(/** @type {SchemaProperty} */ (property), hint.field);
|
|
274
|
+
if (fault) problems.push({ path: '/x-zx/field', message: fault });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return problems;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Turns a schema into the options of Fieldset fields, keyed and ordered by property name. Run
|
|
283
|
+
* `unsupported()` first: a schema outside the subset throws.
|
|
284
|
+
* @param {InputSchema} schema An input schema within the subset.
|
|
285
|
+
* @returns {Record<string, SchemaField>}
|
|
286
|
+
* @throws {TypeError} When the schema is outside the subset.
|
|
287
|
+
*/
|
|
288
|
+
export function schemaToFields(schema) {
|
|
289
|
+
const problems = unsupported(schema);
|
|
290
|
+
if (problems.length) throw new TypeError(`Unsupported input schema: ${problems.map(describe).join('; ')}`);
|
|
291
|
+
const required = new Set(schema.required ?? []);
|
|
292
|
+
/** @type {Record<string, SchemaField>} */
|
|
293
|
+
const fields = {};
|
|
294
|
+
for (const [name, property] of Object.entries(schema.properties)) {
|
|
295
|
+
fields[name] = propertyField(name, property, required.has(name));
|
|
296
|
+
}
|
|
297
|
+
return fields;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The Fieldset field for one property.
|
|
302
|
+
* @param {string} name @param {SchemaProperty} property @param {boolean} required
|
|
303
|
+
* @returns {SchemaField}
|
|
304
|
+
*/
|
|
305
|
+
function propertyField(name, property, required) {
|
|
306
|
+
const kind = kindOf(property);
|
|
307
|
+
const type = property['x-zx']?.field ?? FIELD_TYPES[kind][0];
|
|
308
|
+
/** @type {SchemaField} */
|
|
309
|
+
const field = { type, label: property.title || name, required };
|
|
310
|
+
if (property.description) field.description = property.description;
|
|
311
|
+
const hasDefault = Object.hasOwn(property, 'default');
|
|
312
|
+
if (hasDefault) field.value = property.default;
|
|
313
|
+
/** @type {Record<string, unknown>} */
|
|
314
|
+
const props = {};
|
|
315
|
+
|
|
316
|
+
switch (kind) {
|
|
317
|
+
case 'text':
|
|
318
|
+
case 'email':
|
|
319
|
+
case 'uri':
|
|
320
|
+
case 'time':
|
|
321
|
+
if (property.minLength !== undefined) props.minLength = property.minLength;
|
|
322
|
+
if (property.maxLength !== undefined) props.maxLength = property.maxLength;
|
|
323
|
+
if (kind === 'email') props.inputMode = 'email';
|
|
324
|
+
if (kind === 'uri') props.inputMode = 'url';
|
|
325
|
+
if (kind === 'time') {
|
|
326
|
+
props.inputMode = 'numeric';
|
|
327
|
+
field.placeholder = 'HH:MM';
|
|
328
|
+
}
|
|
329
|
+
if (kind === 'email' || kind === 'uri' || kind === 'time') props.spellcheck = false;
|
|
330
|
+
break;
|
|
331
|
+
case 'date':
|
|
332
|
+
case 'date-time':
|
|
333
|
+
break;
|
|
334
|
+
case 'choice': {
|
|
335
|
+
const choices = choicesOf(property);
|
|
336
|
+
if (type === 'zxselect') {
|
|
337
|
+
props.items = choices.map(([value, label]) => ({ value, label }));
|
|
338
|
+
props.valueKey = 'value';
|
|
339
|
+
props.labelKey = 'label';
|
|
340
|
+
props.clearable = !required;
|
|
341
|
+
} else {
|
|
342
|
+
/** @type {Record<string, string>} */
|
|
343
|
+
const options = {};
|
|
344
|
+
// A native select starts on its first option, so an empty one comes first unless the
|
|
345
|
+
// field is required and already has its value.
|
|
346
|
+
if (type === 'select' && !(required && hasDefault)) options[''] = '';
|
|
347
|
+
// Choice values become keys: defined, never assigned, so `__proto__` stays a value.
|
|
348
|
+
for (const [value, label] of choices) Object.defineProperty(options, value, { value: label, enumerable: true, writable: true, configurable: true });
|
|
349
|
+
field.options = options;
|
|
350
|
+
}
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
case 'number':
|
|
354
|
+
if (type === 'rating') {
|
|
355
|
+
props.max = property.maximum ?? 5;
|
|
356
|
+
props.clearable = !required;
|
|
357
|
+
} else {
|
|
358
|
+
if (property.minimum !== undefined) props.min = property.minimum;
|
|
359
|
+
if (property.maximum !== undefined) props.max = property.maximum;
|
|
360
|
+
if (property.type === 'integer') props.step = 1;
|
|
361
|
+
if (type === 'number' && property.type === 'integer') props.precision = 0;
|
|
362
|
+
if (type === 'slider') props.showValue = true;
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
case 'boolean':
|
|
366
|
+
if (!hasDefault) field.value = false;
|
|
367
|
+
break;
|
|
368
|
+
case 'multiple': {
|
|
369
|
+
const items = choicesOf(property).map(([value, label]) => ({ value, label }));
|
|
370
|
+
props.items = items;
|
|
371
|
+
props.valueKey = 'value';
|
|
372
|
+
props.labelKey = 'label';
|
|
373
|
+
if (type === 'checklist') props.search = items.length > 8;
|
|
374
|
+
if (type === 'tagpicker' && property.maxItems !== undefined) props.max = property.maxItems;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
default:
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
if (Object.keys(props).length) field.props = props;
|
|
381
|
+
return field;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Checks submitted values against a schema and returns them as JSON. Hidden properties are skipped
|
|
386
|
+
* entirely: they are not required and not part of the result. Whatever a Field holds internally
|
|
387
|
+
* comes back as JSON: a `Date` becomes `YYYY-MM-DD` for a `date`, a local `YYYY-MM-DDTHH:MM:SS`
|
|
388
|
+
* for a `date-time` and `HH:MM` for a `time`; a numeric string becomes a number for a `number` or
|
|
389
|
+
* `integer`.
|
|
390
|
+
* @param {InputSchema} schema An input schema within the subset.
|
|
391
|
+
* @param {Record<string, unknown>} values The submitted values by property name.
|
|
392
|
+
* @param {Iterable<string>} [hidden=[]] Names of the properties hidden at the moment.
|
|
393
|
+
* @param {RuleMessage} [message] Resolves a message key, as `checkRules()` does.
|
|
394
|
+
* @returns {CheckedValues}
|
|
395
|
+
* @throws {TypeError} When the schema is outside the subset.
|
|
396
|
+
*/
|
|
397
|
+
export function checkValue(schema, values, hidden = [], message = ruleMessage) {
|
|
398
|
+
const problems = unsupported(schema);
|
|
399
|
+
if (problems.length) throw new TypeError(`Unsupported input schema: ${problems.map(describe).join('; ')}`);
|
|
400
|
+
const skip = new Set(hidden);
|
|
401
|
+
const required = new Set(schema.required ?? []);
|
|
402
|
+
const source = isObject(values) ? values : {};
|
|
403
|
+
/** @type {CheckedValues} */
|
|
404
|
+
const result = { values: {}, errors: [] };
|
|
405
|
+
for (const [name, property] of Object.entries(schema.properties)) {
|
|
406
|
+
if (skip.has(name)) continue;
|
|
407
|
+
const value = toJsonValue(property, Object.hasOwn(source, name) ? source[name] : undefined);
|
|
408
|
+
const empty = isEmptyAnswer(value) || (property.type === 'boolean' && value !== true && value !== false);
|
|
409
|
+
if (empty) {
|
|
410
|
+
if (required.has(name)) result.errors.push({ name, message: message('required') });
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const typeKey = typeProblem(property, value);
|
|
414
|
+
const fault = typeKey ? typeMessage(typeKey, message) : checkRules(rulesOf(property), value, message);
|
|
415
|
+
if (fault) {
|
|
416
|
+
result.errors.push({ name, message: fault });
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
result.values[name] = /** @type {string|number|boolean|string[]} */ (value);
|
|
420
|
+
}
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Brings what a Field holds into the JSON a reply carries. Values that cannot be brought are
|
|
426
|
+
* returned as they are, for `checkValue()` to report.
|
|
427
|
+
* @param {SchemaProperty} property The property's schema.
|
|
428
|
+
* @param {unknown} value What the Field holds.
|
|
429
|
+
* @returns {unknown}
|
|
430
|
+
*/
|
|
431
|
+
export function toJsonValue(property, value) {
|
|
432
|
+
if (value === undefined) return null;
|
|
433
|
+
if (value instanceof Date) {
|
|
434
|
+
if (Number.isNaN(value.getTime())) return null;
|
|
435
|
+
const format = property.type === 'string' ? property.format : undefined;
|
|
436
|
+
const date = `${String(value.getFullYear()).padStart(4, '0')}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`;
|
|
437
|
+
const time = `${pad(value.getHours())}:${pad(value.getMinutes())}`;
|
|
438
|
+
if (format === 'time') return time;
|
|
439
|
+
if (format === 'date-time') return `${date}T${time}:${pad(value.getSeconds())}`;
|
|
440
|
+
return date;
|
|
441
|
+
}
|
|
442
|
+
switch (property.type) {
|
|
443
|
+
case 'number':
|
|
444
|
+
case 'integer':
|
|
445
|
+
if (typeof value === 'string') {
|
|
446
|
+
const trimmed = value.trim();
|
|
447
|
+
if (trimmed === '') return null;
|
|
448
|
+
const number = Number(trimmed);
|
|
449
|
+
return Number.isFinite(number) ? number : value;
|
|
450
|
+
}
|
|
451
|
+
return value;
|
|
452
|
+
case 'string':
|
|
453
|
+
return typeof value === 'number' && Number.isFinite(value) ? String(value) : value;
|
|
454
|
+
case 'array':
|
|
455
|
+
return Array.isArray(value) ? value.map((entry) => (typeof entry === 'number' && Number.isFinite(entry) ? String(entry) : entry)) : value;
|
|
456
|
+
default:
|
|
457
|
+
return value;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The values a property offers, each with its label, in order.
|
|
463
|
+
* @param {SchemaProperty} property
|
|
464
|
+
* @returns {Array<[string, string]>}
|
|
465
|
+
*/
|
|
466
|
+
export function choicesOf(property) {
|
|
467
|
+
const source = property.type === 'array' ? property.items ?? {} : property;
|
|
468
|
+
if (Array.isArray(source.enum)) return source.enum.map((value) => [value, value]);
|
|
469
|
+
const list = property.type === 'array' ? /** @type {{anyOf?: SchemaChoice[]}} */ (source).anyOf : property.oneOf;
|
|
470
|
+
return Array.isArray(list) ? list.map((choice) => [choice.const, choice.title]) : [];
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* What a property is, which decides the Field types it may use.
|
|
475
|
+
* @param {SchemaProperty} property
|
|
476
|
+
* @returns {keyof typeof FIELD_TYPES}
|
|
477
|
+
*/
|
|
478
|
+
export function kindOf(property) {
|
|
479
|
+
switch (property.type) {
|
|
480
|
+
case 'boolean': return 'boolean';
|
|
481
|
+
case 'number':
|
|
482
|
+
case 'integer': return 'number';
|
|
483
|
+
case 'array': return 'multiple';
|
|
484
|
+
default:
|
|
485
|
+
if (Array.isArray(property.enum) || Array.isArray(property.oneOf)) return 'choice';
|
|
486
|
+
return property.format && Object.hasOwn(FIELD_TYPES, property.format) ? /** @type {keyof typeof FIELD_TYPES} */ (property.format) : 'text';
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* The declarative rules a property's constraints amount to.
|
|
492
|
+
* @param {SchemaProperty} property
|
|
493
|
+
* @returns {import('../questionnaire/conditions.js').ValidationRule[]}
|
|
494
|
+
*/
|
|
495
|
+
function rulesOf(property) {
|
|
496
|
+
/** @type {import('../questionnaire/conditions.js').ValidationRule[]} */
|
|
497
|
+
const rules = [];
|
|
498
|
+
if (property.type === 'string') {
|
|
499
|
+
if (property.format) rules.push({ format: property.format });
|
|
500
|
+
if (property.minLength !== undefined) rules.push({ minLength: property.minLength });
|
|
501
|
+
if (property.maxLength !== undefined) rules.push({ maxLength: property.maxLength });
|
|
502
|
+
}
|
|
503
|
+
if (property.type === 'array') {
|
|
504
|
+
if (property.minItems !== undefined) rules.push({ minLength: property.minItems });
|
|
505
|
+
if (property.maxItems !== undefined) rules.push({ maxLength: property.maxItems });
|
|
506
|
+
}
|
|
507
|
+
if (property.type === 'number' || property.type === 'integer') {
|
|
508
|
+
if (property.minimum !== undefined) rules.push({ minimum: property.minimum });
|
|
509
|
+
if (property.maximum !== undefined) rules.push({ maximum: property.maximum });
|
|
510
|
+
}
|
|
511
|
+
const choices = choicesOf(property);
|
|
512
|
+
if (choices.length) rules.push({ oneOf: choices.map(([value]) => value) });
|
|
513
|
+
return rules;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** English texts of the type messages, used when the message function does not know the key. */
|
|
517
|
+
export const TYPE_MESSAGES = Object.freeze({
|
|
518
|
+
string: 'Enter text.',
|
|
519
|
+
number: 'Enter a number.',
|
|
520
|
+
integer: 'Enter a whole number.',
|
|
521
|
+
boolean: 'Choose yes or no.',
|
|
522
|
+
array: 'Choose from the list.'
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Checks a value's JSON type against its property.
|
|
527
|
+
* @param {SchemaProperty} property @param {unknown} value
|
|
528
|
+
* @returns {keyof typeof TYPE_MESSAGES|null} The message key of the expected type, or null when
|
|
529
|
+
* the type fits.
|
|
530
|
+
*/
|
|
531
|
+
function typeProblem(property, value) {
|
|
532
|
+
switch (property.type) {
|
|
533
|
+
case 'string': return typeof value === 'string' ? null : 'string';
|
|
534
|
+
case 'number': return typeof value === 'number' && Number.isFinite(value) ? null : 'number';
|
|
535
|
+
case 'integer': return Number.isInteger(value) ? null : 'integer';
|
|
536
|
+
case 'boolean': return typeof value === 'boolean' ? null : 'boolean';
|
|
537
|
+
default: return Array.isArray(value) && value.every((entry) => typeof entry === 'string') ? null : 'array';
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* @param {keyof typeof TYPE_MESSAGES} key @param {RuleMessage} message
|
|
543
|
+
* @returns {string} The message's text for the key, or the English default.
|
|
544
|
+
*/
|
|
545
|
+
function typeMessage(key, message) {
|
|
546
|
+
const text = message(key);
|
|
547
|
+
return text && text !== key ? text : TYPE_MESSAGES[key];
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** @param {unknown} list @param {string} path @returns {SchemaProblem[]} */
|
|
551
|
+
function enumProblems(list, path) {
|
|
552
|
+
if (!Array.isArray(list) || list.length === 0) return [{ path, message: '"enum" needs a non-empty list of strings' }];
|
|
553
|
+
if (list.length > MAX_SCHEMA_CHOICES) return [{ path, message: `At most ${MAX_SCHEMA_CHOICES} choices` }];
|
|
554
|
+
if (!list.every((value) => typeof value === 'string')) return [{ path, message: 'Choices are strings' }];
|
|
555
|
+
if (new Set(list).size !== list.length) return [{ path, message: 'Choices must differ' }];
|
|
556
|
+
return [];
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** @param {unknown} list @param {string} path @returns {SchemaProblem[]} */
|
|
560
|
+
function choiceProblems(list, path) {
|
|
561
|
+
if (!Array.isArray(list) || list.length === 0) return [{ path, message: 'Needs a non-empty list of {const, title}' }];
|
|
562
|
+
if (list.length > MAX_SCHEMA_CHOICES) return [{ path, message: `At most ${MAX_SCHEMA_CHOICES} choices` }];
|
|
563
|
+
/** @type {SchemaProblem[]} */
|
|
564
|
+
const problems = [];
|
|
565
|
+
const seen = new Set();
|
|
566
|
+
list.forEach((choice, index) => {
|
|
567
|
+
if (!isObject(choice) || Object.keys(choice).some((key) => key !== 'const' && key !== 'title')
|
|
568
|
+
|| typeof choice.const !== 'string' || typeof choice.title !== 'string') {
|
|
569
|
+
problems.push({ path: `${path}/${index}`, message: 'A choice is {const, title} with two strings' });
|
|
570
|
+
} else if (seen.has(choice.const)) {
|
|
571
|
+
problems.push({ path: `${path}/${index}/const`, message: 'Choices must differ' });
|
|
572
|
+
} else {
|
|
573
|
+
seen.add(choice.const);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
return problems;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* @param {SchemaProperty} property @param {unknown} value
|
|
581
|
+
* @returns {string|null} Why the default does not fit, or null.
|
|
582
|
+
*/
|
|
583
|
+
function defaultProblem(property, value) {
|
|
584
|
+
if (typeProblem(property, value)) return `"default" must be a ${property.type === 'array' ? 'list of strings' : property.type}`;
|
|
585
|
+
const choices = choicesOf(property).map(([choice]) => choice);
|
|
586
|
+
if (choices.length) {
|
|
587
|
+
const entries = Array.isArray(value) ? value : [value];
|
|
588
|
+
if (!entries.every((entry) => choices.includes(/** @type {string} */ (entry)))) return '"default" must be one of the choices';
|
|
589
|
+
}
|
|
590
|
+
return null;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @param {SchemaProperty} property @param {unknown} field
|
|
595
|
+
* @returns {string|null} Why the hint does not fit, or null.
|
|
596
|
+
*/
|
|
597
|
+
function fieldProblem(property, field) {
|
|
598
|
+
let kind;
|
|
599
|
+
try {
|
|
600
|
+
kind = kindOf(property);
|
|
601
|
+
} catch {
|
|
602
|
+
return 'The property has no usable type';
|
|
603
|
+
}
|
|
604
|
+
const allowed = FIELD_TYPES[kind] ?? [];
|
|
605
|
+
if (typeof field !== 'string' || !allowed.includes(field)) {
|
|
606
|
+
return `"field" must be one of ${allowed.join(', ')} for this property`;
|
|
607
|
+
}
|
|
608
|
+
if (field === 'slider' && !(finite(property.minimum) && finite(property.maximum))) {
|
|
609
|
+
return 'A slider needs "minimum" and "maximum"';
|
|
610
|
+
}
|
|
611
|
+
if (field === 'rating') {
|
|
612
|
+
if (property.type !== 'integer') return 'A rating is an integer';
|
|
613
|
+
if (property.minimum !== undefined && property.minimum > 1) return 'A rating starts at 0 or 1';
|
|
614
|
+
if (property.maximum !== undefined && !(property.maximum >= 1 && property.maximum <= MAX_RATING)) {
|
|
615
|
+
return `A rating goes up to at most ${MAX_RATING}`;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** @param {SchemaProblem} problem @returns {string} */
|
|
622
|
+
function describe(problem) {
|
|
623
|
+
return problem.path ? `${problem.path}: ${problem.message}` : problem.message;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** @param {unknown} value @returns {value is number} */
|
|
627
|
+
function finite(value) {
|
|
628
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** @param {number} value @returns {string} */
|
|
632
|
+
function pad(value) {
|
|
633
|
+
return String(value).padStart(2, '0');
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** @param {unknown} value @returns {value is Record<string, any>} */
|
|
637
|
+
function isObject(value) {
|
|
638
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** @param {string} key @returns {string} */
|
|
642
|
+
function escape(key) {
|
|
643
|
+
return key.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
644
|
+
}
|