@atscript/ui 0.1.197 → 0.1.199
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/dist/form/create-form-def.cjs +2 -0
- package/dist/form/create-form-def.mjs +3 -2
- package/dist/form/path-utils.cjs +1 -1
- package/dist/form/path-utils.mjs +2 -2
- package/dist/form/types.d.cts +8 -0
- package/dist/form/types.d.mts +8 -0
- package/dist/form/validate.cjs +95 -12
- package/dist/form/validate.d.cts +6 -0
- package/dist/form/validate.d.mts +6 -0
- package/dist/form/validate.mjs +98 -15
- package/dist/shared/annotation-keys.cjs +2 -0
- package/dist/shared/annotation-keys.mjs +2 -1
- package/dist/value-help/extract-ref.cjs +36 -0
- package/dist/value-help/extract-ref.mjs +35 -1
- package/package.json +1 -1
|
@@ -150,6 +150,7 @@ function collectFields(objectType, flatMap, edit, opts) {
|
|
|
150
150
|
}
|
|
151
151
|
if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
|
|
152
152
|
const field = createFieldDef(path, originalProp, edit);
|
|
153
|
+
if (require_extract_ref.isValueHelpPicker(originalProp)) field.picker = true;
|
|
153
154
|
fields.push({
|
|
154
155
|
field: opts?.metaFields?.[path]?.computed ? {
|
|
155
156
|
...field,
|
|
@@ -505,3 +506,4 @@ function typeLabel(kind) {
|
|
|
505
506
|
exports.buildUnionVariants = buildUnionVariants;
|
|
506
507
|
exports.createFormDef = createFormDef;
|
|
507
508
|
exports.findVersionColumn = findVersionColumn;
|
|
509
|
+
exports.resolveOriginalProp = resolveOriginalProp;
|
|
@@ -3,7 +3,7 @@ import { isServedType } from "../i18n/model-texts.mjs";
|
|
|
3
3
|
import { getFieldMeta, hasComputedAnnotations } from "../shared/field-resolver.mjs";
|
|
4
4
|
import { AS_UI_MESSAGES } from "../messages.mjs";
|
|
5
5
|
import { isEmptyMember, isPureLiteralUnion } from "../value-help/extract-literals.mjs";
|
|
6
|
-
import { extractValueHelp } from "../value-help/extract-ref.mjs";
|
|
6
|
+
import { extractValueHelp, isValueHelpPicker } from "../value-help/extract-ref.mjs";
|
|
7
7
|
import { isSkippedNavField } from "./skipped-nav.mjs";
|
|
8
8
|
import { flattenAnnotatedType } from "@atscript/typescript/utils";
|
|
9
9
|
import { isNavRelation, isServerStampedOnUpdate } from "@atscript/db/validator";
|
|
@@ -150,6 +150,7 @@ function collectFields(objectType, flatMap, edit, opts) {
|
|
|
150
150
|
}
|
|
151
151
|
if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
|
|
152
152
|
const field = createFieldDef(path, originalProp, edit);
|
|
153
|
+
if (isValueHelpPicker(originalProp)) field.picker = true;
|
|
153
154
|
fields.push({
|
|
154
155
|
field: opts?.metaFields?.[path]?.computed ? {
|
|
155
156
|
...field,
|
|
@@ -502,4 +503,4 @@ function typeLabel(kind) {
|
|
|
502
503
|
} : { label: capitalize(kind) };
|
|
503
504
|
}
|
|
504
505
|
//#endregion
|
|
505
|
-
export { buildUnionVariants, createFormDef, findVersionColumn };
|
|
506
|
+
export { buildUnionVariants, createFormDef, findVersionColumn, resolveOriginalProp };
|
package/dist/form/path-utils.cjs
CHANGED
|
@@ -141,7 +141,7 @@ function createFormData(type, resolver) {
|
|
|
141
141
|
if (resolved !== void 0) return resolved;
|
|
142
142
|
if (pickers.get(path) === prop) unset.push(path);
|
|
143
143
|
else if (prop.type.kind === "object") {
|
|
144
|
-
for (const [key, child] of prop.type.props) if (
|
|
144
|
+
for (const [key, child] of prop.type.props) if (require_extract_ref.isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
|
|
145
145
|
}
|
|
146
146
|
} });
|
|
147
147
|
if (value === void 0) value = primitiveInitFallback(type);
|
package/dist/form/path-utils.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { META_DEFAULT, UI_FORM_FN_VALUE } from "../shared/annotation-keys.mjs";
|
|
2
2
|
import { resolveFieldProp } from "../shared/field-resolver.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { isValueHelpPicker } from "../value-help/extract-ref.mjs";
|
|
4
4
|
import { createDataFromAnnotatedType, detectDiscriminator } from "@atscript/typescript/utils";
|
|
5
5
|
//#region src/form/path-utils.ts
|
|
6
6
|
/**
|
|
@@ -141,7 +141,7 @@ function createFormData(type, resolver) {
|
|
|
141
141
|
if (resolved !== void 0) return resolved;
|
|
142
142
|
if (pickers.get(path) === prop) unset.push(path);
|
|
143
143
|
else if (prop.type.kind === "object") {
|
|
144
|
-
for (const [key, child] of prop.type.props) if (
|
|
144
|
+
for (const [key, child] of prop.type.props) if (isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
|
|
145
145
|
}
|
|
146
146
|
} });
|
|
147
147
|
if (value === void 0) value = primitiveInitFallback(type);
|
package/dist/form/types.d.cts
CHANGED
|
@@ -57,6 +57,14 @@ interface FormFieldDef {
|
|
|
57
57
|
* the component layer.
|
|
58
58
|
*/
|
|
59
59
|
valueHelpInfo?: ValueHelpInfo;
|
|
60
|
+
/**
|
|
61
|
+
* A property of an object (at any depth: a nested object, an object row of
|
|
62
|
+
* an array) that the form renders as a value-help picker. A new form leaves
|
|
63
|
+
* it unset, and a required one left unpicked fails with code `REQUIRED`
|
|
64
|
+
* (pass it to `createFieldValidator` as `picker`). An array item, a tuple
|
|
65
|
+
* position and a union-typed field are none. Since 0.1.198.
|
|
66
|
+
*/
|
|
67
|
+
picker?: true;
|
|
60
68
|
/**
|
|
61
69
|
* Server-owned field: a `@db.column.derived` one (read from the type
|
|
62
70
|
* metadata), a computed view column (`CreateFormDefOptions.metaFields`) or,
|
package/dist/form/types.d.mts
CHANGED
|
@@ -57,6 +57,14 @@ interface FormFieldDef {
|
|
|
57
57
|
* the component layer.
|
|
58
58
|
*/
|
|
59
59
|
valueHelpInfo?: ValueHelpInfo;
|
|
60
|
+
/**
|
|
61
|
+
* A property of an object (at any depth: a nested object, an object row of
|
|
62
|
+
* an array) that the form renders as a value-help picker. A new form leaves
|
|
63
|
+
* it unset, and a required one left unpicked fails with code `REQUIRED`
|
|
64
|
+
* (pass it to `createFieldValidator` as `picker`). An array item, a tuple
|
|
65
|
+
* position and a union-typed field are none. Since 0.1.198.
|
|
66
|
+
*/
|
|
67
|
+
picker?: true;
|
|
60
68
|
/**
|
|
61
69
|
* Server-owned field: a `@db.column.derived` one (read from the type
|
|
62
70
|
* metadata), a computed view column (`CreateFormDefOptions.metaFields`) or,
|
package/dist/form/validate.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const require_annotation_keys = require("../shared/annotation-keys.cjs");
|
|
2
2
|
const require_types = require("./types.cjs");
|
|
3
3
|
const require_ui_texts = require("../i18n/ui-texts.cjs");
|
|
4
|
+
const require_extract_ref = require("../value-help/extract-ref.cjs");
|
|
4
5
|
const require_skipped_nav = require("./skipped-nav.cjs");
|
|
6
|
+
const require_create_form_def = require("./create-form-def.cjs");
|
|
5
7
|
const require_error_utils = require("./error-utils.cjs");
|
|
6
8
|
let _atscript_typescript_utils = require("@atscript/typescript/utils");
|
|
7
9
|
let _atscript_db_validator = require("@atscript/db/validator");
|
|
@@ -57,6 +59,40 @@ const serverManagedPlugin = (ctx, def, value) => {
|
|
|
57
59
|
if (value !== void 0) return void 0;
|
|
58
60
|
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), withoutFk(def), value);
|
|
59
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* A required value-help picker left unpicked — absent, `null` (its ×), or `""`
|
|
64
|
+
* for a string foreign key (never a key, unless its own `@meta.default` is
|
|
65
|
+
* `''`: its "none"; a `@ui.valueHelp` binding on a plain string column takes
|
|
66
|
+
* `""`, as the server does) — fails with code `REQUIRED`
|
|
67
|
+
* and its `@expect.message`, else the code's text ("Must not be empty"), as
|
|
68
|
+
* `@meta.required` reports a missing value (a number field may not carry
|
|
69
|
+
* `@meta.required`). A `@meta.required` field keeps the validator's own rule
|
|
70
|
+
* (its message).
|
|
71
|
+
*
|
|
72
|
+
* Only for `pickers`: the props of the fields the form renders as a picker
|
|
73
|
+
* ({@link collectPickerProps}, or the per-field validator's own `picker`
|
|
74
|
+
* prop) — not an array item, a tuple position or a union branch, which keep
|
|
75
|
+
* the validation they had.
|
|
76
|
+
*
|
|
77
|
+
* Listed last, as the validator checks `@meta.required` after its plugins: an
|
|
78
|
+
* absent FK the database fills passes first ({@link serverManagedPlugin}), and
|
|
79
|
+
* a field's own `@ui.form.validate` words its error.
|
|
80
|
+
*/
|
|
81
|
+
function missingPickPlugin(pickers) {
|
|
82
|
+
return (ctx, def, value) => {
|
|
83
|
+
if (value !== void 0 && value !== null && value !== "") return void 0;
|
|
84
|
+
if (!pickers.has(def) || def.optional || def.metadata.has("meta.required")) return void 0;
|
|
85
|
+
if (value === "") {
|
|
86
|
+
if (!(def.type.designType === "string") || def.metadata.get("meta.default") === "" || !require_extract_ref.isForeignKey(def)) return;
|
|
87
|
+
}
|
|
88
|
+
const custom = def.metadata.get(require_annotation_keys.EXPECT_MESSAGE);
|
|
89
|
+
ctx.error(custom || _atscript_typescript_utils.VALIDATOR_MESSAGES.REQUIRED, void 0, void 0, {
|
|
90
|
+
code: "REQUIRED",
|
|
91
|
+
custom: !!custom
|
|
92
|
+
});
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
60
96
|
/** A view of `def` without its `@db.rel.FK` marker — `def` itself when it has none. */
|
|
61
97
|
function withoutFk(def) {
|
|
62
98
|
if (!def.metadata.has("db.rel.FK")) return def;
|
|
@@ -72,27 +108,68 @@ function withoutFk(def) {
|
|
|
72
108
|
* it is not derived (an interface's stamp below a union of several types, too)
|
|
73
109
|
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
74
110
|
* visited once.
|
|
111
|
+
*
|
|
112
|
+
* Each field's prop is read from `def.type`, the type the validator walks,
|
|
113
|
+
* along the field's path — not from the field def: a def a Vue form holds
|
|
114
|
+
* reactive hands out proxies of the props of a compiled type, which the
|
|
115
|
+
* validator visits raw (fixed in 0.1.198).
|
|
75
116
|
*/
|
|
76
117
|
function collectDerivedProps(def) {
|
|
77
118
|
const derived = /* @__PURE__ */ new Set();
|
|
78
119
|
const kept = /* @__PURE__ */ new Set();
|
|
79
120
|
const seen = /* @__PURE__ */ new Set();
|
|
80
|
-
const
|
|
121
|
+
const walkFields = (fields, type) => {
|
|
81
122
|
for (const field of fields) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (field.derived) {
|
|
85
|
-
derived.add(field.prop);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
kept.add(field.prop);
|
|
89
|
-
walk(require_types.childFields(field));
|
|
123
|
+
const prop = field.path === "" ? type : type.type.kind === "object" ? require_create_form_def.resolveOriginalProp(type, field.path) : void 0;
|
|
124
|
+
if (prop) walkField(field, prop);
|
|
90
125
|
}
|
|
91
126
|
};
|
|
92
|
-
|
|
127
|
+
const walkField = (field, prop) => {
|
|
128
|
+
if (seen.has(field)) return;
|
|
129
|
+
seen.add(field);
|
|
130
|
+
if (field.derived) {
|
|
131
|
+
derived.add(prop);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
kept.add(prop);
|
|
135
|
+
if (require_types.isObjectField(field)) return walkFields(field.objectDef.fields, prop);
|
|
136
|
+
if (require_types.isArrayField(field)) return walkField(field.itemField, prop.type.of);
|
|
137
|
+
const items = prop.type.items ?? [prop];
|
|
138
|
+
if (require_types.isTupleField(field)) field.itemFields.forEach((f, i) => walkField(f, items[i] ?? prop));
|
|
139
|
+
else if (require_types.isUnionField(field)) field.unionVariants.forEach((v, i) => {
|
|
140
|
+
const item = items[i] ?? prop;
|
|
141
|
+
if (v.def) walkFields(v.def.fields, item);
|
|
142
|
+
if (v.itemField) walkField(v.itemField, item);
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
walkFields(def.fields, def.type);
|
|
93
146
|
for (const prop of kept) derived.delete(prop);
|
|
94
147
|
return derived;
|
|
95
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* The props of the fields the form renders as a value-help picker
|
|
151
|
+
* ({@link FormFieldDef.picker}): every object property of `type` at any depth
|
|
152
|
+
* (a nested object, an object row of an array, a union's object variant) that
|
|
153
|
+
* {@link isValueHelpPicker} accepts — not an array item, a tuple position or a
|
|
154
|
+
* union branch. Read from the type the validator walks, so the props are the
|
|
155
|
+
* nodes it visits: a def a Vue form holds reactive hands out proxies of them.
|
|
156
|
+
* A recursive type is visited once per node.
|
|
157
|
+
*/
|
|
158
|
+
function collectPickerProps(type) {
|
|
159
|
+
const pickers = /* @__PURE__ */ new Set();
|
|
160
|
+
const seen = /* @__PURE__ */ new Set();
|
|
161
|
+
const walk = (node) => {
|
|
162
|
+
if (seen.has(node)) return;
|
|
163
|
+
seen.add(node);
|
|
164
|
+
const t = node.type;
|
|
165
|
+
if (t.kind === "object") for (const prop of t.props.values()) if (require_extract_ref.isValueHelpPicker(prop)) pickers.add(prop);
|
|
166
|
+
else walk(prop);
|
|
167
|
+
else if (t.kind === "array") walk(t.of);
|
|
168
|
+
else if (t.kind !== "") for (const item of t.items) walk(item);
|
|
169
|
+
};
|
|
170
|
+
walk(type);
|
|
171
|
+
return pickers;
|
|
172
|
+
}
|
|
96
173
|
let defaultValidatorPlugins = [];
|
|
97
174
|
/** Replace the default validator plugins applied to every form/field validator. */
|
|
98
175
|
function setDefaultValidatorPlugins(plugins) {
|
|
@@ -120,6 +197,7 @@ function getDefaultValidatorPlugins() {
|
|
|
120
197
|
function getFormValidator(def, opts) {
|
|
121
198
|
const derived = collectDerivedProps(def);
|
|
122
199
|
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
200
|
+
const pickers = collectPickerProps(def.type);
|
|
123
201
|
const options = {
|
|
124
202
|
unknownProps: "ignore",
|
|
125
203
|
...opts,
|
|
@@ -127,7 +205,8 @@ function getFormValidator(def, opts) {
|
|
|
127
205
|
...derived.size > 0 ? [derivedPlugin] : [],
|
|
128
206
|
serverManagedPlugin,
|
|
129
207
|
...defaultValidatorPlugins,
|
|
130
|
-
...opts?.plugins ?? []
|
|
208
|
+
...opts?.plugins ?? [],
|
|
209
|
+
...pickers.size > 0 ? [missingPickPlugin(pickers)] : []
|
|
131
210
|
]
|
|
132
211
|
};
|
|
133
212
|
const validator = new _atscript_typescript_utils.Validator(def.type, options);
|
|
@@ -186,7 +265,11 @@ function createFieldValidator(prop, opts) {
|
|
|
186
265
|
skipDescendantsPlugin,
|
|
187
266
|
serverManagedPlugin,
|
|
188
267
|
...defaultValidatorPlugins
|
|
189
|
-
] : [
|
|
268
|
+
] : [
|
|
269
|
+
serverManagedPlugin,
|
|
270
|
+
...defaultValidatorPlugins,
|
|
271
|
+
...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
|
|
272
|
+
] };
|
|
190
273
|
cached = new _atscript_typescript_utils.Validator(prop, options);
|
|
191
274
|
}
|
|
192
275
|
const judged = skipNormalize ? value : (0, _atscript_typescript_utils.normalizeForType)(prop, value, options);
|
package/dist/form/validate.d.cts
CHANGED
|
@@ -63,6 +63,12 @@ interface TFieldValidatorOptions {
|
|
|
63
63
|
* own annotations are then not looked up as ids again. Codes are still translated.
|
|
64
64
|
*/
|
|
65
65
|
served?: () => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The prop is a field the form renders as a value-help picker
|
|
68
|
+
* ({@link FormFieldDef.picker}): left unpicked, a required one fails with
|
|
69
|
+
* code `REQUIRED`, as the form validator reports it. Since 0.1.198.
|
|
70
|
+
*/
|
|
71
|
+
picker?: boolean;
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
74
|
* Whether a type kind is a container that carries its own children:
|
package/dist/form/validate.d.mts
CHANGED
|
@@ -63,6 +63,12 @@ interface TFieldValidatorOptions {
|
|
|
63
63
|
* own annotations are then not looked up as ids again. Codes are still translated.
|
|
64
64
|
*/
|
|
65
65
|
served?: () => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The prop is a field the form renders as a value-help picker
|
|
68
|
+
* ({@link FormFieldDef.picker}): left unpicked, a required one fails with
|
|
69
|
+
* code `REQUIRED`, as the form validator reports it. Since 0.1.198.
|
|
70
|
+
*/
|
|
71
|
+
picker?: boolean;
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
74
|
* Whether a type kind is a container that carries its own children:
|
package/dist/form/validate.mjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { DB_REL_FK } from "../shared/annotation-keys.mjs";
|
|
2
|
-
import {
|
|
1
|
+
import { DB_REL_FK, EXPECT_MESSAGE } from "../shared/annotation-keys.mjs";
|
|
2
|
+
import { isArrayField, isObjectField, isTupleField, isUnionField } from "./types.mjs";
|
|
3
3
|
import { getUiTexts } from "../i18n/ui-texts.mjs";
|
|
4
|
+
import { isForeignKey, isValueHelpPicker } from "../value-help/extract-ref.mjs";
|
|
4
5
|
import { isSkippedNavField } from "./skipped-nav.mjs";
|
|
6
|
+
import { resolveOriginalProp } from "./create-form-def.mjs";
|
|
5
7
|
import { validatorErrorsToMap } from "./error-utils.mjs";
|
|
6
|
-
import { Validator, localizeErrors, normalizeForType } from "@atscript/typescript/utils";
|
|
8
|
+
import { VALIDATOR_MESSAGES, Validator, localizeErrors, normalizeForType } from "@atscript/typescript/utils";
|
|
7
9
|
import { createDbValidatorPlugin } from "@atscript/db/validator";
|
|
8
10
|
//#region src/form/validate.ts
|
|
9
11
|
/**
|
|
@@ -57,6 +59,40 @@ const serverManagedPlugin = (ctx, def, value) => {
|
|
|
57
59
|
if (value !== void 0) return void 0;
|
|
58
60
|
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), withoutFk(def), value);
|
|
59
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* A required value-help picker left unpicked — absent, `null` (its ×), or `""`
|
|
64
|
+
* for a string foreign key (never a key, unless its own `@meta.default` is
|
|
65
|
+
* `''`: its "none"; a `@ui.valueHelp` binding on a plain string column takes
|
|
66
|
+
* `""`, as the server does) — fails with code `REQUIRED`
|
|
67
|
+
* and its `@expect.message`, else the code's text ("Must not be empty"), as
|
|
68
|
+
* `@meta.required` reports a missing value (a number field may not carry
|
|
69
|
+
* `@meta.required`). A `@meta.required` field keeps the validator's own rule
|
|
70
|
+
* (its message).
|
|
71
|
+
*
|
|
72
|
+
* Only for `pickers`: the props of the fields the form renders as a picker
|
|
73
|
+
* ({@link collectPickerProps}, or the per-field validator's own `picker`
|
|
74
|
+
* prop) — not an array item, a tuple position or a union branch, which keep
|
|
75
|
+
* the validation they had.
|
|
76
|
+
*
|
|
77
|
+
* Listed last, as the validator checks `@meta.required` after its plugins: an
|
|
78
|
+
* absent FK the database fills passes first ({@link serverManagedPlugin}), and
|
|
79
|
+
* a field's own `@ui.form.validate` words its error.
|
|
80
|
+
*/
|
|
81
|
+
function missingPickPlugin(pickers) {
|
|
82
|
+
return (ctx, def, value) => {
|
|
83
|
+
if (value !== void 0 && value !== null && value !== "") return void 0;
|
|
84
|
+
if (!pickers.has(def) || def.optional || def.metadata.has("meta.required")) return void 0;
|
|
85
|
+
if (value === "") {
|
|
86
|
+
if (!(def.type.designType === "string") || def.metadata.get("meta.default") === "" || !isForeignKey(def)) return;
|
|
87
|
+
}
|
|
88
|
+
const custom = def.metadata.get(EXPECT_MESSAGE);
|
|
89
|
+
ctx.error(custom || VALIDATOR_MESSAGES.REQUIRED, void 0, void 0, {
|
|
90
|
+
code: "REQUIRED",
|
|
91
|
+
custom: !!custom
|
|
92
|
+
});
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
60
96
|
/** A view of `def` without its `@db.rel.FK` marker — `def` itself when it has none. */
|
|
61
97
|
function withoutFk(def) {
|
|
62
98
|
if (!def.metadata.has("db.rel.FK")) return def;
|
|
@@ -72,27 +108,68 @@ function withoutFk(def) {
|
|
|
72
108
|
* it is not derived (an interface's stamp below a union of several types, too)
|
|
73
109
|
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
74
110
|
* visited once.
|
|
111
|
+
*
|
|
112
|
+
* Each field's prop is read from `def.type`, the type the validator walks,
|
|
113
|
+
* along the field's path — not from the field def: a def a Vue form holds
|
|
114
|
+
* reactive hands out proxies of the props of a compiled type, which the
|
|
115
|
+
* validator visits raw (fixed in 0.1.198).
|
|
75
116
|
*/
|
|
76
117
|
function collectDerivedProps(def) {
|
|
77
118
|
const derived = /* @__PURE__ */ new Set();
|
|
78
119
|
const kept = /* @__PURE__ */ new Set();
|
|
79
120
|
const seen = /* @__PURE__ */ new Set();
|
|
80
|
-
const
|
|
121
|
+
const walkFields = (fields, type) => {
|
|
81
122
|
for (const field of fields) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (field.derived) {
|
|
85
|
-
derived.add(field.prop);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
kept.add(field.prop);
|
|
89
|
-
walk(childFields(field));
|
|
123
|
+
const prop = field.path === "" ? type : type.type.kind === "object" ? resolveOriginalProp(type, field.path) : void 0;
|
|
124
|
+
if (prop) walkField(field, prop);
|
|
90
125
|
}
|
|
91
126
|
};
|
|
92
|
-
|
|
127
|
+
const walkField = (field, prop) => {
|
|
128
|
+
if (seen.has(field)) return;
|
|
129
|
+
seen.add(field);
|
|
130
|
+
if (field.derived) {
|
|
131
|
+
derived.add(prop);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
kept.add(prop);
|
|
135
|
+
if (isObjectField(field)) return walkFields(field.objectDef.fields, prop);
|
|
136
|
+
if (isArrayField(field)) return walkField(field.itemField, prop.type.of);
|
|
137
|
+
const items = prop.type.items ?? [prop];
|
|
138
|
+
if (isTupleField(field)) field.itemFields.forEach((f, i) => walkField(f, items[i] ?? prop));
|
|
139
|
+
else if (isUnionField(field)) field.unionVariants.forEach((v, i) => {
|
|
140
|
+
const item = items[i] ?? prop;
|
|
141
|
+
if (v.def) walkFields(v.def.fields, item);
|
|
142
|
+
if (v.itemField) walkField(v.itemField, item);
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
walkFields(def.fields, def.type);
|
|
93
146
|
for (const prop of kept) derived.delete(prop);
|
|
94
147
|
return derived;
|
|
95
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* The props of the fields the form renders as a value-help picker
|
|
151
|
+
* ({@link FormFieldDef.picker}): every object property of `type` at any depth
|
|
152
|
+
* (a nested object, an object row of an array, a union's object variant) that
|
|
153
|
+
* {@link isValueHelpPicker} accepts — not an array item, a tuple position or a
|
|
154
|
+
* union branch. Read from the type the validator walks, so the props are the
|
|
155
|
+
* nodes it visits: a def a Vue form holds reactive hands out proxies of them.
|
|
156
|
+
* A recursive type is visited once per node.
|
|
157
|
+
*/
|
|
158
|
+
function collectPickerProps(type) {
|
|
159
|
+
const pickers = /* @__PURE__ */ new Set();
|
|
160
|
+
const seen = /* @__PURE__ */ new Set();
|
|
161
|
+
const walk = (node) => {
|
|
162
|
+
if (seen.has(node)) return;
|
|
163
|
+
seen.add(node);
|
|
164
|
+
const t = node.type;
|
|
165
|
+
if (t.kind === "object") for (const prop of t.props.values()) if (isValueHelpPicker(prop)) pickers.add(prop);
|
|
166
|
+
else walk(prop);
|
|
167
|
+
else if (t.kind === "array") walk(t.of);
|
|
168
|
+
else if (t.kind !== "") for (const item of t.items) walk(item);
|
|
169
|
+
};
|
|
170
|
+
walk(type);
|
|
171
|
+
return pickers;
|
|
172
|
+
}
|
|
96
173
|
let defaultValidatorPlugins = [];
|
|
97
174
|
/** Replace the default validator plugins applied to every form/field validator. */
|
|
98
175
|
function setDefaultValidatorPlugins(plugins) {
|
|
@@ -120,6 +197,7 @@ function getDefaultValidatorPlugins() {
|
|
|
120
197
|
function getFormValidator(def, opts) {
|
|
121
198
|
const derived = collectDerivedProps(def);
|
|
122
199
|
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
200
|
+
const pickers = collectPickerProps(def.type);
|
|
123
201
|
const options = {
|
|
124
202
|
unknownProps: "ignore",
|
|
125
203
|
...opts,
|
|
@@ -127,7 +205,8 @@ function getFormValidator(def, opts) {
|
|
|
127
205
|
...derived.size > 0 ? [derivedPlugin] : [],
|
|
128
206
|
serverManagedPlugin,
|
|
129
207
|
...defaultValidatorPlugins,
|
|
130
|
-
...opts?.plugins ?? []
|
|
208
|
+
...opts?.plugins ?? [],
|
|
209
|
+
...pickers.size > 0 ? [missingPickPlugin(pickers)] : []
|
|
131
210
|
]
|
|
132
211
|
};
|
|
133
212
|
const validator = new Validator(def.type, options);
|
|
@@ -186,7 +265,11 @@ function createFieldValidator(prop, opts) {
|
|
|
186
265
|
skipDescendantsPlugin,
|
|
187
266
|
serverManagedPlugin,
|
|
188
267
|
...defaultValidatorPlugins
|
|
189
|
-
] : [
|
|
268
|
+
] : [
|
|
269
|
+
serverManagedPlugin,
|
|
270
|
+
...defaultValidatorPlugins,
|
|
271
|
+
...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
|
|
272
|
+
] };
|
|
190
273
|
cached = new Validator(prop, options);
|
|
191
274
|
}
|
|
192
275
|
const judged = skipNormalize ? value : normalizeForType(prop, value, options);
|
|
@@ -77,6 +77,7 @@ const META_REQUIRED = "meta.required";
|
|
|
77
77
|
const META_DEFAULT = "meta.default";
|
|
78
78
|
const META_SENSITIVE = "meta.sensitive";
|
|
79
79
|
const EXPECT_MAX_LENGTH = "expect.maxLength";
|
|
80
|
+
const EXPECT_MESSAGE = "expect.message";
|
|
80
81
|
const EXPECT_ARRAY_KEY = "expect.array.key";
|
|
81
82
|
const EXPECT_ARRAY_UNIQUE_ITEMS = "expect.array.uniqueItems";
|
|
82
83
|
const UI_FORM_FN_PREFIX = "ui.form.fn.";
|
|
@@ -119,6 +120,7 @@ exports.DB_VIEW_FOR = DB_VIEW_FOR;
|
|
|
119
120
|
exports.EXPECT_ARRAY_KEY = EXPECT_ARRAY_KEY;
|
|
120
121
|
exports.EXPECT_ARRAY_UNIQUE_ITEMS = EXPECT_ARRAY_UNIQUE_ITEMS;
|
|
121
122
|
exports.EXPECT_MAX_LENGTH = EXPECT_MAX_LENGTH;
|
|
123
|
+
exports.EXPECT_MESSAGE = EXPECT_MESSAGE;
|
|
122
124
|
exports.META_DEFAULT = META_DEFAULT;
|
|
123
125
|
exports.META_DESCRIPTION = META_DESCRIPTION;
|
|
124
126
|
exports.META_ID = META_ID;
|
|
@@ -77,6 +77,7 @@ const META_REQUIRED = "meta.required";
|
|
|
77
77
|
const META_DEFAULT = "meta.default";
|
|
78
78
|
const META_SENSITIVE = "meta.sensitive";
|
|
79
79
|
const EXPECT_MAX_LENGTH = "expect.maxLength";
|
|
80
|
+
const EXPECT_MESSAGE = "expect.message";
|
|
80
81
|
const EXPECT_ARRAY_KEY = "expect.array.key";
|
|
81
82
|
const EXPECT_ARRAY_UNIQUE_ITEMS = "expect.array.uniqueItems";
|
|
82
83
|
const UI_FORM_FN_PREFIX = "ui.form.fn.";
|
|
@@ -101,4 +102,4 @@ const UI_TABLE_FN_CLASSES = "ui.table.fn.classes";
|
|
|
101
102
|
const UI_TABLE_FN_STYLES = "ui.table.fn.styles";
|
|
102
103
|
const UI_FORM_VALIDATE = "ui.form.validate";
|
|
103
104
|
//#endregion
|
|
104
|
-
export { DB_AMOUNT_CURRENCY, DB_AMOUNT_CURRENCY_REF, DB_COLUMN_DERIVED, DB_COLUMN_PRECISION, DB_COLUMN_VERSION, DB_COLUMN_VERSION_TOKEN, DB_ENCRYPTED, DB_HTTP_PATH, DB_JSON, DB_REL_FK, DB_TABLE, DB_UNIT, DB_UNIT_REF, DB_VIEW, DB_VIEW_FOR, EXPECT_ARRAY_KEY, EXPECT_ARRAY_UNIQUE_ITEMS, EXPECT_MAX_LENGTH, META_DEFAULT, META_DESCRIPTION, META_ID, META_LABEL, META_READONLY, META_REQUIRED, META_SENSITIVE, UI_DICT_ATTR, UI_DICT_DESCR, UI_DICT_FILTERABLE, UI_DICT_LABEL, UI_DICT_SEARCHABLE, UI_DICT_SORTABLE, UI_FORM_ACTION, UI_FORM_ATTR, UI_FORM_AUTOCOMPLETE, UI_FORM_CLASSES, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_CLASSES, UI_FORM_FN_DESCRIPTION, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_HINT, UI_FORM_FN_LABEL, UI_FORM_FN_OPTIONS, UI_FORM_FN_PLACEHOLDER, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_FN_STYLES, UI_FORM_FN_SUBMIT_DISABLED, UI_FORM_FN_SUBMIT_TEXT, UI_FORM_FN_TITLE, UI_FORM_FN_VALUE, UI_FORM_GRID_COL_SPAN, UI_FORM_GRID_ROW_SPAN, UI_FORM_HIDDEN, UI_FORM_HINT, UI_FORM_LABEL_SINGULAR, UI_FORM_NS, UI_FORM_OPTIONS, UI_FORM_ORDER, UI_FORM_PLACEHOLDER, UI_FORM_PREFIX, UI_FORM_PREFIX_ICON, UI_FORM_PREFIX_REF, UI_FORM_PUSH_DOWN, UI_FORM_STYLES, UI_FORM_SUBMIT_TEXT, UI_FORM_SUFFIX, UI_FORM_SUFFIX_ICON, UI_FORM_SUFFIX_REF, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_LITERAL_LABEL, UI_NAV_GROUP, UI_NAV_HIDDEN, UI_NAV_ORDER, UI_TABLE_ATTR, UI_TABLE_CLASSES, UI_TABLE_COMPONENT, UI_TABLE_EXCLUDE, UI_TABLE_FN_ATTR, UI_TABLE_FN_CLASSES, UI_TABLE_FN_PREFIX, UI_TABLE_FN_STYLES, UI_TABLE_ORDER, UI_TABLE_SELECT_WITH, UI_TABLE_STYLES, UI_TABLE_TYPE, UI_TABLE_WIDTH, UI_TYPE, UI_VALUE_HELP, UI_VALUE_HELP_COLUMN, UI_VALUE_HELP_DISTINCT, UI_VALUE_HELP_FILL, UI_VALUE_LABEL, WF_ACTION_WITH_DATA };
|
|
105
|
+
export { DB_AMOUNT_CURRENCY, DB_AMOUNT_CURRENCY_REF, DB_COLUMN_DERIVED, DB_COLUMN_PRECISION, DB_COLUMN_VERSION, DB_COLUMN_VERSION_TOKEN, DB_ENCRYPTED, DB_HTTP_PATH, DB_JSON, DB_REL_FK, DB_TABLE, DB_UNIT, DB_UNIT_REF, DB_VIEW, DB_VIEW_FOR, EXPECT_ARRAY_KEY, EXPECT_ARRAY_UNIQUE_ITEMS, EXPECT_MAX_LENGTH, EXPECT_MESSAGE, META_DEFAULT, META_DESCRIPTION, META_ID, META_LABEL, META_READONLY, META_REQUIRED, META_SENSITIVE, UI_DICT_ATTR, UI_DICT_DESCR, UI_DICT_FILTERABLE, UI_DICT_LABEL, UI_DICT_SEARCHABLE, UI_DICT_SORTABLE, UI_FORM_ACTION, UI_FORM_ATTR, UI_FORM_AUTOCOMPLETE, UI_FORM_CLASSES, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_CLASSES, UI_FORM_FN_DESCRIPTION, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_HINT, UI_FORM_FN_LABEL, UI_FORM_FN_OPTIONS, UI_FORM_FN_PLACEHOLDER, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_FN_STYLES, UI_FORM_FN_SUBMIT_DISABLED, UI_FORM_FN_SUBMIT_TEXT, UI_FORM_FN_TITLE, UI_FORM_FN_VALUE, UI_FORM_GRID_COL_SPAN, UI_FORM_GRID_ROW_SPAN, UI_FORM_HIDDEN, UI_FORM_HINT, UI_FORM_LABEL_SINGULAR, UI_FORM_NS, UI_FORM_OPTIONS, UI_FORM_ORDER, UI_FORM_PLACEHOLDER, UI_FORM_PREFIX, UI_FORM_PREFIX_ICON, UI_FORM_PREFIX_REF, UI_FORM_PUSH_DOWN, UI_FORM_STYLES, UI_FORM_SUBMIT_TEXT, UI_FORM_SUFFIX, UI_FORM_SUFFIX_ICON, UI_FORM_SUFFIX_REF, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_LITERAL_LABEL, UI_NAV_GROUP, UI_NAV_HIDDEN, UI_NAV_ORDER, UI_TABLE_ATTR, UI_TABLE_CLASSES, UI_TABLE_COMPONENT, UI_TABLE_EXCLUDE, UI_TABLE_FN_ATTR, UI_TABLE_FN_CLASSES, UI_TABLE_FN_PREFIX, UI_TABLE_FN_STYLES, UI_TABLE_ORDER, UI_TABLE_SELECT_WITH, UI_TABLE_STYLES, UI_TABLE_TYPE, UI_TABLE_WIDTH, UI_TYPE, UI_VALUE_HELP, UI_VALUE_HELP_COLUMN, UI_VALUE_HELP_DISTINCT, UI_VALUE_HELP_FILL, UI_VALUE_LABEL, WF_ACTION_WITH_DATA };
|
|
@@ -113,5 +113,41 @@ function extractValueHelp(prop) {
|
|
|
113
113
|
node = (target.type.kind === "object" ? target.type : void 0)?.props.get(ref.field);
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
|
+
/** Field types that still pick from the value help: the picker, a dropdown or radio group of the dictionary. */
|
|
117
|
+
const PICKER_TYPES = new Set([
|
|
118
|
+
"ref",
|
|
119
|
+
"select",
|
|
120
|
+
"radio"
|
|
121
|
+
]);
|
|
122
|
+
/**
|
|
123
|
+
* Whether the form renders `prop` as a value-help picker: a scalar with a value
|
|
124
|
+
* help ({@link extractValueHelp}) whose `@ui.form.type` / `@ui.type`, if any,
|
|
125
|
+
* still picks from it (`tags` edits an array only, so a scalar ignores it) —
|
|
126
|
+
* or that names its own component (`@ui.form.component`, which wins over the
|
|
127
|
+
* type: a custom picker). Any other type, a custom type name included, makes a
|
|
128
|
+
* plain field (an FK typed `text`). A union-typed field renders as a union or a
|
|
129
|
+
* select instead.
|
|
130
|
+
*/
|
|
131
|
+
function isValueHelpPicker(prop) {
|
|
132
|
+
if (prop.type.kind !== "" || extractValueHelp(prop) === void 0) return false;
|
|
133
|
+
if (prop.metadata.has("ui.form.component")) return true;
|
|
134
|
+
const declared = prop.metadata.get("ui.form.type") ?? prop.metadata.get("ui.type");
|
|
135
|
+
return declared === void 0 || declared === "tags" || PICKER_TYPES.has(declared);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Whether `prop` holds a foreign key: it carries `@db.rel.FK`, or references a
|
|
139
|
+
* field that does (a reference chain, as {@link extractValueHelp} walks it).
|
|
140
|
+
*/
|
|
141
|
+
function isForeignKey(prop) {
|
|
142
|
+
let node = prop;
|
|
143
|
+
for (let depth = 0; node && depth < MAX_REF_CHAIN_DEPTH; depth++) {
|
|
144
|
+
if (node.metadata.has("db.rel.FK")) return true;
|
|
145
|
+
const target = node.ref?.type();
|
|
146
|
+
node = target?.type.kind === "object" ? target.type.props.get(node.ref.field) : void 0;
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
116
150
|
//#endregion
|
|
117
151
|
exports.extractValueHelp = extractValueHelp;
|
|
152
|
+
exports.isForeignKey = isForeignKey;
|
|
153
|
+
exports.isValueHelpPicker = isValueHelpPicker;
|
|
@@ -113,5 +113,39 @@ function extractValueHelp(prop) {
|
|
|
113
113
|
node = (target.type.kind === "object" ? target.type : void 0)?.props.get(ref.field);
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
|
+
/** Field types that still pick from the value help: the picker, a dropdown or radio group of the dictionary. */
|
|
117
|
+
const PICKER_TYPES = new Set([
|
|
118
|
+
"ref",
|
|
119
|
+
"select",
|
|
120
|
+
"radio"
|
|
121
|
+
]);
|
|
122
|
+
/**
|
|
123
|
+
* Whether the form renders `prop` as a value-help picker: a scalar with a value
|
|
124
|
+
* help ({@link extractValueHelp}) whose `@ui.form.type` / `@ui.type`, if any,
|
|
125
|
+
* still picks from it (`tags` edits an array only, so a scalar ignores it) —
|
|
126
|
+
* or that names its own component (`@ui.form.component`, which wins over the
|
|
127
|
+
* type: a custom picker). Any other type, a custom type name included, makes a
|
|
128
|
+
* plain field (an FK typed `text`). A union-typed field renders as a union or a
|
|
129
|
+
* select instead.
|
|
130
|
+
*/
|
|
131
|
+
function isValueHelpPicker(prop) {
|
|
132
|
+
if (prop.type.kind !== "" || extractValueHelp(prop) === void 0) return false;
|
|
133
|
+
if (prop.metadata.has("ui.form.component")) return true;
|
|
134
|
+
const declared = prop.metadata.get("ui.form.type") ?? prop.metadata.get("ui.type");
|
|
135
|
+
return declared === void 0 || declared === "tags" || PICKER_TYPES.has(declared);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Whether `prop` holds a foreign key: it carries `@db.rel.FK`, or references a
|
|
139
|
+
* field that does (a reference chain, as {@link extractValueHelp} walks it).
|
|
140
|
+
*/
|
|
141
|
+
function isForeignKey(prop) {
|
|
142
|
+
let node = prop;
|
|
143
|
+
for (let depth = 0; node && depth < MAX_REF_CHAIN_DEPTH; depth++) {
|
|
144
|
+
if (node.metadata.has("db.rel.FK")) return true;
|
|
145
|
+
const target = node.ref?.type();
|
|
146
|
+
node = target?.type.kind === "object" ? target.type.props.get(node.ref.field) : void 0;
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
116
150
|
//#endregion
|
|
117
|
-
export { extractValueHelp };
|
|
151
|
+
export { extractValueHelp, isForeignKey, isValueHelpPicker };
|