@narrative.io/jsonforms-provider-protocols 3.0.0-beta.22 → 3.0.0-beta.23
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"seedProjectionTargets.d.ts","sourceRoot":"","sources":["../../src/core/seedProjectionTargets.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,YAAY,GAAG,YAAY,EAAE,GAAG,SAAS,GAClD,OAAO,
|
|
1
|
+
{"version":3,"file":"seedProjectionTargets.d.ts","sourceRoot":"","sources":["../../src/core/seedProjectionTargets.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,OAAO,EACb,QAAQ,EAAE,YAAY,GAAG,YAAY,EAAE,GAAG,SAAS,GAClD,OAAO,CAoCT;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC1D,QAAQ,CAAC,EAAE,YAAY,EAAE,CAAC;IAC1B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB"}
|
|
@@ -9,6 +9,8 @@ function seedProjectionTargets(data, uischema) {
|
|
|
9
9
|
const segments = parseProjectionPath(projection);
|
|
10
10
|
for (let i = 0; i < segments.length; i++) {
|
|
11
11
|
if (typeof segments[i] !== "number") continue;
|
|
12
|
+
if (i + 1 >= segments.length) continue;
|
|
13
|
+
if (typeof segments[i + 1] !== "string") continue;
|
|
12
14
|
const partial = segments.slice(0, i + 1).map((s) => String(s)).join(".");
|
|
13
15
|
const fullPath = dataPath ? `${dataPath}.${partial}` : partial;
|
|
14
16
|
if (getProjectedValue(result, fullPath) === void 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"seedProjectionTargets.js","sources":["../../src/core/seedProjectionTargets.ts"],"sourcesContent":["import {\n getProjectedValue,\n parseProjectionPath,\n setProjectedValue,\n} from \"./projection\";\n\n/**\n * Materialize array indices targeted by `options.projection` controls in a\n * UI schema, so that JSON Schema validators (AJV / cfworker) emit\n * `items.required` errors on otherwise-empty arrays.\n *\n * Why this exists:\n * `initFormDataFromSchema` omits optional fields, so an optional array\n * like `data_rates: { type: 'array', items: { $ref: '#/$defs/DataRate' } }`\n * produces `undefined` (or, historically, `[]`). Both shapes pass schema\n * validation: `undefined` doesn't trigger `type: array`, and `[]` has no\n * items to apply `items.required` to. As a result, a projection-targeted\n * field rendered with `Video CPM rate *` lies — the asterisk says\n * \"required\" but the validator never enforces it on an untouched form.\n *\n * This utility opts the consumer into per-item enforcement by walking the\n * UI schema, finding every `Control` with `options.projection` that\n * addresses an array index, and ensuring the corresponding data path has\n * that index materialized as at least `{}`. With the empty object in\n * place, the schema's `items.required` fires and the projected control's\n * error string surfaces at form-validity time.\n *\n * Tradeoff:\n * This re-introduces \"noise\" on untouched forms — required-field errors\n * for fields the user hasn't seen yet. Use it when validation enforcement\n * on projection targets matters more than a clean initial form state.\n * The alternative is to declare these arrays as `required` + `minItems: 1`\n * + `default: [{}]` at the schema level, which avoids needing this helper.\n *\n * Properties:\n * - Idempotent: running twice yields the same result as running once.\n * - Non-destructive: existing values at target paths are preserved.\n * - Pure: returns a new object; does not mutate `data`.\n *\n * Example:\n * ```ts\n * const data = initFormDataFromSchema(schema);\n * const seeded = seedProjectionTargets(data, uischema);\n * // For uischema controls like\n * // { type: 'Control', scope: '#/properties/data_rates',\n * // options: { projection: '0.video_rate_usd' } }\n * // seeded.data_rates is now `[{}]` (was `undefined` or `[]`).\n * ```\n */\nexport function seedProjectionTargets(\n data: unknown,\n uischema: UISchemaLike | UISchemaLike[] | undefined,\n): unknown {\n if (!uischema) return data;\n\n const controls: { scope: string; projection: string }[] = [];\n collectProjectionControls(uischema, controls);\n\n let result = data;\n for (const { scope, projection } of controls) {\n const dataPath = scopeToDataPath(scope);\n const segments = parseProjectionPath(projection);\n\n // Seed
|
|
1
|
+
{"version":3,"file":"seedProjectionTargets.js","sources":["../../src/core/seedProjectionTargets.ts"],"sourcesContent":["import {\n getProjectedValue,\n parseProjectionPath,\n setProjectedValue,\n} from \"./projection\";\n\n/**\n * Materialize array indices targeted by `options.projection` controls in a\n * UI schema, so that JSON Schema validators (AJV / cfworker) emit\n * `items.required` errors on otherwise-empty arrays.\n *\n * Why this exists:\n * `initFormDataFromSchema` omits optional fields, so an optional array\n * like `data_rates: { type: 'array', items: { $ref: '#/$defs/DataRate' } }`\n * produces `undefined` (or, historically, `[]`). Both shapes pass schema\n * validation: `undefined` doesn't trigger `type: array`, and `[]` has no\n * items to apply `items.required` to. As a result, a projection-targeted\n * field rendered with `Video CPM rate *` lies — the asterisk says\n * \"required\" but the validator never enforces it on an untouched form.\n *\n * This utility opts the consumer into per-item enforcement by walking the\n * UI schema, finding every `Control` with `options.projection` that\n * addresses an array index, and ensuring the corresponding data path has\n * that index materialized as at least `{}`. With the empty object in\n * place, the schema's `items.required` fires and the projected control's\n * error string surfaces at form-validity time.\n *\n * Tradeoff:\n * This re-introduces \"noise\" on untouched forms — required-field errors\n * for fields the user hasn't seen yet. Use it when validation enforcement\n * on projection targets matters more than a clean initial form state.\n * The alternative is to declare these arrays as `required` + `minItems: 1`\n * + `default: [{}]` at the schema level, which avoids needing this helper.\n *\n * Properties:\n * - Idempotent: running twice yields the same result as running once.\n * - Non-destructive: existing values at target paths are preserved.\n * - Pure: returns a new object; does not mutate `data`.\n *\n * Example:\n * ```ts\n * const data = initFormDataFromSchema(schema);\n * const seeded = seedProjectionTargets(data, uischema);\n * // For uischema controls like\n * // { type: 'Control', scope: '#/properties/data_rates',\n * // options: { projection: '0.video_rate_usd' } }\n * // seeded.data_rates is now `[{}]` (was `undefined` or `[]`).\n * ```\n */\nexport function seedProjectionTargets(\n data: unknown,\n uischema: UISchemaLike | UISchemaLike[] | undefined,\n): unknown {\n if (!uischema) return data;\n\n const controls: { scope: string; projection: string }[] = [];\n collectProjectionControls(uischema, controls);\n\n let result = data;\n for (const { scope, projection } of controls) {\n const dataPath = scopeToDataPath(scope);\n const segments = parseProjectionPath(projection);\n\n // Seed only when a numeric segment is followed by a *string* segment —\n // i.e., the consumer is reading a property of the array item, so the\n // item must be an object. Numeric-at-end (`'0'`) addresses the array\n // element itself, which can be any type (primitive, nested array,\n // object) — we can't infer it from the uischema alone, so leave it\n // alone. Numeric-followed-by-numeric (`'0.0'`) is a nested array; we'd\n // need to seed `[]`, but again the inner item type is unknown.\n for (let i = 0; i < segments.length; i++) {\n if (typeof segments[i] !== \"number\") continue;\n if (i + 1 >= segments.length) continue;\n if (typeof segments[i + 1] !== \"string\") continue;\n\n const partial = segments\n .slice(0, i + 1)\n .map((s) => String(s))\n .join(\".\");\n const fullPath = dataPath ? `${dataPath}.${partial}` : partial;\n\n if (getProjectedValue(result, fullPath) === undefined) {\n result = setProjectedValue(result, fullPath, {});\n }\n }\n }\n\n return result;\n}\n\n/**\n * Minimal UI schema shape this utility traverses. Compatible with\n * `@jsonforms/core`'s `UISchemaElement` but kept local to avoid a runtime\n * dep on `@jsonforms/core` (it's a peer dep — types only).\n */\nexport interface UISchemaLike {\n type?: string;\n scope?: string;\n options?: { projection?: string; [key: string]: unknown };\n elements?: UISchemaLike[];\n [key: string]: unknown;\n}\n\nfunction collectProjectionControls(\n ui: UISchemaLike | UISchemaLike[] | undefined,\n out: { scope: string; projection: string }[],\n): void {\n if (!ui) return;\n if (Array.isArray(ui)) {\n for (const el of ui) collectProjectionControls(el, out);\n return;\n }\n if (typeof ui !== \"object\") return;\n\n const projection = ui.options?.projection;\n if (\n ui.type === \"Control\" &&\n typeof ui.scope === \"string\" &&\n typeof projection === \"string\"\n ) {\n out.push({ scope: ui.scope, projection });\n }\n\n if (Array.isArray(ui.elements)) {\n for (const el of ui.elements) collectProjectionControls(el, out);\n }\n}\n\n/**\n * Convert a JsonForms scope pointer (`#/properties/foo/properties/bar`) to\n * a dot-separated data path (`foo.bar`). Drops `items` segments — array\n * indices are added at runtime via the projection path, not the scope.\n */\nfunction scopeToDataPath(scope: string): string {\n if (!scope.startsWith(\"#/\")) return \"\";\n const parts = scope.slice(2).split(\"/\");\n const out: string[] = [];\n for (let i = 0; i < parts.length; i++) {\n if (parts[i] === \"properties\" && i + 1 < parts.length) {\n out.push(parts[++i]!);\n }\n }\n return out.join(\".\");\n}\n"],"names":[],"mappings":";AAiDO,SAAS,sBACd,MACA,UACS;AACT,MAAI,CAAC,SAAU,QAAO;AAEtB,QAAM,WAAoD,CAAA;AAC1D,4BAA0B,UAAU,QAAQ;AAE5C,MAAI,SAAS;AACb,aAAW,EAAE,OAAO,WAAA,KAAgB,UAAU;AAC5C,UAAM,WAAW,gBAAgB,KAAK;AACtC,UAAM,WAAW,oBAAoB,UAAU;AAS/C,aAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,UAAI,OAAO,SAAS,CAAC,MAAM,SAAU;AACrC,UAAI,IAAI,KAAK,SAAS,OAAQ;AAC9B,UAAI,OAAO,SAAS,IAAI,CAAC,MAAM,SAAU;AAEzC,YAAM,UAAU,SACb,MAAM,GAAG,IAAI,CAAC,EACd,IAAI,CAAC,MAAM,OAAO,CAAC,CAAC,EACpB,KAAK,GAAG;AACX,YAAM,WAAW,WAAW,GAAG,QAAQ,IAAI,OAAO,KAAK;AAEvD,UAAI,kBAAkB,QAAQ,QAAQ,MAAM,QAAW;AACrD,iBAAS,kBAAkB,QAAQ,UAAU,CAAA,CAAE;AAAA,MACjD;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAeA,SAAS,0BACP,IACA,KACM;AACN,MAAI,CAAC,GAAI;AACT,MAAI,MAAM,QAAQ,EAAE,GAAG;AACrB,eAAW,MAAM,GAAI,2BAA0B,IAAI,GAAG;AACtD;AAAA,EACF;AACA,MAAI,OAAO,OAAO,SAAU;AAE5B,QAAM,aAAa,GAAG,SAAS;AAC/B,MACE,GAAG,SAAS,aACZ,OAAO,GAAG,UAAU,YACpB,OAAO,eAAe,UACtB;AACA,QAAI,KAAK,EAAE,OAAO,GAAG,OAAO,YAAY;AAAA,EAC1C;AAEA,MAAI,MAAM,QAAQ,GAAG,QAAQ,GAAG;AAC9B,eAAW,MAAM,GAAG,SAAU,2BAA0B,IAAI,GAAG;AAAA,EACjE;AACF;AAOA,SAAS,gBAAgB,OAAuB;AAC9C,MAAI,CAAC,MAAM,WAAW,IAAI,EAAG,QAAO;AACpC,QAAM,QAAQ,MAAM,MAAM,CAAC,EAAE,MAAM,GAAG;AACtC,QAAM,MAAgB,CAAA;AACtB,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,QAAI,MAAM,CAAC,MAAM,gBAAgB,IAAI,IAAI,MAAM,QAAQ;AACrD,UAAI,KAAK,MAAM,EAAE,CAAC,CAAE;AAAA,IACtB;AAAA,EACF;AACA,SAAO,IAAI,KAAK,GAAG;AACrB;"}
|
package/package.json
CHANGED
|
@@ -61,11 +61,17 @@ export function seedProjectionTargets(
|
|
|
61
61
|
const dataPath = scopeToDataPath(scope);
|
|
62
62
|
const segments = parseProjectionPath(projection);
|
|
63
63
|
|
|
64
|
-
// Seed
|
|
65
|
-
//
|
|
66
|
-
//
|
|
64
|
+
// Seed only when a numeric segment is followed by a *string* segment —
|
|
65
|
+
// i.e., the consumer is reading a property of the array item, so the
|
|
66
|
+
// item must be an object. Numeric-at-end (`'0'`) addresses the array
|
|
67
|
+
// element itself, which can be any type (primitive, nested array,
|
|
68
|
+
// object) — we can't infer it from the uischema alone, so leave it
|
|
69
|
+
// alone. Numeric-followed-by-numeric (`'0.0'`) is a nested array; we'd
|
|
70
|
+
// need to seed `[]`, but again the inner item type is unknown.
|
|
67
71
|
for (let i = 0; i < segments.length; i++) {
|
|
68
72
|
if (typeof segments[i] !== "number") continue;
|
|
73
|
+
if (i + 1 >= segments.length) continue;
|
|
74
|
+
if (typeof segments[i + 1] !== "string") continue;
|
|
69
75
|
|
|
70
76
|
const partial = segments
|
|
71
77
|
.slice(0, i + 1)
|