@atscript/ui 0.1.162 → 0.1.164
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/client-factory.cjs +27 -0
- package/dist/client-factory.d.cts +53 -0
- package/dist/client-factory.d.mts +53 -0
- package/dist/client-factory.mjs +25 -0
- package/dist/form/apply.cjs +38 -0
- package/dist/form/apply.d.cts +36 -0
- package/dist/form/apply.d.mts +36 -0
- package/dist/form/apply.mjs +38 -0
- package/dist/form/clone.cjs +30 -0
- package/dist/form/clone.d.cts +23 -0
- package/dist/form/clone.d.mts +23 -0
- package/dist/form/clone.mjs +30 -0
- package/dist/form/create-form-def.cjs +440 -0
- package/dist/form/create-form-def.d.cts +67 -0
- package/dist/form/create-form-def.d.mts +67 -0
- package/dist/form/create-form-def.mjs +438 -0
- package/dist/form/decimal-format.cjs +324 -0
- package/dist/form/decimal-format.d.cts +86 -0
- package/dist/form/decimal-format.d.mts +86 -0
- package/dist/form/decimal-format.mjs +315 -0
- package/dist/form/diff.cjs +458 -0
- package/dist/form/diff.d.cts +80 -0
- package/dist/form/diff.d.mts +80 -0
- package/dist/form/diff.mjs +457 -0
- package/dist/form/dirty.cjs +65 -0
- package/dist/form/dirty.d.cts +47 -0
- package/dist/form/dirty.d.mts +47 -0
- package/dist/form/dirty.mjs +64 -0
- package/dist/form/error-utils.cjs +97 -0
- package/dist/form/error-utils.d.cts +58 -0
- package/dist/form/error-utils.d.mts +58 -0
- package/dist/form/error-utils.mjs +92 -0
- package/dist/form/form-actions.cjs +30 -0
- package/dist/form/form-actions.d.cts +17 -0
- package/dist/form/form-actions.d.mts +17 -0
- package/dist/form/form-actions.mjs +30 -0
- package/dist/form/grid.cjs +69 -0
- package/dist/form/grid.d.cts +49 -0
- package/dist/form/grid.d.mts +49 -0
- package/dist/form/grid.mjs +64 -0
- package/dist/form/labels.cjs +10 -0
- package/dist/form/labels.d.cts +7 -0
- package/dist/form/labels.d.mts +7 -0
- package/dist/form/labels.mjs +10 -0
- package/dist/form/measurement.cjs +20 -0
- package/dist/form/measurement.d.cts +29 -0
- package/dist/form/measurement.d.mts +29 -0
- package/dist/form/measurement.mjs +20 -0
- package/dist/form/path-utils.cjs +182 -0
- package/dist/form/path-utils.d.cts +47 -0
- package/dist/form/path-utils.d.mts +47 -0
- package/dist/form/path-utils.mjs +175 -0
- package/dist/form/rebase.cjs +89 -0
- package/dist/form/rebase.d.cts +61 -0
- package/dist/form/rebase.d.mts +61 -0
- package/dist/form/rebase.mjs +89 -0
- package/dist/form/skipped-nav.cjs +100 -0
- package/dist/form/skipped-nav.d.cts +13 -0
- package/dist/form/skipped-nav.d.mts +13 -0
- package/dist/form/skipped-nav.mjs +98 -0
- package/dist/form/types.cjs +40 -0
- package/dist/form/types.d.cts +133 -0
- package/dist/form/types.d.mts +133 -0
- package/dist/form/types.mjs +36 -0
- package/dist/form/union-detect.cjs +46 -0
- package/dist/form/union-detect.d.cts +22 -0
- package/dist/form/union-detect.d.mts +22 -0
- package/dist/form/union-detect.mjs +46 -0
- package/dist/form/validate.cjs +150 -0
- package/dist/form/validate.d.cts +50 -0
- package/dist/form/validate.d.mts +50 -0
- package/dist/form/validate.mjs +147 -0
- package/dist/index.cjs +217 -3360
- package/dist/index.d.cts +38 -1693
- package/dist/index.d.mts +38 -1693
- package/dist/index.mjs +35 -3179
- package/dist/nav/model-routes.cjs +59 -0
- package/dist/nav/model-routes.d.cts +33 -0
- package/dist/nav/model-routes.d.mts +33 -0
- package/dist/nav/model-routes.mjs +59 -0
- package/dist/plugin/annotations.cjs +577 -0
- package/dist/plugin/annotations.mjs +577 -0
- package/dist/plugin/primitives.cjs +30 -0
- package/dist/plugin/primitives.mjs +30 -0
- package/dist/plugin/value-help-validation.cjs +419 -0
- package/dist/plugin/value-help-validation.mjs +413 -0
- package/dist/plugin.cjs +4 -1001
- package/dist/plugin.mjs +2 -999
- package/dist/shared/annotation-keys.cjs +189 -0
- package/dist/shared/annotation-keys.d.cts +82 -0
- package/dist/shared/annotation-keys.d.mts +82 -0
- package/dist/shared/annotation-keys.mjs +97 -0
- package/dist/shared/field-resolver.cjs +123 -0
- package/dist/shared/field-resolver.d.cts +77 -0
- package/dist/shared/field-resolver.d.mts +77 -0
- package/dist/shared/field-resolver.mjs +110 -0
- package/dist/shared/meta-cache.cjs +244 -0
- package/dist/shared/meta-cache.d.cts +110 -0
- package/dist/shared/meta-cache.d.mts +110 -0
- package/dist/shared/meta-cache.mjs +237 -0
- package/dist/shared/str.cjs +19 -0
- package/dist/shared/str.d.cts +7 -0
- package/dist/shared/str.d.mts +7 -0
- package/dist/shared/str.mjs +17 -0
- package/dist/table/column-resolver.cjs +17 -0
- package/dist/table/column-resolver.d.cts +11 -0
- package/dist/table/column-resolver.d.mts +11 -0
- package/dist/table/column-resolver.mjs +15 -0
- package/dist/table/create-table-def.cjs +233 -0
- package/dist/table/create-table-def.d.cts +15 -0
- package/dist/table/create-table-def.d.mts +15 -0
- package/dist/table/create-table-def.mjs +233 -0
- package/dist/table/navigate-href.cjs +45 -0
- package/dist/table/navigate-href.d.cts +31 -0
- package/dist/table/navigate-href.d.mts +31 -0
- package/dist/table/navigate-href.mjs +45 -0
- package/dist/table/types.d.cts +287 -0
- package/dist/table/types.d.mts +287 -0
- package/dist/table/value-kind.cjs +20 -0
- package/dist/table/value-kind.d.cts +15 -0
- package/dist/table/value-kind.d.mts +15 -0
- package/dist/table/value-kind.mjs +19 -0
- package/dist/value-help/dict-paths.cjs +17 -0
- package/dist/value-help/dict-paths.d.cts +11 -0
- package/dist/value-help/dict-paths.d.mts +11 -0
- package/dist/value-help/dict-paths.mjs +17 -0
- package/dist/value-help/distinct-client.cjs +51 -0
- package/dist/value-help/distinct-client.d.cts +40 -0
- package/dist/value-help/distinct-client.d.mts +40 -0
- package/dist/value-help/distinct-client.mjs +50 -0
- package/dist/value-help/extract-literals.cjs +76 -0
- package/dist/value-help/extract-literals.d.cts +32 -0
- package/dist/value-help/extract-literals.d.mts +32 -0
- package/dist/value-help/extract-literals.mjs +74 -0
- package/dist/value-help/extract-ref.cjs +100 -0
- package/dist/value-help/extract-ref.d.cts +37 -0
- package/dist/value-help/extract-ref.d.mts +37 -0
- package/dist/value-help/extract-ref.mjs +100 -0
- package/dist/value-help/filter-tree.cjs +45 -0
- package/dist/value-help/filter-tree.mjs +45 -0
- package/dist/value-help/index.d.mts +8 -0
- package/dist/value-help/resolve-options.cjs +64 -0
- package/dist/value-help/resolve-options.d.cts +35 -0
- package/dist/value-help/resolve-options.d.mts +35 -0
- package/dist/value-help/resolve-options.mjs +60 -0
- package/dist/value-help/resolve.cjs +62 -0
- package/dist/value-help/resolve.d.cts +31 -0
- package/dist/value-help/resolve.d.mts +31 -0
- package/dist/value-help/resolve.mjs +61 -0
- package/dist/value-help/types.d.cts +36 -0
- package/dist/value-help/types.d.mts +36 -0
- package/dist/value-help/value-help-client.cjs +136 -0
- package/dist/value-help/value-help-client.d.cts +67 -0
- package/dist/value-help/value-help-client.d.mts +67 -0
- package/dist/value-help/value-help-client.mjs +134 -0
- package/package.json +10 -10
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { TFormEntryOptions, ValueHelpInfo } from "../value-help/types.mjs";
|
|
2
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/form/types.d.ts
|
|
5
|
+
/** Form action metadata — the action id and display label. */
|
|
6
|
+
interface TFormAction {
|
|
7
|
+
id: string;
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A single form field definition — thin pointer to the ATScript prop.
|
|
12
|
+
*
|
|
13
|
+
* All metadata (label, disabled, options, etc.) lives in `prop.metadata`
|
|
14
|
+
* and is resolved on demand via resolve utilities.
|
|
15
|
+
*/
|
|
16
|
+
interface FormFieldDef {
|
|
17
|
+
/** Dot-separated path relative to the parent data context. `''` = root. */
|
|
18
|
+
path: string;
|
|
19
|
+
prop: TAtscriptAnnotatedType;
|
|
20
|
+
/**
|
|
21
|
+
* Render-key the renderer dispatches on. For structured kinds (array,
|
|
22
|
+
* object, tuple, multi-variant union) this stays the kind name so that
|
|
23
|
+
* `isArrayField`/`isObjectField`/`isUnionField`/`isTupleField` keep
|
|
24
|
+
* working downstream (validator resetValue, child path provide, AsArray
|
|
25
|
+
* recursion). Primitives may store an `@ui.form.type`/`@ui.type`
|
|
26
|
+
* override here directly — for those there is no structural behaviour
|
|
27
|
+
* to preserve.
|
|
28
|
+
*/
|
|
29
|
+
type: string;
|
|
30
|
+
/**
|
|
31
|
+
* Optional `@ui.form.type` (or `@ui.type`) override for structured
|
|
32
|
+
* kinds. When set, the renderer looks this up in the `types` map FIRST
|
|
33
|
+
* and falls back to `type` only when the map has no entry. Letting the
|
|
34
|
+
* override live in a separate field keeps the structural type guards
|
|
35
|
+
* (`isArrayField` et al.) on `type` honest while still allowing a
|
|
36
|
+
* consumer to attach a flat custom widget to e.g. a `string[]`.
|
|
37
|
+
*/
|
|
38
|
+
customType?: string;
|
|
39
|
+
phantom: boolean;
|
|
40
|
+
name: string;
|
|
41
|
+
/** True when no `ui.fn.*` metadata keys exist. Vue perf flag. */
|
|
42
|
+
allStatic: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* `@ui.form.pushDown` — render this field below the submit button in its
|
|
45
|
+
* own grid, instead of in the main field grid above submit. Set on
|
|
46
|
+
* top-level form fields (typically secondary `ui.action` links). The field
|
|
47
|
+
* stays in `fields[]` for validation/data; only its render slot moves.
|
|
48
|
+
*/
|
|
49
|
+
pushDown: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Value-help target for an FK field — `{ url, targetField }` resolved once by
|
|
52
|
+
* `extractValueHelp` while the def is built (since 0.1.134; mirrors
|
|
53
|
+
* `ColumnDef.valueHelpInfo` on the table side). `undefined` for every field
|
|
54
|
+
* that is not an FK. Renderers read this instead of re-walking the
|
|
55
|
+
* annotations per field, which also keeps the annotation dependency out of
|
|
56
|
+
* the component layer.
|
|
57
|
+
*/
|
|
58
|
+
valueHelpInfo?: ValueHelpInfo;
|
|
59
|
+
/**
|
|
60
|
+
* Server-owned field: a `@db.column.derived` one (read from the type
|
|
61
|
+
* metadata), a computed view column (`CreateFormDefOptions.metaFields`) or,
|
|
62
|
+
* in an edit form, an update-stamped one (see `CreateFormDefOptions.mode`).
|
|
63
|
+
* Rendered read-only, exempt from form validation and never sent by
|
|
64
|
+
* `buildFormDiff` (the server drops or overwrites a written value anyway).
|
|
65
|
+
* Since 0.1.144.
|
|
66
|
+
*/
|
|
67
|
+
derived?: true;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Complete form definition — produced by createFormDef().
|
|
71
|
+
* Form-level metadata (title, submit) resolved on demand via resolveFormProp.
|
|
72
|
+
*/
|
|
73
|
+
interface FormDef {
|
|
74
|
+
type: TAtscriptAnnotatedType;
|
|
75
|
+
/** Root field representing the entire form. For interface types this is `type='object'`; for single-type forms it is a leaf field. */
|
|
76
|
+
rootField: FormFieldDef;
|
|
77
|
+
fields: FormFieldDef[];
|
|
78
|
+
/**
|
|
79
|
+
* Render partition of `fields` by `@ui.form.pushDown`, precomputed once so
|
|
80
|
+
* the renderer never re-scans per frame. `mainFields` (above submit) plus
|
|
81
|
+
* `pushDownFields` (below submit) cover `fields` exactly once. In the common
|
|
82
|
+
* case nothing is pushed down: `mainFields === fields` (same ref) and
|
|
83
|
+
* `pushDownFields` is empty.
|
|
84
|
+
*/
|
|
85
|
+
mainFields: FormFieldDef[];
|
|
86
|
+
pushDownFields: FormFieldDef[];
|
|
87
|
+
flatMap: Map<string, TAtscriptAnnotatedType>;
|
|
88
|
+
}
|
|
89
|
+
/** One branch of a union type — used by union fields and union array items. */
|
|
90
|
+
interface FormUnionVariant {
|
|
91
|
+
/** Display label — from @meta.label or auto-generated (e.g. "1. String") */
|
|
92
|
+
label: string;
|
|
93
|
+
/** The annotated type for this variant */
|
|
94
|
+
type: TAtscriptAnnotatedType;
|
|
95
|
+
/** Pre-built FormDef for object variants (undefined for primitives) */
|
|
96
|
+
def?: FormDef;
|
|
97
|
+
/** Pre-built field def for primitive variants (undefined for objects) */
|
|
98
|
+
itemField?: FormFieldDef;
|
|
99
|
+
/** Design type for primitive variants ('string', 'number', 'boolean') */
|
|
100
|
+
designType?: string;
|
|
101
|
+
}
|
|
102
|
+
/** Extended field def for array-typed fields. */
|
|
103
|
+
interface FormArrayFieldDef extends FormFieldDef {
|
|
104
|
+
/** ATScript annotated type of array items (from TAtscriptTypeArray.of) */
|
|
105
|
+
itemType: TAtscriptAnnotatedType;
|
|
106
|
+
/** Pre-built template field def for items (path=''). */
|
|
107
|
+
itemField: FormFieldDef;
|
|
108
|
+
}
|
|
109
|
+
/** Extended field def for object (interface/type) nested fields. */
|
|
110
|
+
interface FormObjectFieldDef extends FormFieldDef {
|
|
111
|
+
/** Pre-built FormDef for the nested object's fields */
|
|
112
|
+
objectDef: FormDef;
|
|
113
|
+
}
|
|
114
|
+
/** Extended field def for union fields — standalone union props and union array items. */
|
|
115
|
+
interface FormUnionFieldDef extends FormFieldDef {
|
|
116
|
+
/** Available union branches. */
|
|
117
|
+
unionVariants: FormUnionVariant[];
|
|
118
|
+
}
|
|
119
|
+
/** Extended field def for tuple fields — fixed-length with typed positions. */
|
|
120
|
+
interface FormTupleFieldDef extends FormFieldDef {
|
|
121
|
+
/** Pre-built field defs, one per tuple position. */
|
|
122
|
+
itemFields: FormFieldDef[];
|
|
123
|
+
}
|
|
124
|
+
/** Type guard: checks if a field def is an array field. */
|
|
125
|
+
declare function isArrayField(field: FormFieldDef): field is FormArrayFieldDef;
|
|
126
|
+
/** Type guard: checks if a field def is an object field. */
|
|
127
|
+
declare function isObjectField(field: FormFieldDef): field is FormObjectFieldDef;
|
|
128
|
+
/** Type guard: checks if a field def is a union field. */
|
|
129
|
+
declare function isUnionField(field: FormFieldDef): field is FormUnionFieldDef;
|
|
130
|
+
/** Type guard: checks if a field def is a tuple field. */
|
|
131
|
+
declare function isTupleField(field: FormFieldDef): field is FormTupleFieldDef;
|
|
132
|
+
//#endregion
|
|
133
|
+
export { FormArrayFieldDef, FormDef, FormFieldDef, FormObjectFieldDef, FormTupleFieldDef, FormUnionFieldDef, FormUnionVariant, TFormAction, isArrayField, isObjectField, isTupleField, isUnionField };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/form/types.ts
|
|
2
|
+
/** Type guard: checks if a field def is an array field. */
|
|
3
|
+
function isArrayField(field) {
|
|
4
|
+
return field.type === "array";
|
|
5
|
+
}
|
|
6
|
+
/** Type guard: checks if a field def is an object field. */
|
|
7
|
+
function isObjectField(field) {
|
|
8
|
+
return field.type === "object";
|
|
9
|
+
}
|
|
10
|
+
/** Type guard: checks if a field def is a union field. */
|
|
11
|
+
function isUnionField(field) {
|
|
12
|
+
return field.type === "union";
|
|
13
|
+
}
|
|
14
|
+
/** Type guard: checks if a field def is a tuple field. */
|
|
15
|
+
function isTupleField(field) {
|
|
16
|
+
return field.type === "tuple";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The field defs directly below `field`: an object's fields, an array's item,
|
|
20
|
+
* a tuple's items, each union variant's fields and item. A recursive type's
|
|
21
|
+
* def graph is cyclic: a walk over it tracks the fields it has seen.
|
|
22
|
+
*/
|
|
23
|
+
function childFields(field) {
|
|
24
|
+
if (isObjectField(field)) return field.objectDef.fields;
|
|
25
|
+
if (isArrayField(field)) return [field.itemField];
|
|
26
|
+
if (isTupleField(field)) return field.itemFields;
|
|
27
|
+
if (!isUnionField(field)) return [];
|
|
28
|
+
const children = [];
|
|
29
|
+
for (const v of field.unionVariants) {
|
|
30
|
+
if (v.def) children.push(...v.def.fields);
|
|
31
|
+
if (v.itemField) children.push(v.itemField);
|
|
32
|
+
}
|
|
33
|
+
return children;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { childFields, isArrayField, isObjectField, isTupleField, isUnionField };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const require_types = require("./types.cjs");
|
|
2
|
+
const require_path_utils = require("./path-utils.cjs");
|
|
3
|
+
//#region src/form/union-detect.ts
|
|
4
|
+
/**
|
|
5
|
+
* True when ANY union field in the form resolves to a DIFFERENT discriminated
|
|
6
|
+
* variant between two wrapped data containers. A variant picker typically
|
|
7
|
+
* detects its variant index once at setup and keys the variant subtree on it,
|
|
8
|
+
* so a rebase that lands a different variant (via conflict OR an upstream-only
|
|
9
|
+
* switch) needs a remount to re-detect. This walks union + nested-object fields
|
|
10
|
+
* and compares `detectUnionVariant` at each union path.
|
|
11
|
+
*
|
|
12
|
+
* Scope note (pragmatic): walks standalone + nested-OBJECT union fields. Unions
|
|
13
|
+
* nested INSIDE array items are not walked — an array renderer that keeps a
|
|
14
|
+
* stable per-item key across in-place value mutations would not remount an
|
|
15
|
+
* existing row's picker on an upstream-driven variant flip, but that collision
|
|
16
|
+
* (a 3-way rebase landing a different union variant inside an unchanged array
|
|
17
|
+
* row) is a rare edge. TODO: extend to array-item unions if a real consumer
|
|
18
|
+
* hits a stuck picker inside an array row.
|
|
19
|
+
*/
|
|
20
|
+
function unionVariantChanged(def, before, after) {
|
|
21
|
+
return walkUnionFields(def.fields, "", before, after);
|
|
22
|
+
}
|
|
23
|
+
function walkUnionFields(fields, prefix, before, after) {
|
|
24
|
+
for (const field of fields) {
|
|
25
|
+
if (field.phantom) continue;
|
|
26
|
+
const fullPath = field.path ? prefix ? `${prefix}.${field.path}` : field.path : prefix;
|
|
27
|
+
if (require_types.isUnionField(field)) {
|
|
28
|
+
const variants = field.unionVariants;
|
|
29
|
+
if (variants.length > 1) {
|
|
30
|
+
if (require_path_utils.detectUnionVariant(require_path_utils.getByPath(before, fullPath), variants) !== require_path_utils.detectUnionVariant(require_path_utils.getByPath(after, fullPath), variants)) return true;
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (require_types.isObjectField(field) && (isObject(before, fullPath) || isObject(after, fullPath))) {
|
|
35
|
+
const objectDef = field.objectDef;
|
|
36
|
+
if (walkUnionFields(objectDef.fields, fullPath, before, after)) return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function isObject(data, path) {
|
|
42
|
+
const v = require_path_utils.getByPath(data, path);
|
|
43
|
+
return typeof v === "object" && v !== null;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.unionVariantChanged = unionVariantChanged;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FormDef } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/union-detect.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* True when ANY union field in the form resolves to a DIFFERENT discriminated
|
|
6
|
+
* variant between two wrapped data containers. A variant picker typically
|
|
7
|
+
* detects its variant index once at setup and keys the variant subtree on it,
|
|
8
|
+
* so a rebase that lands a different variant (via conflict OR an upstream-only
|
|
9
|
+
* switch) needs a remount to re-detect. This walks union + nested-object fields
|
|
10
|
+
* and compares `detectUnionVariant` at each union path.
|
|
11
|
+
*
|
|
12
|
+
* Scope note (pragmatic): walks standalone + nested-OBJECT union fields. Unions
|
|
13
|
+
* nested INSIDE array items are not walked — an array renderer that keeps a
|
|
14
|
+
* stable per-item key across in-place value mutations would not remount an
|
|
15
|
+
* existing row's picker on an upstream-driven variant flip, but that collision
|
|
16
|
+
* (a 3-way rebase landing a different union variant inside an unchanged array
|
|
17
|
+
* row) is a rare edge. TODO: extend to array-item unions if a real consumer
|
|
18
|
+
* hits a stuck picker inside an array row.
|
|
19
|
+
*/
|
|
20
|
+
declare function unionVariantChanged(def: FormDef, before: Record<string, unknown>, after: Record<string, unknown>): boolean;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { unionVariantChanged };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FormDef } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/form/union-detect.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* True when ANY union field in the form resolves to a DIFFERENT discriminated
|
|
6
|
+
* variant between two wrapped data containers. A variant picker typically
|
|
7
|
+
* detects its variant index once at setup and keys the variant subtree on it,
|
|
8
|
+
* so a rebase that lands a different variant (via conflict OR an upstream-only
|
|
9
|
+
* switch) needs a remount to re-detect. This walks union + nested-object fields
|
|
10
|
+
* and compares `detectUnionVariant` at each union path.
|
|
11
|
+
*
|
|
12
|
+
* Scope note (pragmatic): walks standalone + nested-OBJECT union fields. Unions
|
|
13
|
+
* nested INSIDE array items are not walked — an array renderer that keeps a
|
|
14
|
+
* stable per-item key across in-place value mutations would not remount an
|
|
15
|
+
* existing row's picker on an upstream-driven variant flip, but that collision
|
|
16
|
+
* (a 3-way rebase landing a different union variant inside an unchanged array
|
|
17
|
+
* row) is a rare edge. TODO: extend to array-item unions if a real consumer
|
|
18
|
+
* hits a stuck picker inside an array row.
|
|
19
|
+
*/
|
|
20
|
+
declare function unionVariantChanged(def: FormDef, before: Record<string, unknown>, after: Record<string, unknown>): boolean;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { unionVariantChanged };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isObjectField, isUnionField } from "./types.mjs";
|
|
2
|
+
import { detectUnionVariant, getByPath } from "./path-utils.mjs";
|
|
3
|
+
//#region src/form/union-detect.ts
|
|
4
|
+
/**
|
|
5
|
+
* True when ANY union field in the form resolves to a DIFFERENT discriminated
|
|
6
|
+
* variant between two wrapped data containers. A variant picker typically
|
|
7
|
+
* detects its variant index once at setup and keys the variant subtree on it,
|
|
8
|
+
* so a rebase that lands a different variant (via conflict OR an upstream-only
|
|
9
|
+
* switch) needs a remount to re-detect. This walks union + nested-object fields
|
|
10
|
+
* and compares `detectUnionVariant` at each union path.
|
|
11
|
+
*
|
|
12
|
+
* Scope note (pragmatic): walks standalone + nested-OBJECT union fields. Unions
|
|
13
|
+
* nested INSIDE array items are not walked — an array renderer that keeps a
|
|
14
|
+
* stable per-item key across in-place value mutations would not remount an
|
|
15
|
+
* existing row's picker on an upstream-driven variant flip, but that collision
|
|
16
|
+
* (a 3-way rebase landing a different union variant inside an unchanged array
|
|
17
|
+
* row) is a rare edge. TODO: extend to array-item unions if a real consumer
|
|
18
|
+
* hits a stuck picker inside an array row.
|
|
19
|
+
*/
|
|
20
|
+
function unionVariantChanged(def, before, after) {
|
|
21
|
+
return walkUnionFields(def.fields, "", before, after);
|
|
22
|
+
}
|
|
23
|
+
function walkUnionFields(fields, prefix, before, after) {
|
|
24
|
+
for (const field of fields) {
|
|
25
|
+
if (field.phantom) continue;
|
|
26
|
+
const fullPath = field.path ? prefix ? `${prefix}.${field.path}` : field.path : prefix;
|
|
27
|
+
if (isUnionField(field)) {
|
|
28
|
+
const variants = field.unionVariants;
|
|
29
|
+
if (variants.length > 1) {
|
|
30
|
+
if (detectUnionVariant(getByPath(before, fullPath), variants) !== detectUnionVariant(getByPath(after, fullPath), variants)) return true;
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (isObjectField(field) && (isObject(before, fullPath) || isObject(after, fullPath))) {
|
|
35
|
+
const objectDef = field.objectDef;
|
|
36
|
+
if (walkUnionFields(objectDef.fields, fullPath, before, after)) return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function isObject(data, path) {
|
|
42
|
+
const v = getByPath(data, path);
|
|
43
|
+
return typeof v === "object" && v !== null;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { unionVariantChanged };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
require("../shared/annotation-keys.cjs");
|
|
2
|
+
const require_types = require("./types.cjs");
|
|
3
|
+
const require_skipped_nav = require("./skipped-nav.cjs");
|
|
4
|
+
const require_error_utils = require("./error-utils.cjs");
|
|
5
|
+
let _atscript_typescript_utils = require("@atscript/typescript/utils");
|
|
6
|
+
//#region src/form/validate.ts
|
|
7
|
+
const dbPlugin = (0, require("@atscript/db/validator").createDbValidatorPlugin)();
|
|
8
|
+
const INSERT_CONTEXT = { mode: "insert" };
|
|
9
|
+
/**
|
|
10
|
+
* Always-on plugin for fields the server fills in, so a create form is not
|
|
11
|
+
* blocked on them:
|
|
12
|
+
* - `@db.column.derived` — read-only and server-computed: never validated
|
|
13
|
+
* (not even a loaded value, which the user could not fix).
|
|
14
|
+
* - a navigation field the form leaves out (`isSkippedNavField`): loaded
|
|
15
|
+
* related rows the form neither renders nor sends.
|
|
16
|
+
* - an absent `@db.default*` / `@db.column.version` / `@db.onUpdate.inc` value
|
|
17
|
+
* — delegated to `@atscript/db`'s own insert-mode rule, so the key list has
|
|
18
|
+
* one owner.
|
|
19
|
+
* `db.rel.FK` is kept out: a form fills the FK itself (no nested nav
|
|
20
|
+
* object), so an empty required FK must still fail.
|
|
21
|
+
*
|
|
22
|
+
* The db plugin reads its mode from the validation context, which here
|
|
23
|
+
* carries the form's `{ data, context }` — it gets a view of `ctx` with the
|
|
24
|
+
* insert context instead.
|
|
25
|
+
*/
|
|
26
|
+
const serverManagedPlugin = (ctx, def, value) => {
|
|
27
|
+
if (def.metadata.has("db.column.derived") || require_skipped_nav.isSkippedNavField(def)) return true;
|
|
28
|
+
if (value !== void 0 || def.metadata.has("db.rel.FK")) return void 0;
|
|
29
|
+
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
33
|
+
* computed view column (`metaFields`) or an edit form's update-stamped field
|
|
34
|
+
* (see `CreateFormDefOptions.mode`) carries no annotation the plugin above
|
|
35
|
+
* could read. The validator matches by prop, so a prop that is also used where
|
|
36
|
+
* it is not derived (an interface's stamp below a union of several types, too)
|
|
37
|
+
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
38
|
+
* visited once.
|
|
39
|
+
*/
|
|
40
|
+
function collectDerivedProps(def) {
|
|
41
|
+
const derived = /* @__PURE__ */ new Set();
|
|
42
|
+
const kept = /* @__PURE__ */ new Set();
|
|
43
|
+
const seen = /* @__PURE__ */ new Set();
|
|
44
|
+
const walk = (fields) => {
|
|
45
|
+
for (const field of fields) {
|
|
46
|
+
if (seen.has(field)) continue;
|
|
47
|
+
seen.add(field);
|
|
48
|
+
if (field.derived) {
|
|
49
|
+
derived.add(field.prop);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
kept.add(field.prop);
|
|
53
|
+
walk(require_types.childFields(field));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
walk(def.fields);
|
|
57
|
+
for (const prop of kept) derived.delete(prop);
|
|
58
|
+
return derived;
|
|
59
|
+
}
|
|
60
|
+
let defaultValidatorPlugins = [];
|
|
61
|
+
/** Replace the default validator plugins applied to every form/field validator. */
|
|
62
|
+
function setDefaultValidatorPlugins(plugins) {
|
|
63
|
+
defaultValidatorPlugins = plugins;
|
|
64
|
+
}
|
|
65
|
+
/** Get the currently registered default validator plugins. */
|
|
66
|
+
function getDefaultValidatorPlugins() {
|
|
67
|
+
return defaultValidatorPlugins;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a reusable validator function for a whole FormDef.
|
|
71
|
+
*
|
|
72
|
+
* Validator is created once and reused on every call.
|
|
73
|
+
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
74
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
75
|
+
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
76
|
+
*
|
|
77
|
+
* Errors are keyed by dotted path. A root-level error (a failing
|
|
78
|
+
* `@ui.form.validate` on the form's interface) is keyed `__form`.
|
|
79
|
+
*/
|
|
80
|
+
function getFormValidator(def, opts) {
|
|
81
|
+
const derived = collectDerivedProps(def);
|
|
82
|
+
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
83
|
+
const validator = new _atscript_typescript_utils.Validator(def.type, {
|
|
84
|
+
unknownProps: "ignore",
|
|
85
|
+
...opts,
|
|
86
|
+
plugins: [
|
|
87
|
+
...derived.size > 0 ? [derivedPlugin] : [],
|
|
88
|
+
serverManagedPlugin,
|
|
89
|
+
...defaultValidatorPlugins,
|
|
90
|
+
...opts?.plugins ?? []
|
|
91
|
+
]
|
|
92
|
+
});
|
|
93
|
+
return (callOpts) => {
|
|
94
|
+
if (validator.validate(callOpts.data, true, {
|
|
95
|
+
data: callOpts.data,
|
|
96
|
+
context: callOpts.context ?? {}
|
|
97
|
+
})) return {};
|
|
98
|
+
return require_error_utils.validatorErrorsToMap(validator.errors);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Root-only plugin: passes every descendant without visiting it. Only
|
|
103
|
+
* `object` / `array` / `tuple` roots use it — their root-path errors (shape,
|
|
104
|
+
* length, a container-level `@ui.form.validate`) are all decided before the
|
|
105
|
+
* walk descends, so skipping the children cannot change the root error. A
|
|
106
|
+
* union or intersection root evaluates its branches AT the root path, where
|
|
107
|
+
* a branch's verdict depends on its children — those keep the full walk.
|
|
108
|
+
*/
|
|
109
|
+
const skipDescendantsPlugin = (ctx) => ctx.path === "" ? void 0 : true;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a type kind is a container that carries its own children:
|
|
112
|
+
* `object`, `array` or `tuple`. Union / intersection kinds are not
|
|
113
|
+
* containers — they evaluate their branches at their own path.
|
|
114
|
+
*/
|
|
115
|
+
function isContainerKind(kind) {
|
|
116
|
+
return kind === "object" || kind === "array" || kind === "tuple";
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Creates a cached validator function for a single ATScript prop.
|
|
120
|
+
*
|
|
121
|
+
* The `Validator` instance is created lazily on first call and reused.
|
|
122
|
+
* Returns `true` when valid, or the first error message string when invalid.
|
|
123
|
+
* With `rootOnly`, an object / array / tuple prop is validated at its root
|
|
124
|
+
* only (descendants are not walked — their errors would be discarded anyway).
|
|
125
|
+
*/
|
|
126
|
+
function createFieldValidator(prop, opts) {
|
|
127
|
+
let cached;
|
|
128
|
+
return (value, externalCtx) => {
|
|
129
|
+
cached ??= new _atscript_typescript_utils.Validator(prop, { plugins: opts?.rootOnly && isContainerKind(prop.type.kind) ? [
|
|
130
|
+
skipDescendantsPlugin,
|
|
131
|
+
serverManagedPlugin,
|
|
132
|
+
...defaultValidatorPlugins
|
|
133
|
+
] : [serverManagedPlugin, ...defaultValidatorPlugins] });
|
|
134
|
+
if (!cached.validate(value, true, externalCtx)) {
|
|
135
|
+
if (opts?.rootOnly) {
|
|
136
|
+
const rootError = cached.errors?.find((e) => e.path === "");
|
|
137
|
+
if (rootError) return rootError.message;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return cached.errors?.[0]?.message || "Invalid value";
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
exports.createFieldValidator = createFieldValidator;
|
|
147
|
+
exports.getDefaultValidatorPlugins = getDefaultValidatorPlugins;
|
|
148
|
+
exports.getFormValidator = getFormValidator;
|
|
149
|
+
exports.isContainerKind = isContainerKind;
|
|
150
|
+
exports.setDefaultValidatorPlugins = setDefaultValidatorPlugins;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { FormDef } from "./types.cjs";
|
|
2
|
+
import { TAtscriptAnnotatedType, TValidatorOptions, TValidatorPlugin } from "@atscript/typescript/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/form/validate.d.ts
|
|
5
|
+
/** Per-call options for the form validator function. */
|
|
6
|
+
interface TFormValidatorCallOptions {
|
|
7
|
+
data: Record<string, unknown>;
|
|
8
|
+
context?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** Replace the default validator plugins applied to every form/field validator. */
|
|
11
|
+
declare function setDefaultValidatorPlugins(plugins: TValidatorPlugin[]): void;
|
|
12
|
+
/** Get the currently registered default validator plugins. */
|
|
13
|
+
declare function getDefaultValidatorPlugins(): TValidatorPlugin[];
|
|
14
|
+
/**
|
|
15
|
+
* Returns a reusable validator function for a whole FormDef.
|
|
16
|
+
*
|
|
17
|
+
* Validator is created once and reused on every call.
|
|
18
|
+
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
19
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
20
|
+
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
21
|
+
*
|
|
22
|
+
* Errors are keyed by dotted path. A root-level error (a failing
|
|
23
|
+
* `@ui.form.validate` on the form's interface) is keyed `__form`.
|
|
24
|
+
*/
|
|
25
|
+
declare function getFormValidator(def: FormDef, opts?: Partial<TValidatorOptions>): (callOpts: TFormValidatorCallOptions) => Record<string, string>;
|
|
26
|
+
/** Options for createFieldValidator. */
|
|
27
|
+
interface TFieldValidatorOptions {
|
|
28
|
+
/** Only report errors at the root path (for structure/array container validation). */
|
|
29
|
+
rootOnly?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether a type kind is a container that carries its own children:
|
|
33
|
+
* `object`, `array` or `tuple`. Union / intersection kinds are not
|
|
34
|
+
* containers — they evaluate their branches at their own path.
|
|
35
|
+
*/
|
|
36
|
+
declare function isContainerKind(kind: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a cached validator function for a single ATScript prop.
|
|
39
|
+
*
|
|
40
|
+
* The `Validator` instance is created lazily on first call and reused.
|
|
41
|
+
* Returns `true` when valid, or the first error message string when invalid.
|
|
42
|
+
* With `rootOnly`, an object / array / tuple prop is validated at its root
|
|
43
|
+
* only (descendants are not walked — their errors would be discarded anyway).
|
|
44
|
+
*/
|
|
45
|
+
declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFieldValidatorOptions): (value: unknown, externalCtx?: {
|
|
46
|
+
data: unknown;
|
|
47
|
+
context: unknown;
|
|
48
|
+
}) => true | string;
|
|
49
|
+
//#endregion
|
|
50
|
+
export { TFieldValidatorOptions, TFormValidatorCallOptions, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { FormDef } from "./types.mjs";
|
|
2
|
+
import { TAtscriptAnnotatedType, TValidatorOptions, TValidatorPlugin } from "@atscript/typescript/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/form/validate.d.ts
|
|
5
|
+
/** Per-call options for the form validator function. */
|
|
6
|
+
interface TFormValidatorCallOptions {
|
|
7
|
+
data: Record<string, unknown>;
|
|
8
|
+
context?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** Replace the default validator plugins applied to every form/field validator. */
|
|
11
|
+
declare function setDefaultValidatorPlugins(plugins: TValidatorPlugin[]): void;
|
|
12
|
+
/** Get the currently registered default validator plugins. */
|
|
13
|
+
declare function getDefaultValidatorPlugins(): TValidatorPlugin[];
|
|
14
|
+
/**
|
|
15
|
+
* Returns a reusable validator function for a whole FormDef.
|
|
16
|
+
*
|
|
17
|
+
* Validator is created once and reused on every call.
|
|
18
|
+
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
19
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
20
|
+
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
21
|
+
*
|
|
22
|
+
* Errors are keyed by dotted path. A root-level error (a failing
|
|
23
|
+
* `@ui.form.validate` on the form's interface) is keyed `__form`.
|
|
24
|
+
*/
|
|
25
|
+
declare function getFormValidator(def: FormDef, opts?: Partial<TValidatorOptions>): (callOpts: TFormValidatorCallOptions) => Record<string, string>;
|
|
26
|
+
/** Options for createFieldValidator. */
|
|
27
|
+
interface TFieldValidatorOptions {
|
|
28
|
+
/** Only report errors at the root path (for structure/array container validation). */
|
|
29
|
+
rootOnly?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether a type kind is a container that carries its own children:
|
|
33
|
+
* `object`, `array` or `tuple`. Union / intersection kinds are not
|
|
34
|
+
* containers — they evaluate their branches at their own path.
|
|
35
|
+
*/
|
|
36
|
+
declare function isContainerKind(kind: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a cached validator function for a single ATScript prop.
|
|
39
|
+
*
|
|
40
|
+
* The `Validator` instance is created lazily on first call and reused.
|
|
41
|
+
* Returns `true` when valid, or the first error message string when invalid.
|
|
42
|
+
* With `rootOnly`, an object / array / tuple prop is validated at its root
|
|
43
|
+
* only (descendants are not walked — their errors would be discarded anyway).
|
|
44
|
+
*/
|
|
45
|
+
declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFieldValidatorOptions): (value: unknown, externalCtx?: {
|
|
46
|
+
data: unknown;
|
|
47
|
+
context: unknown;
|
|
48
|
+
}) => true | string;
|
|
49
|
+
//#endregion
|
|
50
|
+
export { TFieldValidatorOptions, TFormValidatorCallOptions, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
|