@narrative.io/jsonforms-provider-protocols 2.11.0 → 2.12.0
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/README.md +193 -33
- package/dist/core/initFormData.d.ts +17 -0
- package/dist/core/initFormData.d.ts.map +1 -0
- package/dist/core/initFormData.js +99 -0
- package/dist/core/initFormData.js.map +1 -0
- package/dist/core/projection.d.ts +36 -0
- package/dist/core/projection.d.ts.map +1 -0
- package/dist/core/projection.js +77 -0
- package/dist/core/projection.js.map +1 -0
- package/dist/core/refs.d.ts +58 -0
- package/dist/core/refs.d.ts.map +1 -0
- package/dist/core/refs.js +70 -0
- package/dist/core/refs.js.map +1 -0
- package/dist/core/resolveScope.d.ts +17 -0
- package/dist/core/resolveScope.d.ts.map +1 -0
- package/dist/core/resolveScope.js +28 -0
- package/dist/core/resolveScope.js.map +1 -0
- package/dist/core/seedProjectionTargets.d.ts +60 -0
- package/dist/core/seedProjectionTargets.d.ts.map +1 -0
- package/dist/core/seedProjectionTargets.js +52 -0
- package/dist/core/seedProjectionTargets.js.map +1 -0
- package/dist/core/transforms.d.ts +8 -10
- package/dist/core/transforms.d.ts.map +1 -1
- package/dist/core/transforms.js +58 -13
- package/dist/core/transforms.js.map +1 -1
- package/dist/core/types.d.ts +8 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -3
- package/dist/index.js.map +1 -1
- package/dist/jsonforms-provider-protocols.css +6 -2
- package/dist/no-eval-ajv.d.ts +70 -0
- package/dist/no-eval-ajv.d.ts.map +1 -0
- package/dist/no-eval-ajv.js +247 -0
- package/dist/no-eval-ajv.js.map +1 -0
- package/dist/vue/components/ProviderAutocomplete.vue.d.ts.map +1 -1
- package/dist/vue/components/ProviderAutocomplete.vue.js +12 -6
- package/dist/vue/components/ProviderAutocomplete.vue.js.map +1 -1
- package/dist/vue/components/ProviderMultiSelect.vue.d.ts.map +1 -1
- package/dist/vue/components/ProviderMultiSelect.vue.js +1 -1
- package/dist/vue/components/ProviderMultiSelect.vue2.js +21 -11
- package/dist/vue/components/ProviderMultiSelect.vue2.js.map +1 -1
- package/dist/vue/components/ProviderObjectMultiSelect.vue.d.ts +9 -0
- package/dist/vue/components/ProviderObjectMultiSelect.vue.d.ts.map +1 -0
- package/dist/vue/components/ProviderObjectMultiSelect.vue.js +8 -0
- package/dist/vue/components/ProviderObjectMultiSelect.vue.js.map +1 -0
- package/dist/vue/components/ProviderObjectMultiSelect.vue2.js +142 -0
- package/dist/vue/components/ProviderObjectMultiSelect.vue2.js.map +1 -0
- package/dist/vue/components/ProviderSelect.vue.d.ts.map +1 -1
- package/dist/vue/components/ProviderSelect.vue.js +1 -1
- package/dist/vue/components/ProviderSelect.vue2.js +22 -10
- package/dist/vue/components/ProviderSelect.vue2.js.map +1 -1
- package/dist/vue/composables/useDataLayer.d.ts +10 -0
- package/dist/vue/composables/useDataLayer.d.ts.map +1 -0
- package/dist/vue/composables/useDataLayer.js +26 -0
- package/dist/vue/composables/useDataLayer.js.map +1 -0
- package/dist/vue/composables/useDerive.d.ts +5 -2
- package/dist/vue/composables/useDerive.d.ts.map +1 -1
- package/dist/vue/composables/useDerive.js +29 -12
- package/dist/vue/composables/useDerive.js.map +1 -1
- package/dist/vue/composables/useDeriveInitialValue.d.ts +36 -0
- package/dist/vue/composables/useDeriveInitialValue.d.ts.map +1 -0
- package/dist/vue/composables/useDeriveInitialValue.js +125 -0
- package/dist/vue/composables/useDeriveInitialValue.js.map +1 -0
- package/dist/vue/composables/useDirtyValidation.d.ts +9 -0
- package/dist/vue/composables/useDirtyValidation.d.ts.map +1 -0
- package/dist/vue/composables/useDirtyValidation.js +15 -0
- package/dist/vue/composables/useDirtyValidation.js.map +1 -0
- package/dist/vue/composables/useProjection.d.ts +42 -0
- package/dist/vue/composables/useProjection.d.ts.map +1 -0
- package/dist/vue/composables/useProjection.js +116 -0
- package/dist/vue/composables/useProjection.js.map +1 -0
- package/dist/vue/composables/useProvider.d.ts +2 -2
- package/dist/vue/composables/useProvider.d.ts.map +1 -1
- package/dist/vue/composables/useProvider.js +14 -10
- package/dist/vue/composables/useProvider.js.map +1 -1
- package/dist/vue/index.d.ts +9 -1
- package/dist/vue/index.d.ts.map +1 -1
- package/dist/vue/index.js +72 -34
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/primevue/JfBoolean.vue.d.ts +9 -0
- package/dist/vue/primevue/JfBoolean.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfBoolean.vue.js +44 -17
- package/dist/vue/primevue/JfBoolean.vue.js.map +1 -1
- package/dist/vue/primevue/JfEnum.vue.d.ts +9 -0
- package/dist/vue/primevue/JfEnum.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfEnum.vue.js +38 -24
- package/dist/vue/primevue/JfEnum.vue.js.map +1 -1
- package/dist/vue/primevue/JfEnumArray.vue.d.ts +9 -0
- package/dist/vue/primevue/JfEnumArray.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfEnumArray.vue.js +40 -20
- package/dist/vue/primevue/JfEnumArray.vue.js.map +1 -1
- package/dist/vue/primevue/JfNumber.vue.d.ts +9 -0
- package/dist/vue/primevue/JfNumber.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfNumber.vue.js +33 -23
- package/dist/vue/primevue/JfNumber.vue.js.map +1 -1
- package/dist/vue/primevue/JfText.vue.d.ts +9 -0
- package/dist/vue/primevue/JfText.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfText.vue.js +51 -35
- package/dist/vue/primevue/JfText.vue.js.map +1 -1
- package/dist/vue/primevue/JfTextArea.vue.d.ts +9 -0
- package/dist/vue/primevue/JfTextArea.vue.d.ts.map +1 -1
- package/dist/vue/primevue/JfTextArea.vue.js +34 -19
- package/dist/vue/primevue/JfTextArea.vue.js.map +1 -1
- package/dist/vue/primevue/index.d.ts.map +1 -1
- package/dist/vue/primevue/index.js +100 -8
- package/dist/vue/primevue/index.js.map +1 -1
- package/dist/vue/utils/objectMultiSelect.d.ts +68 -0
- package/dist/vue/utils/objectMultiSelect.d.ts.map +1 -0
- package/dist/vue/utils/objectMultiSelect.js +72 -0
- package/dist/vue/utils/objectMultiSelect.js.map +1 -0
- package/dist/vue/utils/placeholder.d.ts +17 -0
- package/dist/vue/utils/placeholder.d.ts.map +1 -0
- package/dist/vue/utils/placeholder.js +17 -0
- package/dist/vue/utils/placeholder.js.map +1 -0
- package/package.json +10 -2
- package/src/core/initFormData.ts +208 -0
- package/src/core/projection.ts +147 -0
- package/src/core/refs.ts +166 -0
- package/src/core/resolveScope.ts +54 -0
- package/src/core/seedProjectionTargets.ts +144 -0
- package/src/core/transforms.ts +118 -26
- package/src/core/types.ts +9 -0
- package/src/index.ts +22 -2
- package/src/no-eval-ajv.ts +381 -0
- package/src/vue/components/ProviderAutocomplete.vue +11 -7
- package/src/vue/components/ProviderMultiSelect.vue +22 -15
- package/src/vue/components/ProviderObjectMultiSelect.vue +169 -0
- package/src/vue/components/ProviderSelect.vue +23 -14
- package/src/vue/composables/useDataLayer.ts +43 -0
- package/src/vue/composables/useDerive.ts +62 -16
- package/src/vue/composables/useDeriveInitialValue.ts +195 -0
- package/src/vue/composables/useDirtyValidation.ts +20 -0
- package/src/vue/composables/useProjection.ts +245 -0
- package/src/vue/composables/useProvider.ts +28 -11
- package/src/vue/index.ts +83 -47
- package/src/vue/primevue/JfBoolean.vue +35 -12
- package/src/vue/primevue/JfEnum.vue +35 -26
- package/src/vue/primevue/JfEnumArray.vue +37 -20
- package/src/vue/primevue/JfNumber.vue +32 -24
- package/src/vue/primevue/JfText.vue +48 -33
- package/src/vue/primevue/JfTextArea.vue +32 -21
- package/src/vue/primevue/index.ts +114 -8
- package/src/vue/styles.css +26 -1
- package/src/vue/utils/objectMultiSelect.ts +171 -0
- package/src/vue/utils/placeholder.ts +42 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initialize a form data object from a JSON Schema.
|
|
3
|
+
* Resolves $ref, const, default, oneOf/discriminator, and typed empty values.
|
|
4
|
+
*
|
|
5
|
+
* Optional fields (not listed in the parent schema's `required` array) that
|
|
6
|
+
* have no `const`, no single-value `enum`, and no `default` are omitted
|
|
7
|
+
* entirely from the result. This avoids seeding values (e.g. `null` for
|
|
8
|
+
* `type: "integer"`) that fail AJV's type check and surface spurious errors
|
|
9
|
+
* on untouched fields. Required fields retain legacy typed-empty seeding
|
|
10
|
+
* (`""`, `null`, `false`, `[]`) so that "is required" surfaces cleanly.
|
|
11
|
+
*
|
|
12
|
+
* @param schema - The full JSON Schema (must include $defs if $refs are used)
|
|
13
|
+
* @param seed - Optional existing data to merge (seed values take priority)
|
|
14
|
+
* @returns A data object with schema-defined fields initialized
|
|
15
|
+
*/
|
|
16
|
+
export function initFormDataFromSchema(
|
|
17
|
+
schema: Record<string, unknown>,
|
|
18
|
+
seed?: Record<string, unknown>,
|
|
19
|
+
): Record<string, unknown> {
|
|
20
|
+
const result = initProperty(schema, schema, seed, true) as
|
|
21
|
+
| Record<string, unknown>
|
|
22
|
+
| undefined;
|
|
23
|
+
|
|
24
|
+
const base =
|
|
25
|
+
result && typeof result === "object" && !Array.isArray(result)
|
|
26
|
+
? result
|
|
27
|
+
: {};
|
|
28
|
+
|
|
29
|
+
// Preserve seed keys not described by the schema's properties.
|
|
30
|
+
if (seed && typeof seed === "object") {
|
|
31
|
+
const schemaKeys = new Set(
|
|
32
|
+
Object.keys(
|
|
33
|
+
(resolveRef(schema, schema) as Record<string, unknown>)?.properties ??
|
|
34
|
+
{},
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
for (const key of Object.keys(seed)) {
|
|
38
|
+
if (!schemaKeys.has(key) && !(key in base)) {
|
|
39
|
+
base[key] = seed[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return base;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
import { resolveRef } from "./refs";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Initialize a single property value based on its schema definition.
|
|
51
|
+
* Returns `undefined` when the property is optional and has nothing
|
|
52
|
+
* concrete to seed — the caller then omits the key from its result.
|
|
53
|
+
*/
|
|
54
|
+
function initProperty(
|
|
55
|
+
property: Record<string, unknown>,
|
|
56
|
+
root: Record<string, unknown>,
|
|
57
|
+
seed: unknown,
|
|
58
|
+
required: boolean,
|
|
59
|
+
): unknown {
|
|
60
|
+
if (!property || typeof property !== "object") {
|
|
61
|
+
return required ? null : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const resolved = resolveRef(property, root);
|
|
65
|
+
const type = resolved.type as string | undefined;
|
|
66
|
+
const isOneOf = Array.isArray(resolved.oneOf);
|
|
67
|
+
|
|
68
|
+
// Priority 1: seed wins for non-object, non-oneOf types. For oneOf we still
|
|
69
|
+
// descend so that a partial seed (e.g. `{value: 5}` missing the
|
|
70
|
+
// discriminator) picks up the variant's const/default for untouched fields.
|
|
71
|
+
if (seed !== undefined && seed !== null && type !== "object" && !isOneOf) {
|
|
72
|
+
return seed;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Priority 2: const (schema-invariant — always set).
|
|
76
|
+
if ("const" in resolved) {
|
|
77
|
+
return resolved.const;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Priority 3: single-value enum (same reasoning — only one valid value).
|
|
81
|
+
if (
|
|
82
|
+
Array.isArray(resolved.enum) &&
|
|
83
|
+
(resolved.enum as unknown[]).length === 1
|
|
84
|
+
) {
|
|
85
|
+
return (resolved.enum as unknown[])[0];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Priority 4: default (explicit author intent — always honored).
|
|
89
|
+
if ("default" in resolved) {
|
|
90
|
+
return resolved.default;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Priority 5: oneOf. Recurse into the first variant, propagating the
|
|
94
|
+
// `required` flag so that the variant's own schema-declared required fields
|
|
95
|
+
// only get typed-empty seeding when the container is required. For optional
|
|
96
|
+
// containers, the variant's const / default / single-value enum still seed
|
|
97
|
+
// (author intent is unconditional) but typed-empty placeholders do not.
|
|
98
|
+
// If recursion yields nothing forced, we collapse back to undefined.
|
|
99
|
+
if (isOneOf) {
|
|
100
|
+
const value = initOneOf(resolved, root, seed, required);
|
|
101
|
+
if (
|
|
102
|
+
!required &&
|
|
103
|
+
(value === undefined ||
|
|
104
|
+
(value !== null &&
|
|
105
|
+
typeof value === "object" &&
|
|
106
|
+
!Array.isArray(value) &&
|
|
107
|
+
Object.keys(value as Record<string, unknown>).length === 0))
|
|
108
|
+
) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Priority 6: objects recurse. The `required` flag propagates downward:
|
|
115
|
+
// inside an optional ancestor, nested required primitives also collapse,
|
|
116
|
+
// so an optional object with nested required fields stays absent instead
|
|
117
|
+
// of materializing a shell that AJV would flag.
|
|
118
|
+
if (type === "object") {
|
|
119
|
+
const obj = initObject(
|
|
120
|
+
resolved,
|
|
121
|
+
root,
|
|
122
|
+
seed as Record<string, unknown>,
|
|
123
|
+
required,
|
|
124
|
+
);
|
|
125
|
+
if (!required && Object.keys(obj).length === 0) return undefined;
|
|
126
|
+
return obj;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Priority 7: optional primitives — omit.
|
|
130
|
+
if (!required) return undefined;
|
|
131
|
+
|
|
132
|
+
// Priority 8: required primitives — legacy typed empty.
|
|
133
|
+
switch (type) {
|
|
134
|
+
case "array":
|
|
135
|
+
return seed !== undefined && seed !== null ? seed : [];
|
|
136
|
+
case "string":
|
|
137
|
+
return "";
|
|
138
|
+
case "boolean":
|
|
139
|
+
return false;
|
|
140
|
+
case "number":
|
|
141
|
+
case "integer":
|
|
142
|
+
default:
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Initialize an object type by recursing into its properties.
|
|
149
|
+
* Keys whose initialization returns `undefined` are omitted.
|
|
150
|
+
*
|
|
151
|
+
* `parentRequired` controls how schema.required propagates: a child is
|
|
152
|
+
* treated as required only if both its parent is required AND it appears in
|
|
153
|
+
* the parent's required array. Inside an optional ancestor the whole
|
|
154
|
+
* subtree collapses (except for author-forced values: const, default,
|
|
155
|
+
* single-value enum, or seeded values).
|
|
156
|
+
*/
|
|
157
|
+
function initObject(
|
|
158
|
+
schema: Record<string, unknown>,
|
|
159
|
+
root: Record<string, unknown>,
|
|
160
|
+
seed: Record<string, unknown> | undefined,
|
|
161
|
+
parentRequired: boolean,
|
|
162
|
+
): Record<string, unknown> {
|
|
163
|
+
const properties = schema.properties as
|
|
164
|
+
| Record<string, Record<string, unknown>>
|
|
165
|
+
| undefined;
|
|
166
|
+
if (!properties) {
|
|
167
|
+
return seed && typeof seed === "object" ? { ...seed } : {};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const requiredSet = new Set<string>(
|
|
171
|
+
Array.isArray(schema.required) ? (schema.required as string[]) : [],
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const result: Record<string, unknown> = {};
|
|
175
|
+
|
|
176
|
+
for (const [key, propSchema] of Object.entries(properties)) {
|
|
177
|
+
const seedValue = seed && typeof seed === "object" ? seed[key] : undefined;
|
|
178
|
+
const effectiveRequired = parentRequired && requiredSet.has(key);
|
|
179
|
+
const value = initProperty(propSchema, root, seedValue, effectiveRequired);
|
|
180
|
+
if (value !== undefined) {
|
|
181
|
+
result[key] = value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Handle oneOf schemas — pick the first variant and initialize it.
|
|
190
|
+
* `required` propagates: if the outer oneOf is required, the variant is
|
|
191
|
+
* materialized with typed-empty seeding for its required fields; if optional,
|
|
192
|
+
* only author-forced values (const / default / single-enum / seed) survive.
|
|
193
|
+
*/
|
|
194
|
+
function initOneOf(
|
|
195
|
+
schema: Record<string, unknown>,
|
|
196
|
+
root: Record<string, unknown>,
|
|
197
|
+
seed: unknown,
|
|
198
|
+
required: boolean,
|
|
199
|
+
): unknown {
|
|
200
|
+
const variants = schema.oneOf as Record<string, unknown>[];
|
|
201
|
+
if (!variants || variants.length === 0) return required ? null : undefined;
|
|
202
|
+
|
|
203
|
+
const first = variants[0];
|
|
204
|
+
if (!first) return required ? null : undefined;
|
|
205
|
+
|
|
206
|
+
const firstVariant = resolveRef(first, root);
|
|
207
|
+
return initProperty(firstVariant, root, seed, required);
|
|
208
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { deref as derefSchema, tryCombinatorBranches } from "./refs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Projection utilities for navigating complex data structures
|
|
5
|
+
* through a dot-separated path where numeric segments are array indices.
|
|
6
|
+
*
|
|
7
|
+
* Examples:
|
|
8
|
+
* "0" → first element of an array
|
|
9
|
+
* "include" → the `include` property of an object
|
|
10
|
+
* "0.video_rate_usd" → nested property inside the first array element
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type ProjectionSegment = string | number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parse a projection path string into typed segments.
|
|
17
|
+
* Numeric strings become numbers (array indices), others stay as strings (object keys).
|
|
18
|
+
*/
|
|
19
|
+
export function parseProjectionPath(path: string): ProjectionSegment[] {
|
|
20
|
+
if (!path) return [];
|
|
21
|
+
return path.split(".").map((s) => {
|
|
22
|
+
const n = Number(s);
|
|
23
|
+
return Number.isInteger(n) && n >= 0 ? n : s;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Read a value from `data` by following the projection path.
|
|
29
|
+
* Returns `undefined` if any segment along the path is missing.
|
|
30
|
+
*/
|
|
31
|
+
export function getProjectedValue(data: unknown, path: string): unknown {
|
|
32
|
+
const segments = parseProjectionPath(path);
|
|
33
|
+
let current: unknown = data;
|
|
34
|
+
|
|
35
|
+
for (const seg of segments) {
|
|
36
|
+
if (current === null || current === undefined) return undefined;
|
|
37
|
+
|
|
38
|
+
if (typeof seg === "number") {
|
|
39
|
+
if (!Array.isArray(current)) return undefined;
|
|
40
|
+
current = current[seg];
|
|
41
|
+
} else {
|
|
42
|
+
if (typeof current !== "object") return undefined;
|
|
43
|
+
current = (current as Record<string, unknown>)[seg];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return current;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Immutably set a value at the projection path, preserving all sibling data.
|
|
52
|
+
* Constructs missing intermediate structures (arrays for numeric segments, objects for string segments).
|
|
53
|
+
*/
|
|
54
|
+
export function setProjectedValue(
|
|
55
|
+
data: unknown,
|
|
56
|
+
path: string,
|
|
57
|
+
value: unknown,
|
|
58
|
+
): unknown {
|
|
59
|
+
const segments = parseProjectionPath(path);
|
|
60
|
+
return setAtPath(data, segments, 0, value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function setAtPath(
|
|
64
|
+
current: unknown,
|
|
65
|
+
segments: ProjectionSegment[],
|
|
66
|
+
index: number,
|
|
67
|
+
value: unknown,
|
|
68
|
+
): unknown {
|
|
69
|
+
if (index === segments.length) {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const seg = segments[index]!;
|
|
74
|
+
|
|
75
|
+
if (typeof seg === "number") {
|
|
76
|
+
// Array index — ensure we have an array
|
|
77
|
+
const arr = Array.isArray(current) ? [...current] : [];
|
|
78
|
+
// Pad array if index is out of bounds
|
|
79
|
+
while (arr.length <= seg) {
|
|
80
|
+
arr.push(undefined);
|
|
81
|
+
}
|
|
82
|
+
arr[seg] = setAtPath(arr[seg], segments, index + 1, value);
|
|
83
|
+
return arr;
|
|
84
|
+
} else {
|
|
85
|
+
// Object key — ensure we have an object
|
|
86
|
+
const obj: Record<string, unknown> =
|
|
87
|
+
current !== null &&
|
|
88
|
+
current !== undefined &&
|
|
89
|
+
typeof current === "object" &&
|
|
90
|
+
!Array.isArray(current)
|
|
91
|
+
? { ...(current as Record<string, unknown>) }
|
|
92
|
+
: {};
|
|
93
|
+
obj[seg] = setAtPath(obj[seg], segments, index + 1, value);
|
|
94
|
+
return obj;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve the schema at the projected path.
|
|
100
|
+
* Numeric segments traverse into `items` (array item schema).
|
|
101
|
+
* String segments traverse into `properties[segment]`.
|
|
102
|
+
*
|
|
103
|
+
* Dereferences `$ref` nodes transparently at every step, and falls through
|
|
104
|
+
* to `oneOf` / `anyOf` / `allOf` branches when a segment can't resolve
|
|
105
|
+
* directly — picks the first branch that satisfies the navigation.
|
|
106
|
+
*/
|
|
107
|
+
export function getProjectedSchema(
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
109
|
+
schema: Record<string, any>,
|
|
110
|
+
path: string,
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
): Record<string, any> {
|
|
113
|
+
const segments = parseProjectionPath(path);
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
115
|
+
let current: Record<string, any> | undefined = schema;
|
|
116
|
+
|
|
117
|
+
for (const seg of segments) {
|
|
118
|
+
current = derefSchema(current, schema);
|
|
119
|
+
if (!current) return {};
|
|
120
|
+
|
|
121
|
+
const navigate = (
|
|
122
|
+
node: Record<string, unknown>,
|
|
123
|
+
): Record<string, unknown> | undefined => {
|
|
124
|
+
if (typeof seg === "number") {
|
|
125
|
+
const items = (node as { items?: unknown }).items;
|
|
126
|
+
return items && typeof items === "object"
|
|
127
|
+
? (items as Record<string, unknown>)
|
|
128
|
+
: undefined;
|
|
129
|
+
}
|
|
130
|
+
const properties = (node as { properties?: unknown }).properties as
|
|
131
|
+
| Record<string, Record<string, unknown>>
|
|
132
|
+
| undefined;
|
|
133
|
+
if (properties && properties[seg]) return properties[seg];
|
|
134
|
+
return undefined;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
let next = navigate(current);
|
|
138
|
+
if (next === undefined) {
|
|
139
|
+
next = tryCombinatorBranches(current, schema, navigate);
|
|
140
|
+
}
|
|
141
|
+
if (!next) return {};
|
|
142
|
+
current = next;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const resolved = derefSchema(current, schema);
|
|
146
|
+
return resolved ?? {};
|
|
147
|
+
}
|
package/src/core/refs.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Schema $ref resolution helpers.
|
|
3
|
+
*
|
|
4
|
+
* Supports the pointer grammar used across consumer schemas:
|
|
5
|
+
* - "#/$defs/Name"
|
|
6
|
+
* - "#/properties/foo/items"
|
|
7
|
+
*
|
|
8
|
+
* External refs (URIs, file refs) are intentionally out of scope. A ref that
|
|
9
|
+
* doesn't resolve leaves the node untouched — callers can still inspect the
|
|
10
|
+
* unresolved `$ref` for debugging.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve a JSON pointer (`#/a/b/c`) against an object. Returns the node at
|
|
15
|
+
* that path, or `undefined` if any segment is missing or the pointer doesn't
|
|
16
|
+
* start with `#/`.
|
|
17
|
+
*/
|
|
18
|
+
export function resolvePointer(
|
|
19
|
+
obj: Record<string, unknown>,
|
|
20
|
+
pointer: string,
|
|
21
|
+
): unknown {
|
|
22
|
+
if (!pointer.startsWith("#/")) return undefined;
|
|
23
|
+
const parts = pointer.slice(2).split("/");
|
|
24
|
+
let current: unknown = obj;
|
|
25
|
+
for (const part of parts) {
|
|
26
|
+
if (current && typeof current === "object" && part in current) {
|
|
27
|
+
current = (current as Record<string, unknown>)[part];
|
|
28
|
+
} else {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return current;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Dereference a schema node along a chain of `$ref`s. Follows `A → B → C`
|
|
37
|
+
* transitively. A cycle (same `$ref` seen twice in one chain) returns the
|
|
38
|
+
* last unresolved node rather than hanging. An unresolvable pointer returns
|
|
39
|
+
* the current node unchanged.
|
|
40
|
+
*/
|
|
41
|
+
export function resolveRef(
|
|
42
|
+
property: Record<string, unknown>,
|
|
43
|
+
root: Record<string, unknown>,
|
|
44
|
+
seen?: Set<string>,
|
|
45
|
+
): Record<string, unknown> {
|
|
46
|
+
if (!property || typeof property !== "object") return property;
|
|
47
|
+
|
|
48
|
+
const ref = property.$ref as string | undefined;
|
|
49
|
+
if (!ref) return property;
|
|
50
|
+
|
|
51
|
+
const visited = seen ?? new Set<string>();
|
|
52
|
+
if (visited.has(ref)) return property;
|
|
53
|
+
visited.add(ref);
|
|
54
|
+
|
|
55
|
+
const resolved = resolvePointer(root, ref);
|
|
56
|
+
if (!resolved) return property;
|
|
57
|
+
|
|
58
|
+
return resolveRef(resolved as Record<string, unknown>, root, visited);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Convenience wrapper around `resolveRef` that starts a fresh cycle-detection
|
|
63
|
+
* set. Intended for schema walkers that need to dereference at every step;
|
|
64
|
+
* each call is an independent resolution.
|
|
65
|
+
*/
|
|
66
|
+
export function deref(
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
node: Record<string, any> | undefined,
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
|
+
root: Record<string, any>,
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
): Record<string, any> | undefined {
|
|
73
|
+
if (!node || typeof node !== "object") return node;
|
|
74
|
+
return resolveRef(node, root) as Record<string, unknown> | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Depth limit for combinator (oneOf/anyOf/allOf) branch descent. Schemas
|
|
79
|
+
* rarely nest combinators beyond one or two levels; this guard protects
|
|
80
|
+
* against pathological nesting and cycles (e.g. `oneOf: [$ref back to self]`).
|
|
81
|
+
*/
|
|
82
|
+
const COMBINATOR_DEPTH_LIMIT = 8;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Try to navigate a segment through a schema node's combinator branches
|
|
86
|
+
* (`oneOf` / `anyOf` / `allOf`) when direct navigation has failed.
|
|
87
|
+
*
|
|
88
|
+
* Semantics: for walker purposes (renderer-tester matching), we only need
|
|
89
|
+
* ONE concrete schema that satisfies the next navigation step. First-match
|
|
90
|
+
* by structural shape wins, same convention as `initOneOf` uses for seeding.
|
|
91
|
+
*
|
|
92
|
+
* @param node the schema node to search (already dereffed by caller)
|
|
93
|
+
* @param root the root schema, for dereferencing branch `$ref`s
|
|
94
|
+
* @param tryFn predicate that attempts navigation on a candidate branch
|
|
95
|
+
* and returns the navigated value, or `undefined` if the
|
|
96
|
+
* branch doesn't have what the caller's looking for
|
|
97
|
+
* @param depth recursion depth (capped at `COMBINATOR_DEPTH_LIMIT`)
|
|
98
|
+
*/
|
|
99
|
+
export function tryCombinatorBranches<T>(
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
node: Record<string, any> | undefined,
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
+
root: Record<string, any>,
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
105
|
+
tryFn: (candidate: Record<string, any>) => T | undefined,
|
|
106
|
+
depth = 0,
|
|
107
|
+
): T | undefined {
|
|
108
|
+
if (depth > COMBINATOR_DEPTH_LIMIT) return undefined;
|
|
109
|
+
if (!node || typeof node !== "object") return undefined;
|
|
110
|
+
|
|
111
|
+
const branches = (node.oneOf || node.anyOf || node.allOf) as
|
|
112
|
+
| Record<string, unknown>[]
|
|
113
|
+
| undefined;
|
|
114
|
+
if (!Array.isArray(branches)) return undefined;
|
|
115
|
+
|
|
116
|
+
for (const raw of branches) {
|
|
117
|
+
const branch = deref(raw as Record<string, unknown>, root);
|
|
118
|
+
if (!branch || typeof branch !== "object") continue;
|
|
119
|
+
|
|
120
|
+
const direct = tryFn(branch);
|
|
121
|
+
if (direct !== undefined) return direct;
|
|
122
|
+
|
|
123
|
+
const nested = tryCombinatorBranches(branch, root, tryFn, depth + 1);
|
|
124
|
+
if (nested !== undefined) return nested;
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Recursively dereference every `$ref` in a schema subtree, producing a
|
|
131
|
+
* concrete schema with no remaining refs. Cycles along any single chain are
|
|
132
|
+
* handled by leaving the first recursion back into a seen ref as the
|
|
133
|
+
* unresolved node — matching `resolveRef`'s semantics.
|
|
134
|
+
*
|
|
135
|
+
* Intended for use at API boundaries (e.g. `resolveScopeSchema`'s return)
|
|
136
|
+
* so downstream walkers can operate on self-contained schemas without
|
|
137
|
+
* needing the original root.
|
|
138
|
+
*/
|
|
139
|
+
export function deepDeref(
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
141
|
+
node: any,
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
143
|
+
root: Record<string, any>,
|
|
144
|
+
seen: Set<string> = new Set(),
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
146
|
+
): any {
|
|
147
|
+
if (!node || typeof node !== "object") return node;
|
|
148
|
+
if (Array.isArray(node)) return node.map((n) => deepDeref(n, root, seen));
|
|
149
|
+
|
|
150
|
+
const ref = (node as Record<string, unknown>).$ref as string | undefined;
|
|
151
|
+
if (typeof ref === "string") {
|
|
152
|
+
if (seen.has(ref)) return node;
|
|
153
|
+
const resolved = resolvePointer(root, ref);
|
|
154
|
+
if (!resolved || typeof resolved !== "object") return node;
|
|
155
|
+
const nextSeen = new Set(seen);
|
|
156
|
+
nextSeen.add(ref);
|
|
157
|
+
return deepDeref(resolved, root, nextSeen);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
const result: Record<string, any> = {};
|
|
162
|
+
for (const [key, value] of Object.entries(node)) {
|
|
163
|
+
result[key] = deepDeref(value, root, seen);
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { deepDeref, deref, tryCombinatorBranches } from "./refs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolve a JSON Forms scope path to its schema within a root schema.
|
|
5
|
+
* Handles nested paths like "#/properties/parent/properties/child".
|
|
6
|
+
*
|
|
7
|
+
* Follows JSON Schema structure:
|
|
8
|
+
* - "properties" segments navigate into object `.properties`
|
|
9
|
+
* - "items" segments navigate into array `.items`
|
|
10
|
+
* - all other segments index directly into the current object
|
|
11
|
+
*
|
|
12
|
+
* Dereferences `$ref` nodes transparently at every step, and falls through
|
|
13
|
+
* to `oneOf` / `anyOf` / `allOf` branches when a segment can't resolve
|
|
14
|
+
* directly — picks the first branch that satisfies the navigation. The
|
|
15
|
+
* returned schema is deep-dereferenced so downstream walkers can operate on
|
|
16
|
+
* a self-contained sub-schema without needing the original root.
|
|
17
|
+
*/
|
|
18
|
+
export function resolveScopeSchema(
|
|
19
|
+
scope: string,
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
+
rootSchema: Record<string, any>,
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
): Record<string, any> | undefined {
|
|
24
|
+
if (!scope || !rootSchema) return undefined;
|
|
25
|
+
|
|
26
|
+
// Remove the leading "#/" and split into segments
|
|
27
|
+
const path = scope.replace(/^#\/?/, "");
|
|
28
|
+
if (!path) return deepDeref(rootSchema, rootSchema);
|
|
29
|
+
|
|
30
|
+
const segments = path.split("/");
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
let current: any = rootSchema;
|
|
33
|
+
|
|
34
|
+
for (const segment of segments) {
|
|
35
|
+
current = deref(current, rootSchema);
|
|
36
|
+
if (!current || typeof current !== "object") return undefined;
|
|
37
|
+
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
const navigate = (node: Record<string, any>): unknown => {
|
|
40
|
+
if (segment === "properties") return node.properties;
|
|
41
|
+
if (segment === "items") return node.items;
|
|
42
|
+
return node[segment];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
let next = navigate(current);
|
|
46
|
+
if (next === undefined) {
|
|
47
|
+
next = tryCombinatorBranches(current, rootSchema, navigate);
|
|
48
|
+
}
|
|
49
|
+
if (next === undefined) return undefined;
|
|
50
|
+
current = next;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return deepDeref(current, rootSchema);
|
|
54
|
+
}
|