@atscript/ui 0.1.207 → 0.1.208

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.
Files changed (86) hide show
  1. package/dist/form/create-form-def.cjs +13 -12
  2. package/dist/form/create-form-def.mjs +14 -13
  3. package/dist/form/decimal-format.cjs +8 -3
  4. package/dist/form/decimal-format.d.cts +8 -2
  5. package/dist/form/decimal-format.d.mts +8 -2
  6. package/dist/form/decimal-format.mjs +8 -3
  7. package/dist/form/form-actions.cjs +9 -0
  8. package/dist/form/form-actions.d.cts +7 -1
  9. package/dist/form/form-actions.d.mts +7 -1
  10. package/dist/form/form-actions.mjs +9 -1
  11. package/dist/form/measurement.cjs +58 -0
  12. package/dist/form/measurement.d.cts +36 -1
  13. package/dist/form/measurement.d.mts +36 -1
  14. package/dist/form/measurement.mjs +57 -2
  15. package/dist/form/omit-derived.cjs +11 -0
  16. package/dist/form/omit-derived.d.cts +8 -1
  17. package/dist/form/omit-derived.d.mts +8 -1
  18. package/dist/form/omit-derived.mjs +11 -1
  19. package/dist/form/optional-mode.cjs +7 -7
  20. package/dist/form/optional-mode.d.cts +2 -1
  21. package/dist/form/optional-mode.d.mts +2 -1
  22. package/dist/form/optional-mode.mjs +8 -8
  23. package/dist/form/types.cjs +5 -0
  24. package/dist/form/types.d.cts +14 -0
  25. package/dist/form/types.d.mts +14 -0
  26. package/dist/form/types.mjs +5 -1
  27. package/dist/form/validate.cjs +56 -5
  28. package/dist/form/validate.d.cts +30 -2
  29. package/dist/form/validate.d.mts +30 -2
  30. package/dist/form/validate.mjs +56 -6
  31. package/dist/index.cjs +24 -3
  32. package/dist/index.d.cts +14 -9
  33. package/dist/index.d.mts +14 -9
  34. package/dist/index.mjs +15 -10
  35. package/dist/plugin/annotations.cjs +1 -1
  36. package/dist/plugin/annotations.mjs +1 -1
  37. package/dist/shared/annotation-keys.d.cts +2 -1
  38. package/dist/shared/annotation-keys.d.mts +2 -1
  39. package/dist/shared/dev.cjs +22 -0
  40. package/dist/shared/dev.d.cts +4 -0
  41. package/dist/shared/dev.d.mts +4 -0
  42. package/dist/shared/dev.mjs +22 -0
  43. package/dist/shared/field-resolver.cjs +21 -1
  44. package/dist/shared/field-resolver.d.cts +15 -1
  45. package/dist/shared/field-resolver.d.mts +15 -1
  46. package/dist/shared/field-resolver.mjs +20 -2
  47. package/dist/shared/merge-filters.cjs +28 -0
  48. package/dist/shared/merge-filters.d.cts +13 -0
  49. package/dist/shared/merge-filters.d.mts +13 -0
  50. package/dist/shared/merge-filters.mjs +28 -0
  51. package/dist/shared/resolve-component.cjs +27 -0
  52. package/dist/shared/resolve-component.d.cts +22 -0
  53. package/dist/shared/resolve-component.d.mts +22 -0
  54. package/dist/shared/resolve-component.mjs +27 -0
  55. package/dist/shared/stable-key.cjs +28 -0
  56. package/dist/shared/stable-key.d.cts +13 -0
  57. package/dist/shared/stable-key.d.mts +13 -0
  58. package/dist/shared/stable-key.mjs +28 -0
  59. package/dist/shared/to-date.cjs +65 -0
  60. package/dist/shared/to-date.d.cts +35 -0
  61. package/dist/shared/to-date.d.mts +35 -0
  62. package/dist/shared/to-date.mjs +64 -0
  63. package/dist/table/create-table-def.cjs +49 -15
  64. package/dist/table/create-table-def.mjs +49 -16
  65. package/dist/table/types.d.cts +15 -13
  66. package/dist/table/types.d.mts +15 -13
  67. package/dist/value-help/distinct-client.cjs +80 -7
  68. package/dist/value-help/distinct-client.d.cts +31 -36
  69. package/dist/value-help/distinct-client.d.mts +31 -36
  70. package/dist/value-help/distinct-client.mjs +80 -7
  71. package/dist/value-help/index.d.mts +2 -2
  72. package/dist/value-help/options.cjs +21 -0
  73. package/dist/value-help/options.d.cts +17 -1
  74. package/dist/value-help/options.d.mts +17 -1
  75. package/dist/value-help/options.mjs +21 -1
  76. package/dist/value-help/resolve.cjs +2 -12
  77. package/dist/value-help/resolve.mjs +3 -13
  78. package/dist/value-help/scope.cjs +3 -7
  79. package/dist/value-help/scope.mjs +3 -7
  80. package/dist/value-help/texts.cjs +7 -4
  81. package/dist/value-help/texts.mjs +7 -4
  82. package/dist/value-help/value-help-client.cjs +5 -3
  83. package/dist/value-help/value-help-client.d.cts +3 -2
  84. package/dist/value-help/value-help-client.d.mts +3 -2
  85. package/dist/value-help/value-help-client.mjs +5 -3
  86. package/package.json +10 -10
@@ -24,7 +24,7 @@ function getDefaultOptionalMode() {
24
24
  * included), tuple or union field is always `"technical"`. Since 0.1.205.
