@atscript/ui-fns 0.1.157 → 0.1.159

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/index.cjs CHANGED
@@ -123,9 +123,29 @@ function buildFieldEntry(prop, baseScope, path, opts) {
123
123
  //#endregion
124
124
  //#region src/runtime/validator-plugin.ts
125
125
  /**
126
+ * Whether `value` does NOT have the shape of container `kind` (an object, or
127
+ * an array for array / tuple). A container rule only sees a value of its
128
+ * container's shape; anything else (a missing object, a non-array) falls
129
+ * through to the validator's own shape error instead of handing the rule a
130
+ * value it would crash on.
131
+ */
132
+ function containerShapeMismatch(kind, value) {
133
+ if (kind === "object") return typeof value !== "object" || value === null || Array.isArray(value);
134
+ return !Array.isArray(value);
135
+ }
136
+ /**
126
137
  * Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
127
138
  * using compiled function strings.
128
139
  *
140
+ * The rule receives `(value, data, context, entry)`: `value` is the node's own
141
+ * value (the whole object for an interface / object-type rule), `data` is the
142
+ * form data passed in the validation context.
143
+ *
144
+ * On a leaf, a failing rule ends that leaf's validation (`@expect.*` is not
145
+ * checked). On an object / array / tuple, a failing rule is recorded at the
146
+ * container's path and its children are still validated, so a container-level
147
+ * rule never hides field-level errors.
148
+ *
129
149
  * Usage:
130
150
  * const plugin = uiFnsValidatorPlugin()
131
151
  * const validator = new Validator(field.prop, { plugins: [plugin] })
@@ -135,6 +155,8 @@ function uiFnsValidatorPlugin() {
135
155
  return (ctx, def, value) => {
136
156
  const hasValidators = (0, _atscript_ui.getFieldMeta)(def, _atscript_ui.UI_FORM_VALIDATE);
137
157
  if (!hasValidators) return void 0;
158
+ const container = (0, _atscript_ui.isContainerKind)(def.type.kind);
159
+ if (container && containerShapeMismatch(def.type.kind, value)) return void 0;
138
160
  const fnsCtx = ctx.context;
139
161
  const scope = buildFieldEntry(def, {
140
162
  v: value,
@@ -148,6 +170,7 @@ function uiFnsValidatorPlugin() {
148
170
  const result = compileValidatorFn(fnStr)(scope);
149
171
  if (result !== true) {
150
172
  ctx.error(typeof result === "string" ? result : "Validation failed");
173
+ if (container) ctx.validateAnnotatedType(def, value);
151
174
  return false;
152
175
  }
153
176
  }
package/dist/index.d.cts CHANGED
@@ -100,6 +100,15 @@ interface TValidatorContext {
100
100
  * Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
101
101
  * using compiled function strings.
102
102
  *
103
+ * The rule receives `(value, data, context, entry)`: `value` is the node's own
104
+ * value (the whole object for an interface / object-type rule), `data` is the
105
+ * form data passed in the validation context.
106
+ *
107
+ * On a leaf, a failing rule ends that leaf's validation (`@expect.*` is not
108
+ * checked). On an object / array / tuple, a failing rule is recorded at the
109
+ * container's path and its children are still validated, so a container-level
110
+ * rule never hides field-level errors.
111
+ *
103
112
  * Usage:
104
113
  * const plugin = uiFnsValidatorPlugin()
105
114
  * const validator = new Validator(field.prop, { plugins: [plugin] })
package/dist/index.d.mts CHANGED
@@ -100,6 +100,15 @@ interface TValidatorContext {
100
100
  * Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
101
101
  * using compiled function strings.
102
102
  *
103
+ * The rule receives `(value, data, context, entry)`: `value` is the node's own
104
+ * value (the whole object for an interface / object-type rule), `data` is the
105
+ * form data passed in the validation context.
106
+ *
107
+ * On a leaf, a failing rule ends that leaf's validation (`@expect.*` is not
108
+ * checked). On an object / array / tuple, a failing rule is recorded at the
109
+ * container's path and its children are still validated, so a container-level
110
+ * rule never hides field-level errors.
111
+ *
103
112
  * Usage:
104
113
  * const plugin = uiFnsValidatorPlugin()
105
114
  * const validator = new Validator(field.prop, { plugins: [plugin] })
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { META_READONLY, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_HIDDEN, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_TABLE_FN_ATTR, UI_TABLE_FN_PREFIX, UI_TYPE, asArray, getFieldMeta, resolveFieldProp, resolveOptions, resolveStatic, setDefaultValidatorPlugins, setResolver } from "@atscript/ui";
1
+ import { META_READONLY, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_HIDDEN, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_TABLE_FN_ATTR, UI_TABLE_FN_PREFIX, UI_TYPE, asArray, getFieldMeta, isContainerKind, resolveFieldProp, resolveOptions, resolveStatic, setDefaultValidatorPlugins, setResolver } from "@atscript/ui";
2
2
  import { FNPool } from "@prostojs/deserialize-fn";
3
3
  //#region src/runtime/fn-compiler.ts
4
4
  const pool = new FNPool();
@@ -123,9 +123,29 @@ function buildFieldEntry(prop, baseScope, path, opts) {
123
123
  //#endregion
124
124
  //#region src/runtime/validator-plugin.ts
125
125
  /**
126
+ * Whether `value` does NOT have the shape of container `kind` (an object, or
127
+ * an array for array / tuple). A container rule only sees a value of its
128
+ * container's shape; anything else (a missing object, a non-array) falls
129
+ * through to the validator's own shape error instead of handing the rule a
130
+ * value it would crash on.
131
+ */
132
+ function containerShapeMismatch(kind, value) {
133
+ if (kind === "object") return typeof value !== "object" || value === null || Array.isArray(value);
134
+ return !Array.isArray(value);
135
+ }
136
+ /**
126
137
  * Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
127
138
  * using compiled function strings.
128
139
  *
140
+ * The rule receives `(value, data, context, entry)`: `value` is the node's own
141
+ * value (the whole object for an interface / object-type rule), `data` is the
142
+ * form data passed in the validation context.
143
+ *
144
+ * On a leaf, a failing rule ends that leaf's validation (`@expect.*` is not
145
+ * checked). On an object / array / tuple, a failing rule is recorded at the
146
+ * container's path and its children are still validated, so a container-level
147
+ * rule never hides field-level errors.
148
+ *
129
149
  * Usage:
130
150
  * const plugin = uiFnsValidatorPlugin()
131
151
  * const validator = new Validator(field.prop, { plugins: [plugin] })
@@ -135,6 +155,8 @@ function uiFnsValidatorPlugin() {
135
155
  return (ctx, def, value) => {
136
156
  const hasValidators = getFieldMeta(def, UI_FORM_VALIDATE);
137
157
  if (!hasValidators) return void 0;
158
+ const container = isContainerKind(def.type.kind);
159
+ if (container && containerShapeMismatch(def.type.kind, value)) return void 0;
138
160
  const fnsCtx = ctx.context;
139
161
  const scope = buildFieldEntry(def, {
140
162
  v: value,
@@ -148,6 +170,7 @@ function uiFnsValidatorPlugin() {
148
170
  const result = compileValidatorFn(fnStr)(scope);
149
171
  if (result !== true) {
150
172
  ctx.error(typeof result === "string" ? result : "Validation failed");
173
+ if (container) ctx.validateAnnotatedType(def, value);
151
174
  return false;
152
175
  }
153
176
  }
package/dist/plugin.cjs CHANGED
@@ -110,8 +110,12 @@ const tableFnAttrSpec = new _atscript_core.AnnotationSpec({
110
110
  const uiFnsAnnotations = { ui: {
111
111
  form: {
112
112
  validate: new _atscript_core.AnnotationSpec({
113
- description: "Custom JS validator function string. Returns true for pass, or an error message string.",
114
- nodeType: ["prop", "type"],
113
+ description: "Custom JS validator function string. Returns true for pass, or an error message string. On a field it validates that field. On an interface or object type it is a container-level rule: `value` is the whole object. On the form's root interface the error is form-level (the `__form` banner); on a nested object prop it is reported at that prop's path. Container rules do not stop field-level validation.",
114
+ nodeType: [
115
+ "prop",
116
+ "interface",
117
+ "type"
118
+ ],
115
119
  multiple: true,
116
120
  mergeStrategy: "append",
117
121
  argument: {
package/dist/plugin.mjs CHANGED
@@ -110,8 +110,12 @@ const tableFnAttrSpec = new AnnotationSpec({
110
110
  const uiFnsAnnotations = { ui: {
111
111
  form: {
112
112
  validate: new AnnotationSpec({
113
- description: "Custom JS validator function string. Returns true for pass, or an error message string.",
114
- nodeType: ["prop", "type"],
113
+ description: "Custom JS validator function string. Returns true for pass, or an error message string. On a field it validates that field. On an interface or object type it is a container-level rule: `value` is the whole object. On the form's root interface the error is form-level (the `__form` banner); on a nested object prop it is reported at that prop's path. Container rules do not stop field-level validation.",
114
+ nodeType: [
115
+ "prop",
116
+ "interface",
117
+ "type"
118
+ ],
115
119
  multiple: true,
116
120
  mergeStrategy: "append",
117
121
  argument: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/ui-fns",
3
- "version": "0.1.157",
3
+ "version": "0.1.159",
4
4
  "description": "Dynamic fn-compiled field properties for @atscript/ui (opt-in, uses new Function)",
5
5
  "keywords": [
6
6
  "annotations",
@@ -48,16 +48,16 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "@prostojs/deserialize-fn": "^0.0.6",
51
- "@atscript/ui": "^0.1.157"
51
+ "@atscript/ui": "^0.1.159"
52
52
  },
53
53
  "devDependencies": {
54
- "@atscript/core": "^0.1.107",
55
- "@atscript/typescript": "^0.1.107",
54
+ "@atscript/core": "^0.1.109",
55
+ "@atscript/typescript": "^0.1.109",
56
56
  "vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14"
57
57
  },
58
58
  "peerDependencies": {
59
- "@atscript/core": "^0.1.107",
60
- "@atscript/typescript": "^0.1.107"
59
+ "@atscript/core": "^0.1.109",
60
+ "@atscript/typescript": "^0.1.109"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "vp pack",