@atscript/ui 0.1.196 → 0.1.198
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 +15 -2
- package/dist/form/path-utils.mjs +15 -2
- package/dist/form/types.d.cts +8 -0
- package/dist/form/types.d.mts +8 -0
- package/dist/form/validate.cjs +108 -17
- package/dist/form/validate.d.cts +6 -0
- package/dist/form/validate.d.mts +6 -0
- package/dist/form/validate.mjs +110 -19
- 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 +9 -9
|
@@ -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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_annotation_keys = require("../shared/annotation-keys.cjs");
|
|
2
2
|
const require_field_resolver = require("../shared/field-resolver.cjs");
|
|
3
|
+
const require_extract_ref = require("../value-help/extract-ref.cjs");
|
|
3
4
|
let _atscript_typescript_utils = require("@atscript/typescript/utils");
|
|
4
5
|
//#region src/form/path-utils.ts
|
|
5
6
|
/**
|
|
@@ -132,9 +133,21 @@ function primitiveInitFallback(prop) {
|
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
function createFormData(type, resolver) {
|
|
135
|
-
|
|
136
|
+
const resolve = resolver ?? defaultValueResolver;
|
|
137
|
+
const pickers = /* @__PURE__ */ new Map();
|
|
138
|
+
const unset = [];
|
|
139
|
+
let value = (0, _atscript_typescript_utils.createDataFromAnnotatedType)(type, { mode: (prop, path) => {
|
|
140
|
+
const resolved = resolve(prop, path);
|
|
141
|
+
if (resolved !== void 0) return resolved;
|
|
142
|
+
if (pickers.get(path) === prop) unset.push(path);
|
|
143
|
+
else if (prop.type.kind === "object") {
|
|
144
|
+
for (const [key, child] of prop.type.props) if (require_extract_ref.isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
|
|
145
|
+
}
|
|
146
|
+
} });
|
|
136
147
|
if (value === void 0) value = primitiveInitFallback(type);
|
|
137
|
-
|
|
148
|
+
const data = { value };
|
|
149
|
+
for (const path of unset) deleteByPath(data, path);
|
|
150
|
+
return data;
|
|
138
151
|
}
|
|
139
152
|
const variantValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
140
153
|
function getVariantValidator(variant) {
|
package/dist/form/path-utils.mjs
CHANGED
|
@@ -1,5 +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 { isValueHelpPicker } from "../value-help/extract-ref.mjs";
|
|
3
4
|
import { createDataFromAnnotatedType, detectDiscriminator } from "@atscript/typescript/utils";
|
|
4
5
|
//#region src/form/path-utils.ts
|
|
5
6
|
/**
|
|
@@ -132,9 +133,21 @@ function primitiveInitFallback(prop) {
|
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
function createFormData(type, resolver) {
|
|
135
|
-
|
|
136
|
+
const resolve = resolver ?? defaultValueResolver;
|
|
137
|
+
const pickers = /* @__PURE__ */ new Map();
|
|
138
|
+
const unset = [];
|
|
139
|
+
let value = createDataFromAnnotatedType(type, { mode: (prop, path) => {
|
|
140
|
+
const resolved = resolve(prop, path);
|
|
141
|
+
if (resolved !== void 0) return resolved;
|
|
142
|
+
if (pickers.get(path) === prop) unset.push(path);
|
|
143
|
+
else if (prop.type.kind === "object") {
|
|
144
|
+
for (const [key, child] of prop.type.props) if (isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
|
|
145
|
+
}
|
|
146
|
+
} });
|
|
136
147
|
if (value === void 0) value = primitiveInitFallback(type);
|
|
137
|
-
|
|
148
|
+
const data = { value };
|
|
149
|
+
for (const path of unset) deleteByPath(data, path);
|
|
150
|
+
return data;
|
|
138
151
|
}
|
|
139
152
|
const variantValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
140
153
|
function getVariantValidator(variant) {
|
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
|
-
require("../shared/annotation-keys.cjs");
|
|
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");
|
|
@@ -43,8 +45,10 @@ const INSERT_CONTEXT = { mode: "insert" };
|
|
|
43
45
|
* - an absent `@db.default*` / `@db.column.version` / `@db.onUpdate.inc` value
|
|
44
46
|
* — delegated to `@atscript/db`'s own insert-mode rule, so the key list has
|
|
45
47
|
* one owner.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
+
* That rule passes every absent `@db.rel.FK` too (a nested related row can
|
|
49
|
+
* fill it), but a form fills the FK itself: the rule gets the field without
|
|
50
|
+
* its FK marker, so an empty FK still fails unless the database fills it (a
|
|
51
|
+
* `@db.default*` of its own).
|
|
48
52
|
*
|
|
49
53
|
* The db plugin reads its mode from the validation context, which here
|
|
50
54
|
* carries the form's `{ data, context }` — it gets a view of `ctx` with the
|
|
@@ -52,10 +56,50 @@ const INSERT_CONTEXT = { mode: "insert" };
|
|
|
52
56
|
*/
|
|
53
57
|
const serverManagedPlugin = (ctx, def, value) => {
|
|
54
58
|
if (def.metadata.has("db.column.derived") || require_skipped_nav.isSkippedNavField(def)) return true;
|
|
55
|
-
if (value !== void 0
|
|
56
|
-
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
59
|
+
if (value !== void 0) return void 0;
|
|
60
|
+
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), withoutFk(def), value);
|
|
57
61
|
};
|
|
58
62
|
/**
|
|
63
|
+
* A required value-help picker left unpicked — absent, `null` (its ×), or `""`
|
|
64
|
+
* for a string foreign key (never a key; a `@ui.valueHelp` binding on a plain
|
|
65
|
+
* string column takes `""`, as the server does) — fails with code `REQUIRED`
|
|
66
|
+
* and its `@expect.message`, else the code's text ("Must not be empty"), as
|
|
67
|
+
* `@meta.required` reports a missing value (a number field may not carry
|
|
68
|
+
* `@meta.required`). A `@meta.required` field keeps the validator's own rule
|
|
69
|
+
* (its message).
|
|
70
|
+
*
|
|
71
|
+
* Only for `pickers`: the props of the fields the form renders as a picker
|
|
72
|
+
* ({@link collectPickerProps}, or the per-field validator's own `picker`
|
|
73
|
+
* prop) — not an array item, a tuple position or a union branch, which keep
|
|
74
|
+
* the validation they had.
|
|
75
|
+
*
|
|
76
|
+
* Listed last, as the validator checks `@meta.required` after its plugins: an
|
|
77
|
+
* absent FK the database fills passes first ({@link serverManagedPlugin}), and
|
|
78
|
+
* a field's own `@ui.form.validate` words its error.
|
|
79
|
+
*/
|
|
80
|
+
function missingPickPlugin(pickers) {
|
|
81
|
+
return (ctx, def, value) => {
|
|
82
|
+
if (value !== void 0 && value !== null && value !== "") return void 0;
|
|
83
|
+
if (!pickers.has(def) || def.optional || def.metadata.has("meta.required")) return void 0;
|
|
84
|
+
if (value === "") {
|
|
85
|
+
if (!(def.type.designType === "string") || !require_extract_ref.isForeignKey(def)) return void 0;
|
|
86
|
+
}
|
|
87
|
+
const custom = def.metadata.get(require_annotation_keys.EXPECT_MESSAGE);
|
|
88
|
+
ctx.error(custom || _atscript_typescript_utils.VALIDATOR_MESSAGES.REQUIRED, void 0, void 0, {
|
|
89
|
+
code: "REQUIRED",
|
|
90
|
+
custom: !!custom
|
|
91
|
+
});
|
|
92
|
+
return false;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** A view of `def` without its `@db.rel.FK` marker — `def` itself when it has none. */
|
|
96
|
+
function withoutFk(def) {
|
|
97
|
+
if (!def.metadata.has("db.rel.FK")) return def;
|
|
98
|
+
const metadata = new Map(def.metadata);
|
|
99
|
+
metadata.delete(require_annotation_keys.DB_REL_FK);
|
|
100
|
+
return Object.create(def, { metadata: { value: metadata } });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
59
103
|
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
60
104
|
* computed view column (`metaFields`) or an edit form's update-stamped field
|
|
61
105
|
* (see `CreateFormDefOptions.mode`) carries no annotation the plugin above
|
|
@@ -63,27 +107,68 @@ const serverManagedPlugin = (ctx, def, value) => {
|
|
|
63
107
|
* it is not derived (an interface's stamp below a union of several types, too)
|
|
64
108
|
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
65
109
|
* visited once.
|
|
110
|
+
*
|
|
111
|
+
* Each field's prop is read from `def.type`, the type the validator walks,
|
|
112
|
+
* along the field's path — not from the field def: a def a Vue form holds
|
|
113
|
+
* reactive hands out proxies of the props of a compiled type, which the
|
|
114
|
+
* validator visits raw (fixed in 0.1.198).
|
|
66
115
|
*/
|
|
67
116
|
function collectDerivedProps(def) {
|
|
68
117
|
const derived = /* @__PURE__ */ new Set();
|
|
69
118
|
const kept = /* @__PURE__ */ new Set();
|
|
70
119
|
const seen = /* @__PURE__ */ new Set();
|
|
71
|
-
const
|
|
120
|
+
const walkFields = (fields, type) => {
|
|
72
121
|
for (const field of fields) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (field.derived) {
|
|
76
|
-
derived.add(field.prop);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
kept.add(field.prop);
|
|
80
|
-
walk(require_types.childFields(field));
|
|
122
|
+
const prop = field.path === "" ? type : type.type.kind === "object" ? require_create_form_def.resolveOriginalProp(type, field.path) : void 0;
|
|
123
|
+
if (prop) walkField(field, prop);
|
|
81
124
|
}
|
|
82
125
|
};
|
|
83
|
-
|
|
126
|
+
const walkField = (field, prop) => {
|
|
127
|
+
if (seen.has(field)) return;
|
|
128
|
+
seen.add(field);
|
|
129
|
+
if (field.derived) {
|
|
130
|
+
derived.add(prop);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
kept.add(prop);
|
|
134
|
+
if (require_types.isObjectField(field)) return walkFields(field.objectDef.fields, prop);
|
|
135
|
+
if (require_types.isArrayField(field)) return walkField(field.itemField, prop.type.of);
|
|
136
|
+
const items = prop.type.items ?? [prop];
|
|
137
|
+
if (require_types.isTupleField(field)) field.itemFields.forEach((f, i) => walkField(f, items[i] ?? prop));
|
|
138
|
+
else if (require_types.isUnionField(field)) field.unionVariants.forEach((v, i) => {
|
|
139
|
+
const item = items[i] ?? prop;
|
|
140
|
+
if (v.def) walkFields(v.def.fields, item);
|
|
141
|
+
if (v.itemField) walkField(v.itemField, item);
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
walkFields(def.fields, def.type);
|
|
84
145
|
for (const prop of kept) derived.delete(prop);
|
|
85
146
|
return derived;
|
|
86
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The props of the fields the form renders as a value-help picker
|
|
150
|
+
* ({@link FormFieldDef.picker}): every object property of `type` at any depth
|
|
151
|
+
* (a nested object, an object row of an array, a union's object variant) that
|
|
152
|
+
* {@link isValueHelpPicker} accepts — not an array item, a tuple position or a
|
|
153
|
+
* union branch. Read from the type the validator walks, so the props are the
|
|
154
|
+
* nodes it visits: a def a Vue form holds reactive hands out proxies of them.
|
|
155
|
+
* A recursive type is visited once per node.
|
|
156
|
+
*/
|
|
157
|
+
function collectPickerProps(type) {
|
|
158
|
+
const pickers = /* @__PURE__ */ new Set();
|
|
159
|
+
const seen = /* @__PURE__ */ new Set();
|
|
160
|
+
const walk = (node) => {
|
|
161
|
+
if (seen.has(node)) return;
|
|
162
|
+
seen.add(node);
|
|
163
|
+
const t = node.type;
|
|
164
|
+
if (t.kind === "object") for (const prop of t.props.values()) if (require_extract_ref.isValueHelpPicker(prop)) pickers.add(prop);
|
|
165
|
+
else walk(prop);
|
|
166
|
+
else if (t.kind === "array") walk(t.of);
|
|
167
|
+
else if (t.kind !== "") for (const item of t.items) walk(item);
|
|
168
|
+
};
|
|
169
|
+
walk(type);
|
|
170
|
+
return pickers;
|
|
171
|
+
}
|
|
87
172
|
let defaultValidatorPlugins = [];
|
|
88
173
|
/** Replace the default validator plugins applied to every form/field validator. */
|
|
89
174
|
function setDefaultValidatorPlugins(plugins) {
|
|
@@ -111,6 +196,7 @@ function getDefaultValidatorPlugins() {
|
|
|
111
196
|
function getFormValidator(def, opts) {
|
|
112
197
|
const derived = collectDerivedProps(def);
|
|
113
198
|
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
199
|
+
const pickers = collectPickerProps(def.type);
|
|
114
200
|
const options = {
|
|
115
201
|
unknownProps: "ignore",
|
|
116
202
|
...opts,
|
|
@@ -118,7 +204,8 @@ function getFormValidator(def, opts) {
|
|
|
118
204
|
...derived.size > 0 ? [derivedPlugin] : [],
|
|
119
205
|
serverManagedPlugin,
|
|
120
206
|
...defaultValidatorPlugins,
|
|
121
|
-
...opts?.plugins ?? []
|
|
207
|
+
...opts?.plugins ?? [],
|
|
208
|
+
...pickers.size > 0 ? [missingPickPlugin(pickers)] : []
|
|
122
209
|
]
|
|
123
210
|
};
|
|
124
211
|
const validator = new _atscript_typescript_utils.Validator(def.type, options);
|
|
@@ -177,7 +264,11 @@ function createFieldValidator(prop, opts) {
|
|
|
177
264
|
skipDescendantsPlugin,
|
|
178
265
|
serverManagedPlugin,
|
|
179
266
|
...defaultValidatorPlugins
|
|
180
|
-
] : [
|
|
267
|
+
] : [
|
|
268
|
+
serverManagedPlugin,
|
|
269
|
+
...defaultValidatorPlugins,
|
|
270
|
+
...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
|
|
271
|
+
] };
|
|
181
272
|
cached = new _atscript_typescript_utils.Validator(prop, options);
|
|
182
273
|
}
|
|
183
274
|
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 "../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
|
/**
|
|
@@ -43,8 +45,10 @@ const INSERT_CONTEXT = { mode: "insert" };
|
|
|
43
45
|
* - an absent `@db.default*` / `@db.column.version` / `@db.onUpdate.inc` value
|
|
44
46
|
* — delegated to `@atscript/db`'s own insert-mode rule, so the key list has
|
|
45
47
|
* one owner.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
+
* That rule passes every absent `@db.rel.FK` too (a nested related row can
|
|
49
|
+
* fill it), but a form fills the FK itself: the rule gets the field without
|
|
50
|
+
* its FK marker, so an empty FK still fails unless the database fills it (a
|
|
51
|
+
* `@db.default*` of its own).
|
|
48
52
|
*
|
|
49
53
|
* The db plugin reads its mode from the validation context, which here
|
|
50
54
|
* carries the form's `{ data, context }` — it gets a view of `ctx` with the
|
|
@@ -52,10 +56,50 @@ const INSERT_CONTEXT = { mode: "insert" };
|
|
|
52
56
|
*/
|
|
53
57
|
const serverManagedPlugin = (ctx, def, value) => {
|
|
54
58
|
if (def.metadata.has("db.column.derived") || isSkippedNavField(def)) return true;
|
|
55
|
-
if (value !== void 0
|
|
56
|
-
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
59
|
+
if (value !== void 0) return void 0;
|
|
60
|
+
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), withoutFk(def), value);
|
|
57
61
|
};
|
|
58
62
|
/**
|
|
63
|
+
* A required value-help picker left unpicked — absent, `null` (its ×), or `""`
|
|
64
|
+
* for a string foreign key (never a key; a `@ui.valueHelp` binding on a plain
|
|
65
|
+
* string column takes `""`, as the server does) — fails with code `REQUIRED`
|
|
66
|
+
* and its `@expect.message`, else the code's text ("Must not be empty"), as
|
|
67
|
+
* `@meta.required` reports a missing value (a number field may not carry
|
|
68
|
+
* `@meta.required`). A `@meta.required` field keeps the validator's own rule
|
|
69
|
+
* (its message).
|
|
70
|
+
*
|
|
71
|
+
* Only for `pickers`: the props of the fields the form renders as a picker
|
|
72
|
+
* ({@link collectPickerProps}, or the per-field validator's own `picker`
|
|
73
|
+
* prop) — not an array item, a tuple position or a union branch, which keep
|
|
74
|
+
* the validation they had.
|
|
75
|
+
*
|
|
76
|
+
* Listed last, as the validator checks `@meta.required` after its plugins: an
|
|
77
|
+
* absent FK the database fills passes first ({@link serverManagedPlugin}), and
|
|
78
|
+
* a field's own `@ui.form.validate` words its error.
|
|
79
|
+
*/
|
|
80
|
+
function missingPickPlugin(pickers) {
|
|
81
|
+
return (ctx, def, value) => {
|
|
82
|
+
if (value !== void 0 && value !== null && value !== "") return void 0;
|
|
83
|
+
if (!pickers.has(def) || def.optional || def.metadata.has("meta.required")) return void 0;
|
|
84
|
+
if (value === "") {
|
|
85
|
+
if (!(def.type.designType === "string") || !isForeignKey(def)) return void 0;
|
|
86
|
+
}
|
|
87
|
+
const custom = def.metadata.get(EXPECT_MESSAGE);
|
|
88
|
+
ctx.error(custom || VALIDATOR_MESSAGES.REQUIRED, void 0, void 0, {
|
|
89
|
+
code: "REQUIRED",
|
|
90
|
+
custom: !!custom
|
|
91
|
+
});
|
|
92
|
+
return false;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** A view of `def` without its `@db.rel.FK` marker — `def` itself when it has none. */
|
|
96
|
+
function withoutFk(def) {
|
|
97
|
+
if (!def.metadata.has("db.rel.FK")) return def;
|
|
98
|
+
const metadata = new Map(def.metadata);
|
|
99
|
+
metadata.delete(DB_REL_FK);
|
|
100
|
+
return Object.create(def, { metadata: { value: metadata } });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
59
103
|
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
60
104
|
* computed view column (`metaFields`) or an edit form's update-stamped field
|
|
61
105
|
* (see `CreateFormDefOptions.mode`) carries no annotation the plugin above
|
|
@@ -63,27 +107,68 @@ const serverManagedPlugin = (ctx, def, value) => {
|
|
|
63
107
|
* it is not derived (an interface's stamp below a union of several types, too)
|
|
64
108
|
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
65
109
|
* visited once.
|
|
110
|
+
*
|
|
111
|
+
* Each field's prop is read from `def.type`, the type the validator walks,
|
|
112
|
+
* along the field's path — not from the field def: a def a Vue form holds
|
|
113
|
+
* reactive hands out proxies of the props of a compiled type, which the
|
|
114
|
+
* validator visits raw (fixed in 0.1.198).
|
|
66
115
|
*/
|
|
67
116
|
function collectDerivedProps(def) {
|
|
68
117
|
const derived = /* @__PURE__ */ new Set();
|
|
69
118
|
const kept = /* @__PURE__ */ new Set();
|
|
70
119
|
const seen = /* @__PURE__ */ new Set();
|
|
71
|
-
const
|
|
120
|
+
const walkFields = (fields, type) => {
|
|
72
121
|
for (const field of fields) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (field.derived) {
|
|
76
|
-
derived.add(field.prop);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
kept.add(field.prop);
|
|
80
|
-
walk(childFields(field));
|
|
122
|
+
const prop = field.path === "" ? type : type.type.kind === "object" ? resolveOriginalProp(type, field.path) : void 0;
|
|
123
|
+
if (prop) walkField(field, prop);
|
|
81
124
|
}
|
|
82
125
|
};
|
|
83
|
-
|
|
126
|
+
const walkField = (field, prop) => {
|
|
127
|
+
if (seen.has(field)) return;
|
|
128
|
+
seen.add(field);
|
|
129
|
+
if (field.derived) {
|
|
130
|
+
derived.add(prop);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
kept.add(prop);
|
|
134
|
+
if (isObjectField(field)) return walkFields(field.objectDef.fields, prop);
|
|
135
|
+
if (isArrayField(field)) return walkField(field.itemField, prop.type.of);
|
|
136
|
+
const items = prop.type.items ?? [prop];
|
|
137
|
+
if (isTupleField(field)) field.itemFields.forEach((f, i) => walkField(f, items[i] ?? prop));
|
|
138
|
+
else if (isUnionField(field)) field.unionVariants.forEach((v, i) => {
|
|
139
|
+
const item = items[i] ?? prop;
|
|
140
|
+
if (v.def) walkFields(v.def.fields, item);
|
|
141
|
+
if (v.itemField) walkField(v.itemField, item);
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
walkFields(def.fields, def.type);
|
|
84
145
|
for (const prop of kept) derived.delete(prop);
|
|
85
146
|
return derived;
|
|
86
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The props of the fields the form renders as a value-help picker
|
|
150
|
+
* ({@link FormFieldDef.picker}): every object property of `type` at any depth
|
|
151
|
+
* (a nested object, an object row of an array, a union's object variant) that
|
|
152
|
+
* {@link isValueHelpPicker} accepts — not an array item, a tuple position or a
|
|
153
|
+
* union branch. Read from the type the validator walks, so the props are the
|
|
154
|
+
* nodes it visits: a def a Vue form holds reactive hands out proxies of them.
|
|
155
|
+
* A recursive type is visited once per node.
|
|
156
|
+
*/
|
|
157
|
+
function collectPickerProps(type) {
|
|
158
|
+
const pickers = /* @__PURE__ */ new Set();
|
|
159
|
+
const seen = /* @__PURE__ */ new Set();
|
|
160
|
+
const walk = (node) => {
|
|
161
|
+
if (seen.has(node)) return;
|
|
162
|
+
seen.add(node);
|
|
163
|
+
const t = node.type;
|
|
164
|
+
if (t.kind === "object") for (const prop of t.props.values()) if (isValueHelpPicker(prop)) pickers.add(prop);
|
|
165
|
+
else walk(prop);
|
|
166
|
+
else if (t.kind === "array") walk(t.of);
|
|
167
|
+
else if (t.kind !== "") for (const item of t.items) walk(item);
|
|
168
|
+
};
|
|
169
|
+
walk(type);
|
|
170
|
+
return pickers;
|
|
171
|
+
}
|
|
87
172
|
let defaultValidatorPlugins = [];
|
|
88
173
|
/** Replace the default validator plugins applied to every form/field validator. */
|
|
89
174
|
function setDefaultValidatorPlugins(plugins) {
|
|
@@ -111,6 +196,7 @@ function getDefaultValidatorPlugins() {
|
|
|
111
196
|
function getFormValidator(def, opts) {
|
|
112
197
|
const derived = collectDerivedProps(def);
|
|
113
198
|
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
199
|
+
const pickers = collectPickerProps(def.type);
|
|
114
200
|
const options = {
|
|
115
201
|
unknownProps: "ignore",
|
|
116
202
|
...opts,
|
|
@@ -118,7 +204,8 @@ function getFormValidator(def, opts) {
|
|
|
118
204
|
...derived.size > 0 ? [derivedPlugin] : [],
|
|
119
205
|
serverManagedPlugin,
|
|
120
206
|
...defaultValidatorPlugins,
|
|
121
|
-
...opts?.plugins ?? []
|
|
207
|
+
...opts?.plugins ?? [],
|
|
208
|
+
...pickers.size > 0 ? [missingPickPlugin(pickers)] : []
|
|
122
209
|
]
|
|
123
210
|
};
|
|
124
211
|
const validator = new Validator(def.type, options);
|
|
@@ -177,7 +264,11 @@ function createFieldValidator(prop, opts) {
|
|
|
177
264
|
skipDescendantsPlugin,
|
|
178
265
|
serverManagedPlugin,
|
|
179
266
|
...defaultValidatorPlugins
|
|
180
|
-
] : [
|
|
267
|
+
] : [
|
|
268
|
+
serverManagedPlugin,
|
|
269
|
+
...defaultValidatorPlugins,
|
|
270
|
+
...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
|
|
271
|
+
] };
|
|
181
272
|
cached = new Validator(prop, options);
|
|
182
273
|
}
|
|
183
274
|
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.198",
|
|
4
4
|
"description": "Framework-agnostic runtime for form and table definitions from atscript annotated types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -52,20 +52,20 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@atscript/db-client": "^0.1.
|
|
55
|
+
"@atscript/db-client": "^0.1.202"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@atscript/core": "^0.2.
|
|
59
|
-
"@atscript/db": "^0.1.
|
|
60
|
-
"@atscript/typescript": "^0.2.
|
|
58
|
+
"@atscript/core": "^0.2.27",
|
|
59
|
+
"@atscript/db": "^0.1.202",
|
|
60
|
+
"@atscript/typescript": "^0.2.27",
|
|
61
61
|
"@uniqu/core": "^0.2.2",
|
|
62
|
-
"unplugin-atscript": "^0.2.
|
|
62
|
+
"unplugin-atscript": "^0.2.27",
|
|
63
63
|
"vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"@atscript/core": "^0.2.
|
|
67
|
-
"@atscript/db": "^0.1.
|
|
68
|
-
"@atscript/typescript": "^0.2.
|
|
66
|
+
"@atscript/core": "^0.2.27",
|
|
67
|
+
"@atscript/db": "^0.1.202",
|
|
68
|
+
"@atscript/typescript": "^0.2.27"
|
|
69
69
|
},
|
|
70
70
|
"scripts": {
|
|
71
71
|
"build": "vp pack",
|