25
25
  */
26
26
  function resolveOptionalMode(field, formMode) {
27
- if (require_types.isObjectField(field) || require_types.isArrayField(field) || require_types.isTupleField(field) || require_types.isUnionField(field)) return "technical";
27
+ if (require_types.holdsFields(field)) return "technical";
28
28
  const own = require_field_resolver.getFieldMeta(field.prop, require_annotation_keys.UI_FORM_OPTIONAL_MODE);
29
29
  if (own === "business" || own === "technical") return own;
30
30
  return formMode ?? defaultOptionalMode;
@@ -63,18 +63,18 @@ function takesEmptyText(prop) {
63
63
  * - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
64
64
  * keeps the stored secret.
65
65
  *
66
- * A required field: `""` for text and a string key, else `null`.
66
+ * A required field: `false` for a boolean (since 0.1.208; was `null`), `""`
67
+ * for text and a string key, else `null`.
67
68
  */
68
69
  function emptiedValue(field, opts) {
69
70
  const prop = field.prop;
70
71
  const dt = prop.type.kind === "" ? prop.type.designType : void 0;
71
72
  const key = !!field.valueHelpInfo || field.type === "ref" || require_extract_ref.isForeignKey(prop);
72
73
  const text = TEXT.has(field.type) && !key && !require_exact_number.isExactNumberText(prop);
73
- if (prop.optional) {
74
- if (opts?.mode === "technical" || !opts?.edit || prop.metadata.has("db.writeOnly")) return;
75
- if (dt === "boolean") return false;
76
- return text && takesEmptyText(prop) ? "" : null;
77
- }
74
+ const unset = opts?.mode === "technical" || !opts?.edit || prop.metadata.has("db.writeOnly");
75
+ if (prop.optional && unset) return void 0;
76
+ if (dt === "boolean") return false;
77
+ if (prop.optional) return text && takesEmptyText(prop) ? "" : null;
78
78
  if (key) return dt === "string" ? "" : null;
79
79
  return text ? "" : null;
80
80
  }
@@ -48,7 +48,8 @@ declare function resolveOptionalMode(field: FormFieldDef, formMode?: TOptionalMo
48
48
  * - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
49
49
  * keeps the stored secret.
50
50
  *
51
- * A required field: `""` for text and a string key, else `null`.
51
+ * A required field: `false` for a boolean (since 0.1.208; was `null`), `""`
52
+ * for text and a string key, else `null`.
52
53
  */
53
54
  declare function emptiedValue(field: FormFieldDef, opts?: {
54
55
  mode?: TOptionalMode;
@@ -48,7 +48,8 @@ declare function resolveOptionalMode(field: FormFieldDef, formMode?: TOptionalMo
48
48
  * - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
49
49
  * keeps the stored secret.
50
50
  *
51
- * A required field: `""` for text and a string key, else `null`.
51
+ * A required field: `false` for a boolean (since 0.1.208; was `null`), `""`
52
+ * for text and a string key, else `null`.
52
53
  */
53
54
  declare function emptiedValue(field: FormFieldDef, opts?: {
54
55
  mode?: TOptionalMode;
@@ -1,5 +1,5 @@
1
1
  import { UI_FORM_OPTIONAL_MODE } from "../shared/annotation-keys.mjs";
2
- import { isArrayField, isObjectField, isTupleField, isUnionField } from "./types.mjs";
2
+ import { holdsFields } from "./types.mjs";
3
3
  import { getFieldMeta } from "../shared/field-resolver.mjs";
4
4
  import { isForeignKey } from "../value-help/extract-ref.mjs";
5
5
  import { isExactNumberText } from "../shared/exact-number.mjs";
@@ -24,7 +24,7 @@ function getDefaultOptionalMode() {
24
24
  * included), tuple or union field is always `"technical"`. Since 0.1.205.
25
25
  */
26
26
  function resolveOptionalMode(field, formMode) {
27
- if (isObjectField(field) || isArrayField(field) || isTupleField(field) || isUnionField(field)) return "technical";
27
+ if (holdsFields(field)) return "technical";
28
28
  const own = getFieldMeta(field.prop, UI_FORM_OPTIONAL_MODE);
29
29
  if (own === "business" || own === "technical") return own;
30
30
  return formMode ?? defaultOptionalMode;
@@ -63,18 +63,18 @@ function takesEmptyText(prop) {
63
63
  * - a set-only field (`@db.writeOnly`): always `undefined` — an empty input
64
64
  * keeps the stored secret.
65
65
  *
66
- * A required field: `""` for text and a string key, else `null`.
66
+ * A required field: `false` for a boolean (since 0.1.208; was `null`), `""`
67
+ * for text and a string key, else `null`.
67
68
  */
68
69
  function emptiedValue(field, opts) {
69
70
  const prop = field.prop;
70
71
  const dt = prop.type.kind === "" ? prop.type.designType : void 0;
71
72
  const key = !!field.valueHelpInfo || field.type === "ref" || isForeignKey(prop);
72
73
  const text = TEXT.has(field.type) && !key && !isExactNumberText(prop);
73
- if (prop.optional) {
74
- if (opts?.mode === "technical" || !opts?.edit || prop.metadata.has("db.writeOnly")) return;
75
- if (dt === "boolean") return false;
76
- return text && takesEmptyText(prop) ? "" : null;
77
- }
74
+ const unset = opts?.mode === "technical" || !opts?.edit || prop.metadata.has("db.writeOnly");
75
+ if (prop.optional && unset) return void 0;
76
+ if (dt === "boolean") return false;
77
+ if (prop.optional) return text && takesEmptyText(prop) ? "" : null;
78
78
  if (key) return dt === "string" ? "" : null;
79
79
  return text ? "" : null;
80
80
  }
@@ -15,6 +15,10 @@ function isUnionField(field) {
15
15
  function isTupleField(field) {
16
16
  return field.type === "tuple";
17
17
  }
18
+ /** An object, array, tuple or union field: one holding other fields, which no input empties. */
19
+ function holdsFields(field) {
20
+ return isObjectField(field) || isArrayField(field) || isTupleField(field) || isUnionField(field);
21
+ }
18
22
  /**
19
23
  * The field defs directly below `field`: an object's fields, an array's item,
20
24
  * a tuple's items, each union variant's fields and item. A recursive type's
@@ -34,6 +38,7 @@ function childFields(field) {
34
38
  }
35
39
  //#endregion
36
40
  exports.childFields = childFields;
41
+ exports.holdsFields = holdsFields;
37
42
  exports.isArrayField = isArrayField;
38
43
  exports.isObjectField = isObjectField;
39
44
  exports.isTupleField = isTupleField;
@@ -38,6 +38,12 @@ interface FormFieldDef {
38
38
  * consumer to attach a flat custom widget to e.g. a `string[]`.
39
39
  */
40
40
  customType?: string;
41
+ /**
42
+ * `@ui.form.component` — the name of the component that renders the field
43
+ * (`components[name]`, first in `resolveComponent`'s order), read once
44
+ * while the def is built. Since 0.1.208.
45
+ */
46
+ component?: string;
41
47
  phantom: boolean;
42
48
  name: string;
43
49
  /** True when no `ui.fn.*` metadata keys exist. Vue perf flag. */
@@ -151,6 +157,14 @@ interface FormArrayFieldDef extends FormFieldDef {
151
157
  itemType: TAtscriptAnnotatedType;
152
158
  /** Pre-built template field def for items (path=''). */
153
159
  itemField: FormFieldDef;
160
+ /**
161
+ * `@expect.array.uniqueItems` — its own, or its union's on a `T[] | null`
162
+ * variant: a control that takes free entries (`tags`) refuses a duplicate.
163
+ * Since 0.1.208.
164
+ */
165
+ uniqueItems: boolean;
166
+ /** The items are numbers: a free-entry control converts the typed text. Since 0.1.208. */
167
+ numericItems: boolean;
154
168
  }
155
169
  /** Extended field def for object (interface/type) nested fields. */
156
170
  interface FormObjectFieldDef extends FormFieldDef {
@@ -38,6 +38,12 @@ interface FormFieldDef {
38
38
  * consumer to attach a flat custom widget to e.g. a `string[]`.
39
39
  */
40
40
  customType?: string;
41
+ /**
42
+ * `@ui.form.component` — the name of the component that renders the field
43
+ * (`components[name]`, first in `resolveComponent`'s order), read once
44
+ * while the def is built. Since 0.1.208.
45
+ */
46
+ component?: string;
41
47
  phantom: boolean;
42
48
  name: string;
43
49
  /** True when no `ui.fn.*` metadata keys exist. Vue perf flag. */
@@ -151,6 +157,14 @@ interface FormArrayFieldDef extends FormFieldDef {
151
157
  itemType: TAtscriptAnnotatedType;
152
158
  /** Pre-built template field def for items (path=''). */
153
159
  itemField: FormFieldDef;
160
+ /**
161
+ * `@expect.array.uniqueItems` — its own, or its union's on a `T[] | null`
162
+ * variant: a control that takes free entries (`tags`) refuses a duplicate.
163
+ * Since 0.1.208.
164
+ */
165
+ uniqueItems: boolean;
166
+ /** The items are numbers: a free-entry control converts the typed text. Since 0.1.208. */
167
+ numericItems: boolean;
154
168
  }
155
169
  /** Extended field def for object (interface/type) nested fields. */
156
170
  interface FormObjectFieldDef extends FormFieldDef {
@@ -15,6 +15,10 @@ function isUnionField(field) {
15
15
  function isTupleField(field) {
16
16
  return field.type === "tuple";
17
17
  }
18
+ /** An object, array, tuple or union field: one holding other fields, which no input empties. */
19
+ function holdsFields(field) {
20
+ return isObjectField(field) || isArrayField(field) || isTupleField(field) || isUnionField(field);
21
+ }
18
22
  /**
19
23
  * The field defs directly below `field`: an object's fields, an array's item,
20
24
  * a tuple's items, each union variant's fields and item. A recursive type's
@@ -33,4 +37,4 @@ function childFields(field) {
33
37
  return children;
34
38
  }
35
39
  //#endregion
36
- export { childFields, isArrayField, isObjectField, isTupleField, isUnionField };
40
+ export { childFields, holdsFields, isArrayField, isObjectField, isTupleField, isUnionField };
@@ -4,6 +4,7 @@ const require_ui_texts = require("../i18n/ui-texts.cjs");
4
4
  const require_extract_ref = require("../value-help/extract-ref.cjs");
5
5
  const require_skipped_nav = require("./skipped-nav.cjs");
6
6
  const require_create_form_def = require("./create-form-def.cjs");
7
+ const require_optional_mode = require("./optional-mode.cjs");
7
8
  const require_error_utils = require("./error-utils.cjs");
8
9
  let _atscript_typescript_utils = require("@atscript/typescript/utils");
9
10
  let _atscript_db_validator = require("@atscript/db/validator");
@@ -245,6 +246,17 @@ function isContainerKind(kind) {
245
246
  return kind === "object" || kind === "array" || kind === "tuple";
246
247
  }
247
248
  /**
249
+ * The plugins a field's own value is judged with: the server-managed rules,
250
+ * `rules`, then a value-help picker left unpicked (`picker`).
251
+ */
252
+ function fieldPlugins(prop, picker, rules) {
253
+ return [
254
+ serverManagedPlugin,
255
+ ...rules,
256
+ ...picker ? [missingPickPlugin(new Set([prop]))] : []
257
+ ];
258
+ }
259
+ /**
248
260
  * Creates a cached validator function for a single ATScript prop.
249
261
  *
250
262
  * The `Validator` instance is created lazily on first call and reused.
@@ -255,6 +267,8 @@ function isContainerKind(kind) {
255
267
  * judges it (the value itself is not changed).
256
268
  * With `rootOnly`, an object / array / tuple prop is validated at its root
257
269
  * only (descendants are not walked — their errors would be discarded anyway).
270
+ * `externalCtx.path` is where the value sits in the form data — the path a
271
+ * `@ui.form.validate` rule's `entry.field` starts from (since 0.1.208).
258
272
  */
259
273
  function createFieldValidator(prop, opts) {
260
274
  let cached;
@@ -267,11 +281,7 @@ function createFieldValidator(prop, opts) {
267
281
  skipDescendantsPlugin,
268
282
  serverManagedPlugin,
269
283
  ...defaultValidatorPlugins
270
- ] : [
271
- serverManagedPlugin,
272
- ...defaultValidatorPlugins,
273
- ...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
274
- ] };
284
+ ] : fieldPlugins(prop, opts?.picker, defaultValidatorPlugins) };
275
285
  cached = new _atscript_typescript_utils.Validator(prop, options);
276
286
  }
277
287
  const judged = skipNormalize ? value : (0, _atscript_typescript_utils.normalizeForType)(prop, value, options);
@@ -286,10 +296,51 @@ function createFieldValidator(prop, opts) {
286
296
  return error && messageIn(error, t, served) || t("UI_INVALID_VALUE");
287
297
  };
288
298
  }
299
+ /** Per prop: whether its type refuses an emptied value, keyed by value (and the picker rule). */
300
+ const refusedCache = /* @__PURE__ */ new WeakMap();
301
+ /**
302
+ * Whether `field` shows as required — the `*` of its label, `aria-required`
303
+ * (since 0.1.208): the value the field is left with once the user empties it
304
+ * is one the type's own rules refuse — its kind, `@expect.*`,
305
+ * `@meta.required`, a value-help picker left unpicked; not a
306
+ * `@ui.form.validate` rule, which may read other fields. That value is
307
+ * {@link emptiedValue} (per `opts.mode`, the field's `resolveOptionalMode`,
308
+ * and `opts.edit`, the form's `FormDef.edit`); `[]` for a required array;
309
+ * for a required union, the emptied value of the variant it shows
310
+ * (`opts.variant`), judged by the union's rules. Never for a phantom,
311
+ * server-owned (`derived`) or read-only field, an object or a tuple, an
312
+ * optional array or union, nor a union without `opts.variant`.
313
+ * `opts.readonly` is the field's read-only state when the caller resolved it
314
+ * (`@ui.form.fn.readonly`, an enclosing field's lock); `@meta.readonly` when
315
+ * left out.
316
+ */
317
+ function isFieldRequired(field, opts) {
318
+ if ((opts?.readonly ?? field.prop.metadata.has("meta.readonly")) || field.phantom || field.derived) return false;
319
+ const value = leftEmpty(field, opts);
320
+ if (value === void 0) return false;
321
+ const prop = field.prop;
322
+ let byValue = refusedCache.get(prop);
323
+ if (!byValue) refusedCache.set(prop, byValue = /* @__PURE__ */ new Map());
324
+ const key = `${field.picker ? "picker:" : ""}${JSON.stringify(value)}`;
325
+ let refused = byValue.get(key);
326
+ if (refused === void 0) {
327
+ refused = !new _atscript_typescript_utils.Validator(prop, { plugins: fieldPlugins(prop, field.picker, []) }).validate(value, true);
328
+ byValue.set(key, refused);
329
+ }
330
+ return refused;
331
+ }
332
+ /** The value {@link isFieldRequired} judges: `undefined` for one no input leaves. */
333
+ function leftEmpty(field, opts) {
334
+ if (require_types.isObjectField(field) || require_types.isTupleField(field)) return void 0;
335
+ if (require_types.isUnionField(field)) return opts?.variant && !field.prop.optional ? leftEmpty(opts.variant) : void 0;
336
+ if (require_types.isArrayField(field)) return field.prop.optional ? void 0 : [];
337
+ return require_optional_mode.emptiedValue(field, opts);
338
+ }
289
339
  //#endregion
290
340
  exports.UI_FORM_VALIDATE_ERROR_CODE = UI_FORM_VALIDATE_ERROR_CODE;
291
341
  exports.createFieldValidator = createFieldValidator;
292
342
  exports.getDefaultValidatorPlugins = getDefaultValidatorPlugins;
293
343
  exports.getFormValidator = getFormValidator;
294
344
  exports.isContainerKind = isContainerKind;
345
+ exports.isFieldRequired = isFieldRequired;
295
346
  exports.setDefaultValidatorPlugins = setDefaultValidatorPlugins;
@@ -1,4 +1,5 @@
1
- import { FormDef } from "./types.cjs";
1
+ import { FormDef, FormFieldDef } from "./types.cjs";
2
+ import { TOptionalMode } from "./optional-mode.cjs";
2
3
  import { UiT } from "../i18n/ui-texts.cjs";
3
4
  import { TAtscriptAnnotatedType, TValidatorOptions, TValidatorPlugin } from "@atscript/typescript/utils";
4
5
 
@@ -87,11 +88,38 @@ declare function isContainerKind(kind: string): boolean;
87
88
  * judges it (the value itself is not changed).
88
89
  * With `rootOnly`, an object / array / tuple prop is validated at its root
89
90
  * only (descendants are not walked — their errors would be discarded anyway).
91
+ * `externalCtx.path` is where the value sits in the form data — the path a
92
+ * `@ui.form.validate` rule's `entry.field` starts from (since 0.1.208).
90
93
  */
91
94
  declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFieldValidatorOptions): (value: unknown, externalCtx?: {
92
95
  data: unknown;
93
96
  context: unknown;
94
97
  t?: UiT;
98
+ path?: string;
95
99
  }) => true | string;
100
+ /** Options of {@link isFieldRequired}. */
101
+ interface TFieldRequiredOptions {
102
+ mode?: TOptionalMode;
103
+ edit?: boolean;
104
+ readonly?: boolean;
105
+ variant?: FormFieldDef;
106
+ }
107
+ /**
108
+ * Whether `field` shows as required — the `*` of its label, `aria-required`
109
+ * (since 0.1.208): the value the field is left with once the user empties it
110
+ * is one the type's own rules refuse — its kind, `@expect.*`,
111
+ * `@meta.required`, a value-help picker left unpicked; not a
112
+ * `@ui.form.validate` rule, which may read other fields. That value is
113
+ * {@link emptiedValue} (per `opts.mode`, the field's `resolveOptionalMode`,
114
+ * and `opts.edit`, the form's `FormDef.edit`); `[]` for a required array;
115
+ * for a required union, the emptied value of the variant it shows
116
+ * (`opts.variant`), judged by the union's rules. Never for a phantom,
117
+ * server-owned (`derived`) or read-only field, an object or a tuple, an
118
+ * optional array or union, nor a union without `opts.variant`.
119
+ * `opts.readonly` is the field's read-only state when the caller resolved it
120
+ * (`@ui.form.fn.readonly`, an enclosing field's lock); `@meta.readonly` when
121
+ * left out.
122
+ */
123
+ declare function isFieldRequired(field: FormFieldDef, opts?: TFieldRequiredOptions): boolean;
96
124
  //#endregion
97
- export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
125
+ export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, isFieldRequired, setDefaultValidatorPlugins };
@@ -1,4 +1,5 @@
1
- import { FormDef } from "./types.mjs";
1
+ import { FormDef, FormFieldDef } from "./types.mjs";
2
+ import { TOptionalMode } from "./optional-mode.mjs";
2
3
  import { UiT } from "../i18n/ui-texts.mjs";
3
4
  import { TAtscriptAnnotatedType, TValidatorOptions, TValidatorPlugin } from "@atscript/typescript/utils";
4
5
 
@@ -87,11 +88,38 @@ declare function isContainerKind(kind: string): boolean;
87
88
  * judges it (the value itself is not changed).
88
89
  * With `rootOnly`, an object / array / tuple prop is validated at its root
89
90
  * only (descendants are not walked — their errors would be discarded anyway).
91
+ * `externalCtx.path` is where the value sits in the form data — the path a
92
+ * `@ui.form.validate` rule's `entry.field` starts from (since 0.1.208).
90
93
  */
91
94
  declare function createFieldValidator(prop: TAtscriptAnnotatedType, opts?: TFieldValidatorOptions): (value: unknown, externalCtx?: {
92
95
  data: unknown;
93
96
  context: unknown;
94
97
  t?: UiT;
98
+ path?: string;
95
99
  }) => true | string;
100
+ /** Options of {@link isFieldRequired}. */
101
+ interface TFieldRequiredOptions {
102
+ mode?: TOptionalMode;
103
+ edit?: boolean;
104
+ readonly?: boolean;
105
+ variant?: FormFieldDef;
106
+ }
107
+ /**
108
+ * Whether `field` shows as required — the `*` of its label, `aria-required`
109
+ * (since 0.1.208): the value the field is left with once the user empties it
110
+ * is one the type's own rules refuse — its kind, `@expect.*`,
111
+ * `@meta.required`, a value-help picker left unpicked; not a
112
+ * `@ui.form.validate` rule, which may read other fields. That value is
113
+ * {@link emptiedValue} (per `opts.mode`, the field's `resolveOptionalMode`,
114
+ * and `opts.edit`, the form's `FormDef.edit`); `[]` for a required array;
115
+ * for a required union, the emptied value of the variant it shows
116
+ * (`opts.variant`), judged by the union's rules. Never for a phantom,
117
+ * server-owned (`derived`) or read-only field, an object or a tuple, an
118
+ * optional array or union, nor a union without `opts.variant`.
119
+ * `opts.readonly` is the field's read-only state when the caller resolved it
120
+ * (`@ui.form.fn.readonly`, an enclosing field's lock); `@meta.readonly` when
121
+ * left out.
122
+ */
123
+ declare function isFieldRequired(field: FormFieldDef, opts?: TFieldRequiredOptions): boolean;
96
124
  //#endregion
97
- export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
125
+ export { FormValidator, TFieldValidatorOptions, TFormValidatorCallOptions, UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, isFieldRequired, setDefaultValidatorPlugins };
@@ -4,6 +4,7 @@ import { getUiTexts } from "../i18n/ui-texts.mjs";
4
4
  import { isForeignKey, isValueHelpPicker } from "../value-help/extract-ref.mjs";
5
5
  import { isSkippedNavField } from "./skipped-nav.mjs";
6
6
  import { isServerComputed, resolveOriginalProp } from "./create-form-def.mjs";
7
+ import { emptiedValue } from "./optional-mode.mjs";
7
8
  import { validatorErrorsToMap } from "./error-utils.mjs";
8
9
  import { VALIDATOR_MESSAGES, Validator, localizeErrors, normalizeForType } from "@atscript/typescript/utils";
9
10
  import { createDbValidatorPlugin } from "@atscript/db/validator";
@@ -245,6 +246,17 @@ function isContainerKind(kind) {
245
246
  return kind === "object" || kind === "array" || kind === "tuple";
246
247
  }
247
248
  /**
249
+ * The plugins a field's own value is judged with: the server-managed rules,
250
+ * `rules`, then a value-help picker left unpicked (`picker`).
251
+ */
252
+ function fieldPlugins(prop, picker, rules) {
253
+ return [
254
+ serverManagedPlugin,
255
+ ...rules,
256
+ ...picker ? [missingPickPlugin(new Set([prop]))] : []
257
+ ];
258
+ }
259
+ /**
248
260
  * Creates a cached validator function for a single ATScript prop.
249
261
  *
250
262
  * The `Validator` instance is created lazily on first call and reused.
@@ -255,6 +267,8 @@ function isContainerKind(kind) {
255
267
  * judges it (the value itself is not changed).
256
268
  * With `rootOnly`, an object / array / tuple prop is validated at its root
257
269
  * only (descendants are not walked — their errors would be discarded anyway).
270
+ * `externalCtx.path` is where the value sits in the form data — the path a
271
+ * `@ui.form.validate` rule's `entry.field` starts from (since 0.1.208).
258
272
  */
259
273
  function createFieldValidator(prop, opts) {
260
274
  let cached;
@@ -267,11 +281,7 @@ function createFieldValidator(prop, opts) {
267
281
  skipDescendantsPlugin,
268
282
  serverManagedPlugin,
269
283
  ...defaultValidatorPlugins
270
- ] : [
271
- serverManagedPlugin,
272
- ...defaultValidatorPlugins,
273
- ...opts?.picker ? [missingPickPlugin(new Set([prop]))] : []
274
- ] };
284
+ ] : fieldPlugins(prop, opts?.picker, defaultValidatorPlugins) };
275
285
  cached = new Validator(prop, options);
276
286
  }
277
287
  const judged = skipNormalize ? value : normalizeForType(prop, value, options);
@@ -286,5 +296,45 @@ function createFieldValidator(prop, opts) {
286
296
  return error && messageIn(error, t, served) || t("UI_INVALID_VALUE");
287
297
  };
288
298
  }
299
+ /** Per prop: whether its type refuses an emptied value, keyed by value (and the picker rule). */
300
+ const refusedCache = /* @__PURE__ */ new WeakMap();
301
+ /**
302
+ * Whether `field` shows as required — the `*` of its label, `aria-required`
303
+ * (since 0.1.208): the value the field is left with once the user empties it
304
+ * is one the type's own rules refuse — its kind, `@expect.*`,
305
+ * `@meta.required`, a value-help picker left unpicked; not a
306
+ * `@ui.form.validate` rule, which may read other fields. That value is
307
+ * {@link emptiedValue} (per `opts.mode`, the field's `resolveOptionalMode`,
308
+ * and `opts.edit`, the form's `FormDef.edit`); `[]` for a required array;
309
+ * for a required union, the emptied value of the variant it shows
310
+ * (`opts.variant`), judged by the union's rules. Never for a phantom,
311
+ * server-owned (`derived`) or read-only field, an object or a tuple, an
312
+ * optional array or union, nor a union without `opts.variant`.
313
+ * `opts.readonly` is the field's read-only state when the caller resolved it
314
+ * (`@ui.form.fn.readonly`, an enclosing field's lock); `@meta.readonly` when
315
+ * left out.
316
+ */
317
+ function isFieldRequired(field, opts) {
318
+ if ((opts?.readonly ?? field.prop.metadata.has("meta.readonly")) || field.phantom || field.derived) return false;
319
+ const value = leftEmpty(field, opts);
320
+ if (value === void 0) return false;
321
+ const prop = field.prop;
322
+ let byValue = refusedCache.get(prop);
323
+ if (!byValue) refusedCache.set(prop, byValue = /* @__PURE__ */ new Map());
324
+ const key = `${field.picker ? "picker:" : ""}${JSON.stringify(value)}`;
325
+ let refused = byValue.get(key);
326
+ if (refused === void 0) {
327
+ refused = !new Validator(prop, { plugins: fieldPlugins(prop, field.picker, []) }).validate(value, true);
328
+ byValue.set(key, refused);
329
+ }
330
+ return refused;
331
+ }
332
+ /** The value {@link isFieldRequired} judges: `undefined` for one no input leaves. */
333
+ function leftEmpty(field, opts) {
334
+ if (isObjectField(field) || isTupleField(field)) return void 0;
335
+ if (isUnionField(field)) return opts?.variant && !field.prop.optional ? leftEmpty(opts.variant) : void 0;
336
+ if (isArrayField(field)) return field.prop.optional ? void 0 : [];
337
+ return emptiedValue(field, opts);
338
+ }
289
339
  //#endregion
290
- export { UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
340
+ export { UI_FORM_VALIDATE_ERROR_CODE, createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, isFieldRequired, setDefaultValidatorPlugins };
package/dist/index.cjs CHANGED
@@ -9,14 +9,16 @@ const require_extract_ref = require("./value-help/extract-ref.cjs");
9
9
  const require_clone = require("./form/clone.cjs");
10
10
  const require_path_utils = require("./form/path-utils.cjs");
11
11
  const require_skipped_nav = require("./form/skipped-nav.cjs");
12
+ const require_decimal_format = require("./form/decimal-format.cjs");
13
+ const require_measurement = require("./form/measurement.cjs");
12
14
  const require_create_form_def = require("./form/create-form-def.cjs");
13
15
  const require_optional_mode = require("./form/optional-mode.cjs");
14
16
  const require_loaded_empty = require("./form/loaded-empty.cjs");
15
17
  const require_form_actions = require("./form/form-actions.cjs");
16
18
  const require_dict_paths = require("./value-help/dict-paths.cjs");
17
19
  const require_resolve_options = require("./value-help/resolve-options.cjs");
20
+ const require_merge_filters = require("./shared/merge-filters.cjs");
18
21
  const require_str = require("./shared/str.cjs");
19
- const require_measurement = require("./form/measurement.cjs");
20
22
  const require_value_label = require("./shared/value-label.cjs");
21
23
  const require_create_table_def = require("./table/create-table-def.cjs");
22
24
  const require_value_kind = require("./table/value-kind.cjs");
@@ -28,6 +30,7 @@ const require_source = require("./value-help/source.cjs");
28
30
  const require_texts = require("./value-help/texts.cjs");
29
31
  const require_value_help_client = require("./value-help/value-help-client.cjs");
30
32
  const require_scope = require("./value-help/scope.cjs");
33
+ const require_stable_key = require("./shared/stable-key.cjs");
31
34
  const require_distinct_client = require("./value-help/distinct-client.cjs");
32
35
  const require_display_value = require("./value-help/display-value.cjs");
33
36
  const require_error_utils = require("./form/error-utils.cjs");
@@ -40,7 +43,9 @@ const require_rebase = require("./form/rebase.cjs");
40
43
  const require_union_detect = require("./form/union-detect.cjs");
41
44
  const require_grid = require("./form/grid.cjs");
42
45
  const require_labels = require("./form/labels.cjs");
43
- const require_decimal_format = require("./form/decimal-format.cjs");
46
+ const require_dev = require("./shared/dev.cjs");
47
+ const require_resolve_component = require("./shared/resolve-component.cjs");
48
+ const require_to_date = require("./shared/to-date.cjs");
44
49
  const require_column_resolver = require("./table/column-resolver.cjs");
45
50
  const require_navigate_href = require("./table/navigate-href.cjs");
46
51
  const require_model_routes = require("./nav/model-routes.cjs");
@@ -55,6 +60,8 @@ exports.DB_UNIT_REF = require_annotation_keys.DB_UNIT_REF;
55
60
  exports.DEFAULT_BOOLEAN_LABELS = require_extract_literals.DEFAULT_BOOLEAN_LABELS;
56
61
  exports.DEFAULT_COL_SPAN = require_grid.DEFAULT_COL_SPAN;
57
62
  exports.DEFAULT_ROW_SPAN = require_grid.DEFAULT_ROW_SPAN;
63
+ exports.DEV = require_dev.DEV;
64
+ exports.EXPECT_ARRAY_UNIQUE_ITEMS = require_annotation_keys.EXPECT_ARRAY_UNIQUE_ITEMS;
58
65
  exports.EXPECT_MAX_LENGTH = require_annotation_keys.EXPECT_MAX_LENGTH;
59
66
  exports.FORM_ERROR_KEY = require_error_utils.FORM_ERROR_KEY;
60
67
  exports.META_DEFAULT = require_annotation_keys.META_DEFAULT;
@@ -155,6 +162,7 @@ Object.defineProperty(exports, "compareExact", {
155
162
  return _atscript_db_meta.compareExact;
156
163
  }
157
164
  });
165
+ exports.createDistinctPager = require_distinct_client.createDistinctPager;
158
166
  exports.createFieldValidator = require_validate.createFieldValidator;
159
167
  exports.createFormData = require_path_utils.createFormData;
160
168
  exports.createFormDef = require_create_form_def.createFormDef;
@@ -176,14 +184,15 @@ exports.extractFieldLiteralOptions = require_extract_literals.extractFieldLitera
176
184
  exports.extractLiteralOptions = require_extract_literals.extractLiteralOptions;
177
185
  exports.extractMeasurement = require_measurement.extractMeasurement;
178
186
  exports.extractValueHelp = require_extract_ref.extractValueHelp;
179
- exports.fetchDistinctValues = require_distinct_client.fetchDistinctValues;
180
187
  exports.fieldText = require_model_texts.fieldText;
188
+ exports.formPayload = require_omit_derived.formPayload;
181
189
  exports.formatDecimalForDisplay = require_decimal_format.formatDecimalForDisplay;
182
190
  exports.getByPath = require_path_utils.getByPath;
183
191
  exports.getColumn = require_column_resolver.getColumn;
184
192
  exports.getCurrencyDecimals = require_decimal_format.getCurrencyDecimals;
185
193
  exports.getCurrencyDisplayParts = require_decimal_format.getCurrencyDisplayParts;
186
194
  exports.getDecimalSeparator = require_decimal_format.getDecimalSeparator;
195
+ exports.getDeclaredFormAction = require_form_actions.getDeclaredFormAction;
187
196
  exports.getDeclaredFormActions = require_form_actions.getDeclaredFormActions;
188
197
  exports.getDefaultClientFactory = require_client_factory.getDefaultClientFactory;
189
198
  exports.getDefaultOptionalMode = require_optional_mode.getDefaultOptionalMode;
@@ -204,7 +213,10 @@ exports.hasFieldMeta = require_field_resolver.hasFieldMeta;
204
213
  exports.invalidateValueHelpCache = require_value_help_client.invalidateValueHelpCache;
205
214
  exports.isArrayField = require_types.isArrayField;
206
215
  exports.isContainerKind = require_validate.isContainerKind;
216
+ exports.isFieldDisabled = require_field_resolver.isFieldDisabled;
207
217
  exports.isFieldHidden = require_field_resolver.isFieldHidden;
218
+ exports.isFieldReadonly = require_field_resolver.isFieldReadonly;
219
+ exports.isFieldRequired = require_validate.isFieldRequired;
208
220
  exports.isNumericText = require_value_kind.isNumericText;
209
221
  exports.isObjectField = require_types.isObjectField;
210
222
  exports.isPathDirty = require_dirty.isPathDirty;
@@ -220,8 +232,10 @@ exports.listsDictionary = require_options.listsDictionary;
220
232
  exports.loadValueHelpTexts = require_texts.loadValueHelpTexts;
221
233
  exports.markServedType = require_model_texts.markServedType;
222
234
  exports.mergeErrorMaps = require_error_utils.mergeErrorMaps;
235
+ exports.mergeFilters = require_merge_filters.mergeFilters;
223
236
  exports.navigateHrefFor = require_navigate_href.navigateHrefFor;
224
237
  exports.normalizeLoadedEmpty = require_loaded_empty.normalizeLoadedEmpty;
238
+ exports.numberAdornment = require_measurement.numberAdornment;
225
239
  exports.omitDerivedFields = require_omit_derived.omitDerivedFields;
226
240
  exports.omitPaths = require_error_utils.omitPaths;
227
241
  exports.omitSkippedNavFields = require_skipped_nav.omitSkippedNavFields;
@@ -238,13 +252,16 @@ exports.parseDecimalInput = require_decimal_format.parseDecimalInput;
238
252
  exports.parseRowSpan = require_grid.parseRowSpan;
239
253
  exports.parseStaticAttrs = require_field_resolver.parseStaticAttrs;
240
254
  exports.parseStaticOptions = require_resolve_options.parseStaticOptions;
255
+ exports.refText = require_measurement.refText;
241
256
  exports.resetDefaultClientFactory = require_client_factory.resetDefaultClientFactory;
242
257
  exports.resetMetaCache = require_meta_cache.resetMetaCache;
243
258
  exports.resetValueHelpCache = require_resolve.resetValueHelpCache;
244
259
  exports.resolveAttrs = require_field_resolver.resolveAttrs;
260
+ exports.resolveComponent = require_resolve_component.resolveComponent;
245
261
  exports.resolveFieldProp = require_field_resolver.resolveFieldProp;
246
262
  exports.resolveFormProp = require_field_resolver.resolveFormProp;
247
263
  exports.resolveGridSpec = require_grid.resolveGridSpec;
264
+ exports.resolveMeasurement = require_measurement.resolveMeasurement;
248
265
  exports.resolveOptionalMode = require_optional_mode.resolveOptionalMode;
249
266
  exports.resolveOptions = require_resolve_options.resolveOptions;
250
267
  exports.resolveSingularLabel = require_labels.resolveSingularLabel;
@@ -261,9 +278,12 @@ exports.setResolver = require_field_resolver.setResolver;
261
278
  exports.setUiLocale = require_ui_texts.setUiLocale;
262
279
  exports.setValueHelpCacheTtl = require_value_help_client.setValueHelpCacheTtl;
263
280
  exports.splitDecimalString = require_decimal_format.splitDecimalString;
281
+ exports.stableValueKey = require_stable_key.stableValueKey;
264
282
  exports.str = require_str.str;
283
+ exports.toDate = require_to_date.toDate;
265
284
  exports.unionVariantChanged = require_union_detect.unionVariantChanged;
266
285
  exports.userText = require_model_texts.userText;
286
+ exports.utcDay = require_to_date.utcDay;
267
287
  exports.validatorErrorsToMap = require_error_utils.validatorErrorsToMap;
268
288
  exports.valueHelpColumns = require_source.valueHelpColumns;
269
289
  exports.valueHelpDictPaths = require_dict_paths.valueHelpDictPaths;
@@ -277,3 +297,4 @@ exports.valueHelpSelect = require_value_help_client.valueHelpSelect;
277
297
  exports.valueHelpSource = require_source.valueHelpSource;
278
298
  exports.valueHelpText = require_texts.valueHelpText;
279
299
  exports.valueLabelOf = require_value_label.valueLabelOf;
300
+ exports.valuePickerMode = require_options.valuePickerMode;