@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,147 @@
|
|
|
1
|
+
import "../shared/annotation-keys.mjs";
|
|
2
|
+
import { childFields } from "./types.mjs";
|
|
3
|
+
import { isSkippedNavField } from "./skipped-nav.mjs";
|
|
4
|
+
import { validatorErrorsToMap } from "./error-utils.mjs";
|
|
5
|
+
import { Validator } from "@atscript/typescript/utils";
|
|
6
|
+
import { createDbValidatorPlugin } from "@atscript/db/validator";
|
|
7
|
+
//#region src/form/validate.ts
|
|
8
|
+
const dbPlugin = createDbValidatorPlugin();
|
|
9
|
+
const INSERT_CONTEXT = { mode: "insert" };
|
|
10
|
+
/**
|
|
11
|
+
* Always-on plugin for fields the server fills in, so a create form is not
|
|
12
|
+
* blocked on them:
|
|
13
|
+
* - `@db.column.derived` — read-only and server-computed: never validated
|
|
14
|
+
* (not even a loaded value, which the user could not fix).
|
|
15
|
+
* - a navigation field the form leaves out (`isSkippedNavField`): loaded
|
|
16
|
+
* related rows the form neither renders nor sends.
|
|
17
|
+
* - an absent `@db.default*` / `@db.column.version` / `@db.onUpdate.inc` value
|
|
18
|
+
* — delegated to `@atscript/db`'s own insert-mode rule, so the key list has
|
|
19
|
+
* one owner.
|
|
20
|
+
* `db.rel.FK` is kept out: a form fills the FK itself (no nested nav
|
|
21
|
+
* object), so an empty required FK must still fail.
|
|
22
|
+
*
|
|
23
|
+
* The db plugin reads its mode from the validation context, which here
|
|
24
|
+
* carries the form's `{ data, context }` — it gets a view of `ctx` with the
|
|
25
|
+
* insert context instead.
|
|
26
|
+
*/
|
|
27
|
+
const serverManagedPlugin = (ctx, def, value) => {
|
|
28
|
+
if (def.metadata.has("db.column.derived") || isSkippedNavField(def)) return true;
|
|
29
|
+
if (value !== void 0 || def.metadata.has("db.rel.FK")) return void 0;
|
|
30
|
+
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
34
|
+
* computed view column (`metaFields`) or an edit form's update-stamped field
|
|
35
|
+
* (see `CreateFormDefOptions.mode`) carries no annotation the plugin above
|
|
36
|
+
* could read. The validator matches by prop, so a prop that is also used where
|
|
37
|
+
* it is not derived (an interface's stamp below a union of several types, too)
|
|
38
|
+
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
39
|
+
* visited once.
|
|
40
|
+
*/
|
|
41
|
+
function collectDerivedProps(def) {
|
|
42
|
+
const derived = /* @__PURE__ */ new Set();
|
|
43
|
+
const kept = /* @__PURE__ */ new Set();
|
|
44
|
+
const seen = /* @__PURE__ */ new Set();
|
|
45
|
+
const walk = (fields) => {
|
|
46
|
+
for (const field of fields) {
|
|
47
|
+
if (seen.has(field)) continue;
|
|
48
|
+
seen.add(field);
|
|
49
|
+
if (field.derived) {
|
|
50
|
+
derived.add(field.prop);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
kept.add(field.prop);
|
|
54
|
+
walk(childFields(field));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
walk(def.fields);
|
|
58
|
+
for (const prop of kept) derived.delete(prop);
|
|
59
|
+
return derived;
|
|
60
|
+
}
|
|
61
|
+
let defaultValidatorPlugins = [];
|
|
62
|
+
/** Replace the default validator plugins applied to every form/field validator. */
|
|
63
|
+
function setDefaultValidatorPlugins(plugins) {
|
|
64
|
+
defaultValidatorPlugins = plugins;
|
|
65
|
+
}
|
|
66
|
+
/** Get the currently registered default validator plugins. */
|
|
67
|
+
function getDefaultValidatorPlugins() {
|
|
68
|
+
return defaultValidatorPlugins;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns a reusable validator function for a whole FormDef.
|
|
72
|
+
*
|
|
73
|
+
* Validator is created once and reused on every call.
|
|
74
|
+
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
75
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
76
|
+
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
77
|
+
*
|
|
78
|
+
* Errors are keyed by dotted path. A root-level error (a failing
|
|
79
|
+
* `@ui.form.validate` on the form's interface) is keyed `__form`.
|
|
80
|
+
*/
|
|
81
|
+
function getFormValidator(def, opts) {
|
|
82
|
+
const derived = collectDerivedProps(def);
|
|
83
|
+
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
84
|
+
const validator = new Validator(def.type, {
|
|
85
|
+
unknownProps: "ignore",
|
|
86
|
+
...opts,
|
|
87
|
+
plugins: [
|
|
88
|
+
...derived.size > 0 ? [derivedPlugin] : [],
|
|
89
|
+
serverManagedPlugin,
|
|
90
|
+
...defaultValidatorPlugins,
|
|
91
|
+
...opts?.plugins ?? []
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
return (callOpts) => {
|
|
95
|
+
if (validator.validate(callOpts.data, true, {
|
|
96
|
+
data: callOpts.data,
|
|
97
|
+
context: callOpts.context ?? {}
|
|
98
|
+
})) return {};
|
|
99
|
+
return validatorErrorsToMap(validator.errors);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Root-only plugin: passes every descendant without visiting it. Only
|
|
104
|
+
* `object` / `array` / `tuple` roots use it — their root-path errors (shape,
|
|
105
|
+
* length, a container-level `@ui.form.validate`) are all decided before the
|
|
106
|
+
* walk descends, so skipping the children cannot change the root error. A
|
|
107
|
+
* union or intersection root evaluates its branches AT the root path, where
|
|
108
|
+
* a branch's verdict depends on its children — those keep the full walk.
|
|
109
|
+
*/
|
|
110
|
+
const skipDescendantsPlugin = (ctx) => ctx.path === "" ? void 0 : true;
|
|
111
|
+
/**
|
|
112
|
+
* Whether a type kind is a container that carries its own children:
|
|
113
|
+
* `object`, `array` or `tuple`. Union / intersection kinds are not
|
|
114
|
+
* containers — they evaluate their branches at their own path.
|
|
115
|
+
*/
|
|
116
|
+
function isContainerKind(kind) {
|
|
117
|
+
return kind === "object" || kind === "array" || kind === "tuple";
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Creates a cached validator function for a single ATScript prop.
|
|
121
|
+
*
|
|
122
|
+
* The `Validator` instance is created lazily on first call and reused.
|
|
123
|
+
* Returns `true` when valid, or the first error message string when invalid.
|
|
124
|
+
* With `rootOnly`, an object / array / tuple prop is validated at its root
|
|
125
|
+
* only (descendants are not walked — their errors would be discarded anyway).
|
|
126
|
+
*/
|
|
127
|
+
function createFieldValidator(prop, opts) {
|
|
128
|
+
let cached;
|
|
129
|
+
return (value, externalCtx) => {
|
|
130
|
+
cached ??= new Validator(prop, { plugins: opts?.rootOnly && isContainerKind(prop.type.kind) ? [
|
|
131
|
+
skipDescendantsPlugin,
|
|
132
|
+
serverManagedPlugin,
|
|
133
|
+
...defaultValidatorPlugins
|
|
134
|
+
] : [serverManagedPlugin, ...defaultValidatorPlugins] });
|
|
135
|
+
if (!cached.validate(value, true, externalCtx)) {
|
|
136
|
+
if (opts?.rootOnly) {
|
|
137
|
+
const rootError = cached.errors?.find((e) => e.path === "");
|
|
138
|
+
if (rootError) return rootError.message;
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
return cached.errors?.[0]?.message || "Invalid value";
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
export { createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
|