@orpc/json-schema 2.0.0-beta.49 → 2.0.0-beta.50
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/coercer.d.ts +26 -26
- package/dist/coercer.js +316 -460
- package/dist/composition-utils.d.ts +4 -8
- package/dist/composition-utils.js +281 -351
- package/dist/constants.js +63 -62
- package/dist/convert.d.ts +16 -16
- package/dist/convert.js +12 -14
- package/dist/index.d.ts +11 -10
- package/dist/index.js +9 -9
- package/dist/ref-utils.d.ts +3 -7
- package/dist/ref-utils.js +178 -228
- package/dist/smart-coercion-handler-plugin.d.ts +13 -13
- package/dist/smart-coercion-handler-plugin.js +46 -49
- package/dist/smart-coercion-link-plugin.d.ts +19 -19
- package/dist/smart-coercion-link-plugin.js +62 -74
- package/dist/standard-json-schema-converter.d.ts +8 -8
- package/dist/standard-json-schema-converter.js +34 -43
- package/dist/types.d.ts +34 -36
- package/dist/types.js +42 -40
- package/dist/utils.d.ts +6 -10
- package/dist/utils.js +59 -91
- package/package.json +6 -6
- package/dist/constants.d.ts +0 -8
|
@@ -1,380 +1,310 @@
|
|
|
1
|
+
import { JSON_SCHEMA_LOGIC_KEYWORDS } from "./constants.js";
|
|
2
|
+
import { decodeJsonPointerSegment, encodeJsonPointerSegment, hoistRecursiveRefToDef, mapJsonSchemaRefs, resolveJsonSchemaRootLocalRef } from "./ref-utils.js";
|
|
3
|
+
import { ensureJsonSchemaObject, isJsonArraySchema, isUnconstrainedSchema } from "./utils.js";
|
|
4
|
+
import { get, isDeepEqual, omit, toArray } from "@orpc/shared";
|
|
5
|
+
// src/composition-utils.ts
|
|
1
6
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { get, isDeepEqual, omit, toArray } from '@orpc/shared';
|
|
6
|
-
import { JSON_SCHEMA_LOGIC_KEYWORDS } from './constants.js';
|
|
7
|
-
import { decodeJsonPointerSegment, encodeJsonPointerSegment, hoistRecursiveRefToDef, mapJsonSchemaRefs, resolveJsonSchemaRootLocalRef, } from './ref-utils.js';
|
|
8
|
-
import { ensureJsonSchemaObject, isJsonArraySchema, isUnconstrainedSchema } from './utils.js';
|
|
7
|
+
* These utilities assume the schema has only one root-level `$defs` object
|
|
8
|
+
* and exclusively use absolute JSON pointers for `$ref` values.
|
|
9
|
+
*/
|
|
9
10
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
* Moves a schema's `$defs` into the shared root map, renaming only the names whose bodies
|
|
12
|
+
* conflict with an already promoted def, and returns the schema body rebased onto `basePath`.
|
|
13
|
+
*
|
|
14
|
+
* Refs inside the promoted def bodies are rewritten too: they share the schema's namespace, so
|
|
15
|
+
* a rename left unapplied there would silently bind the body to another schema's defs.
|
|
16
|
+
*/
|
|
16
17
|
function promoteJsonSchemaDefs(schema, mergedDefs, basePath) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
reserved.add(`${name}${counter}`);
|
|
64
|
-
renameMap.set(name, `${name}${counter}`);
|
|
65
|
-
changed = true;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
for (const [name, body] of promoted) {
|
|
69
|
-
mergedDefs.set(renameMap.get(name) ?? name, body);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return mapJsonSchemaRefs(rootBody, mapRef);
|
|
18
|
+
const { $defs, ...rootBody } = schema;
|
|
19
|
+
const renameMap = /* @__PURE__ */ new Map();
|
|
20
|
+
/**
|
|
21
|
+
* `basePath` is where `rootBody` lands in the combined document, so pointers resolved against
|
|
22
|
+
* the original root are rebased onto it, while `#/$defs/` pointers follow the promoted defs.
|
|
23
|
+
*/
|
|
24
|
+
const mapRef = (ref) => {
|
|
25
|
+
if (ref === "#") return basePath;
|
|
26
|
+
if (ref.startsWith("#/$defs/")) {
|
|
27
|
+
const [encodedName, ...segments] = ref.slice(8).split("/");
|
|
28
|
+
const newName = renameMap.get(decodeJsonPointerSegment(encodedName));
|
|
29
|
+
return newName === void 0 ? ref : [
|
|
30
|
+
"#/$defs",
|
|
31
|
+
encodeJsonPointerSegment(newName),
|
|
32
|
+
...segments
|
|
33
|
+
].join("/");
|
|
34
|
+
}
|
|
35
|
+
if (ref.startsWith("#/") && get(rootBody, ref.slice(2).split("/").map(decodeJsonPointerSegment)) !== void 0) return `${basePath}/${ref.slice(2)}`;
|
|
36
|
+
return ref;
|
|
37
|
+
};
|
|
38
|
+
const defs = Object.entries($defs ?? {}).filter(([, def]) => def !== void 0);
|
|
39
|
+
if (defs.length > 0) {
|
|
40
|
+
const promoted = /* @__PURE__ */ new Map();
|
|
41
|
+
const reserved = /* @__PURE__ */ new Set([...mergedDefs.keys(), ...defs.map(([name]) => name)]);
|
|
42
|
+
/**
|
|
43
|
+
* A def body must be rewritten into the merged namespace before it can be compared for
|
|
44
|
+
* deduplication, and each rename invalidates every body pointing at the renamed def, so
|
|
45
|
+
* keep rewriting until a full pass renames nothing.
|
|
46
|
+
*/
|
|
47
|
+
for (let changed = true; changed;) {
|
|
48
|
+
changed = false;
|
|
49
|
+
for (const [name, def] of defs) {
|
|
50
|
+
const body = mapJsonSchemaRefs(def, mapRef);
|
|
51
|
+
promoted.set(name, body);
|
|
52
|
+
if (renameMap.has(name) || !mergedDefs.has(name) || isDeepEqual(mergedDefs.get(name), body)) continue;
|
|
53
|
+
let counter = 2;
|
|
54
|
+
while (reserved.has(`${name}${counter}`)) counter++;
|
|
55
|
+
reserved.add(`${name}${counter}`);
|
|
56
|
+
renameMap.set(name, `${name}${counter}`);
|
|
57
|
+
changed = true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const [name, body] of promoted) mergedDefs.set(renameMap.get(name) ?? name, body);
|
|
61
|
+
}
|
|
62
|
+
return mapJsonSchemaRefs(rootBody, mapRef);
|
|
73
63
|
}
|
|
74
64
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
const result = { [keyword]: compositionBranches };
|
|
90
|
-
if (mergedDefs.size > 0) {
|
|
91
|
-
result.$defs = Object.fromEntries(mergedDefs);
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
65
|
+
* Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
|
|
66
|
+
*/
|
|
67
|
+
function combineJsonSchemasWithComposition(keyword, schemas) {
|
|
68
|
+
if (schemas.length <= 1) return schemas[0] ?? true;
|
|
69
|
+
const mergedDefs = /* @__PURE__ */ new Map();
|
|
70
|
+
const compositionBranches = [];
|
|
71
|
+
for (let i = 0; i < schemas.length; i++) {
|
|
72
|
+
const schema = schemas[i];
|
|
73
|
+
compositionBranches.push(typeof schema === "boolean" ? schema : promoteJsonSchemaDefs(schema, mergedDefs, `#/${keyword}/${i}`));
|
|
74
|
+
}
|
|
75
|
+
const result = { [keyword]: compositionBranches };
|
|
76
|
+
if (mergedDefs.size > 0) result.$defs = Object.fromEntries(mergedDefs);
|
|
77
|
+
return result;
|
|
94
78
|
}
|
|
95
79
|
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (required.length > 0) {
|
|
116
|
-
schema.required = required;
|
|
117
|
-
}
|
|
118
|
-
if (additionalProperties !== undefined) {
|
|
119
|
-
schema.additionalProperties =
|
|
120
|
-
typeof additionalProperties === 'boolean'
|
|
121
|
-
? additionalProperties
|
|
122
|
-
: promoteJsonSchemaDefs(additionalProperties, mergedDefs, '#/additionalProperties');
|
|
123
|
-
}
|
|
124
|
-
if (mergedDefs.size > 0) {
|
|
125
|
-
schema.$defs = Object.fromEntries(mergedDefs);
|
|
126
|
-
}
|
|
127
|
-
return schema;
|
|
80
|
+
* Combines object property entries back into a single object schema.
|
|
81
|
+
* When given, `additionalProperties` constrains every property the entries do not name.
|
|
82
|
+
*/
|
|
83
|
+
function combineJsonObjectSchemaEntries(entries, additionalProperties) {
|
|
84
|
+
const properties = /* @__PURE__ */ new Map();
|
|
85
|
+
const required = [];
|
|
86
|
+
const mergedDefs = /* @__PURE__ */ new Map();
|
|
87
|
+
for (const [name, propertySchema, optional] of entries) {
|
|
88
|
+
if (!optional) required.push(name);
|
|
89
|
+
properties.set(name, typeof propertySchema === "boolean" ? propertySchema : promoteJsonSchemaDefs(propertySchema, mergedDefs, `#/properties/${encodeJsonPointerSegment(name)}`));
|
|
90
|
+
}
|
|
91
|
+
const schema = {
|
|
92
|
+
type: "object",
|
|
93
|
+
properties: Object.fromEntries(properties)
|
|
94
|
+
};
|
|
95
|
+
if (required.length > 0) schema.required = required;
|
|
96
|
+
if (additionalProperties !== void 0) schema.additionalProperties = typeof additionalProperties === "boolean" ? additionalProperties : promoteJsonSchemaDefs(additionalProperties, mergedDefs, "#/additionalProperties");
|
|
97
|
+
if (mergedDefs.size > 0) schema.$defs = Object.fromEntries(mergedDefs);
|
|
98
|
+
return schema;
|
|
128
99
|
}
|
|
129
100
|
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
101
|
+
* Parses an object schema, or a composition of object schemas, into property entries.
|
|
102
|
+
*/
|
|
103
|
+
function extractJsonObjectSchemaEntries(schema) {
|
|
104
|
+
schema = hoistRecursiveRefToDef(schema);
|
|
105
|
+
if (typeof schema !== "object") return;
|
|
106
|
+
const result = extractJsonObjectSchemaEntriesInternal(omit(schema, ["$defs"]), schema.$defs, /* @__PURE__ */ new Set());
|
|
107
|
+
if (!result.objectLike) return;
|
|
108
|
+
return result.entries.map(([n, s, ...r]) => [
|
|
109
|
+
n,
|
|
110
|
+
withRootDefs(s, schema.$defs),
|
|
111
|
+
...r
|
|
112
|
+
]);
|
|
142
113
|
}
|
|
143
114
|
/**
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (typeof resolved !== 'object' || resolved.additionalProperties === undefined) {
|
|
155
|
-
return undefined;
|
|
156
|
-
}
|
|
157
|
-
return withRootDefs(resolved.additionalProperties, schema.$defs);
|
|
115
|
+
* Returns the `additionalProperties` an object schema declares at its root, following root-local refs,
|
|
116
|
+
* with the root `$defs` attached so it can be passed to {@link combineJsonObjectSchemaEntries}.
|
|
117
|
+
* Composition branches are not inspected, since their keyword only constrains that branch.
|
|
118
|
+
*/
|
|
119
|
+
function extractJsonObjectSchemaAdditionalProperties(schema) {
|
|
120
|
+
schema = hoistRecursiveRefToDef(schema);
|
|
121
|
+
if (typeof schema !== "object") return;
|
|
122
|
+
const resolved = resolveJsonSchemaRootLocalRef(schema);
|
|
123
|
+
if (typeof resolved !== "object" || resolved.additionalProperties === void 0) return;
|
|
124
|
+
return withRootDefs(resolved.additionalProperties, schema.$defs);
|
|
158
125
|
}
|
|
159
126
|
const JSON_SCHEMA_OBJECT_KEYWORDS = [
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
127
|
+
"properties",
|
|
128
|
+
"patternProperties",
|
|
129
|
+
"additionalProperties",
|
|
130
|
+
"required",
|
|
131
|
+
"propertyNames",
|
|
132
|
+
"minProperties",
|
|
133
|
+
"maxProperties"
|
|
167
134
|
];
|
|
168
135
|
/**
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
136
|
+
* Returns true when an object can stand for every value the schema describes: the schema constrains objects,
|
|
137
|
+
* by `type` or object keywords, composes only such schemas, or is unconstrained, since an object satisfies it.
|
|
138
|
+
* Unlike {@link extractJsonObjectSchemaEntries}, an unconstrained schema counts as object-like.
|
|
139
|
+
*/
|
|
140
|
+
function isJsonObjectLikeSchema(schema) {
|
|
141
|
+
return isJsonObjectLikeSchemaInternal(schema, typeof schema === "object" ? schema.$defs : void 0, /* @__PURE__ */ new Set());
|
|
175
142
|
}
|
|
176
143
|
function isJsonObjectLikeSchemaInternal(schema, $defs, followedRefs) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
for (const keyword of ['allOf', 'anyOf', 'oneOf']) {
|
|
195
|
-
const members = resolved[keyword];
|
|
196
|
-
if (Array.isArray(members) && members.length > 0) {
|
|
197
|
-
return members.every((member) => isJsonObjectLikeSchemaInternal(member, $defs, followedRefs));
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return isUnconstrainedSchema(resolved);
|
|
144
|
+
if (typeof schema === "object" && typeof schema.$ref === "string") {
|
|
145
|
+
if (followedRefs.has(schema.$ref)) return false;
|
|
146
|
+
followedRefs = new Set(followedRefs).add(schema.$ref);
|
|
147
|
+
}
|
|
148
|
+
const resolved = resolveJsonSchemaRootLocalRef(schema, $defs);
|
|
149
|
+
if (typeof resolved === "boolean") return true;
|
|
150
|
+
if (resolved.type !== void 0) return resolved.type === "object";
|
|
151
|
+
if (JSON_SCHEMA_OBJECT_KEYWORDS.some((keyword) => keyword in resolved)) return true;
|
|
152
|
+
for (const keyword of [
|
|
153
|
+
"allOf",
|
|
154
|
+
"anyOf",
|
|
155
|
+
"oneOf"
|
|
156
|
+
]) {
|
|
157
|
+
const members = resolved[keyword];
|
|
158
|
+
if (Array.isArray(members) && members.length > 0) return members.every((member) => isJsonObjectLikeSchemaInternal(member, $defs, followedRefs));
|
|
159
|
+
}
|
|
160
|
+
return isUnconstrainedSchema(resolved);
|
|
201
161
|
}
|
|
202
162
|
function extractJsonObjectSchemaEntriesInternal(schema, $defs, resolvingRefs) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return {
|
|
289
|
-
entries: Array.from(sourceEntries.entries()).map(([name, entries]) => {
|
|
290
|
-
const schemas = deduplicateJsonSchemas([entries.direct?.[1], entries.allOf?.[1], entries.anyOf?.[1], entries.oneOf?.[1]].filter((schema) => schema !== undefined));
|
|
291
|
-
return [
|
|
292
|
-
name,
|
|
293
|
-
schemas.length === 1 ? schemas[0] : { allOf: schemas },
|
|
294
|
-
!schema.required?.includes(name) &&
|
|
295
|
-
[entries.allOf, entries.anyOf, entries.oneOf].every((entry) => entry === undefined || entry[2]),
|
|
296
|
-
];
|
|
297
|
-
}),
|
|
298
|
-
objectLike,
|
|
299
|
-
};
|
|
163
|
+
if (typeof schema !== "object") return {
|
|
164
|
+
entries: [],
|
|
165
|
+
objectLike: false
|
|
166
|
+
};
|
|
167
|
+
if (typeof schema.$ref === "string") {
|
|
168
|
+
if (resolvingRefs.has(schema.$ref)) return {
|
|
169
|
+
entries: [],
|
|
170
|
+
objectLike: true
|
|
171
|
+
};
|
|
172
|
+
const resolved = resolveJsonSchemaRootLocalRef(schema, $defs);
|
|
173
|
+
if (resolved !== schema) return extractJsonObjectSchemaEntriesInternal(resolved, $defs, new Set(resolvingRefs).add(schema.$ref));
|
|
174
|
+
}
|
|
175
|
+
const sources = [];
|
|
176
|
+
if (schema.properties) sources.push({
|
|
177
|
+
entries: Object.entries(schema.properties).map(([name, propertySchema]) => {
|
|
178
|
+
return [
|
|
179
|
+
name,
|
|
180
|
+
propertySchema,
|
|
181
|
+
!schema.required?.includes(name)
|
|
182
|
+
];
|
|
183
|
+
}),
|
|
184
|
+
source: "direct"
|
|
185
|
+
});
|
|
186
|
+
let objectLike = schema.type === "object" || schema.properties !== void 0 || schema.required !== void 0 || schema.additionalProperties !== void 0;
|
|
187
|
+
for (const keyword of [
|
|
188
|
+
"anyOf",
|
|
189
|
+
"oneOf",
|
|
190
|
+
"allOf"
|
|
191
|
+
]) {
|
|
192
|
+
const branches = schema[keyword];
|
|
193
|
+
if (branches === void 0) continue;
|
|
194
|
+
const branchResults = branches.map((branch) => extractJsonObjectSchemaEntriesInternal(branch, $defs, resolvingRefs));
|
|
195
|
+
if (branchResults.some((result) => !result.objectLike)) return {
|
|
196
|
+
entries: [],
|
|
197
|
+
objectLike: false
|
|
198
|
+
};
|
|
199
|
+
const entriesByName = /* @__PURE__ */ new Map();
|
|
200
|
+
for (const result of branchResults) for (const entry of result.entries) {
|
|
201
|
+
const entries = entriesByName.get(entry[0]);
|
|
202
|
+
if (entries) entries.push(entry);
|
|
203
|
+
else entriesByName.set(entry[0], [entry]);
|
|
204
|
+
}
|
|
205
|
+
objectLike = true;
|
|
206
|
+
sources.push({
|
|
207
|
+
entries: Array.from(entriesByName.entries()).map(([name, entries]) => {
|
|
208
|
+
const schemas = deduplicateJsonSchemas(entries.map((entry) => entry[1]));
|
|
209
|
+
const required = keyword === "allOf" ? entries.some((entry) => !entry[2]) : branchResults.every((result) => {
|
|
210
|
+
const entry = result.entries.find((item) => item[0] === name);
|
|
211
|
+
return entry !== void 0 && !entry[2];
|
|
212
|
+
});
|
|
213
|
+
return [
|
|
214
|
+
name,
|
|
215
|
+
schemas.length === 1 ? schemas[0] : keyword === "allOf" ? { allOf: schemas } : { anyOf: schemas },
|
|
216
|
+
!required
|
|
217
|
+
];
|
|
218
|
+
}),
|
|
219
|
+
source: keyword
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const sourceEntries = /* @__PURE__ */ new Map();
|
|
223
|
+
for (const { entries, source } of sources) for (const entry of entries) {
|
|
224
|
+
const existing = sourceEntries.get(entry[0]);
|
|
225
|
+
if (existing) existing[source] = entry;
|
|
226
|
+
else sourceEntries.set(entry[0], { [source]: entry });
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
entries: Array.from(sourceEntries.entries()).map(([name, entries]) => {
|
|
230
|
+
const schemas = deduplicateJsonSchemas([
|
|
231
|
+
entries.direct?.[1],
|
|
232
|
+
entries.allOf?.[1],
|
|
233
|
+
entries.anyOf?.[1],
|
|
234
|
+
entries.oneOf?.[1]
|
|
235
|
+
].filter((schema) => schema !== void 0));
|
|
236
|
+
return [
|
|
237
|
+
name,
|
|
238
|
+
schemas.length === 1 ? schemas[0] : { allOf: schemas },
|
|
239
|
+
!schema.required?.includes(name) && [
|
|
240
|
+
entries.allOf,
|
|
241
|
+
entries.anyOf,
|
|
242
|
+
entries.oneOf
|
|
243
|
+
].every((entry) => entry === void 0 || entry[2])
|
|
244
|
+
];
|
|
245
|
+
}),
|
|
246
|
+
objectLike
|
|
247
|
+
};
|
|
300
248
|
}
|
|
301
249
|
/**
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
250
|
+
* Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
|
|
251
|
+
*/
|
|
252
|
+
function flattenJsonUnionSchema(schema) {
|
|
253
|
+
return deduplicateJsonSchemas(flattenJsonUnionSchemaInternal(schema, /* @__PURE__ */ new Set()));
|
|
306
254
|
}
|
|
307
255
|
function flattenJsonUnionSchemaInternal(schema, resolvingRefs) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
...Object.fromEntries(entries.filter(([key]) => s[key] === undefined)),
|
|
332
|
-
$defs: schema.$defs,
|
|
333
|
-
};
|
|
334
|
-
const conflicts = entries.filter(([key]) => s[key] !== undefined);
|
|
335
|
-
if (conflicts.length) {
|
|
336
|
-
mergedSchema.allOf = [...toArray(mergedSchema.allOf), Object.fromEntries(conflicts)];
|
|
337
|
-
}
|
|
338
|
-
return flattenJsonUnionSchemaInternal(mergedSchema, resolvingRefs);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
return [schema];
|
|
256
|
+
if (typeof schema !== "object") return [schema];
|
|
257
|
+
if (typeof schema.$ref === "string") {
|
|
258
|
+
if (resolvingRefs.has(schema.$ref)) return [];
|
|
259
|
+
const resolved = resolveJsonSchemaRootLocalRef(schema);
|
|
260
|
+
if (resolved !== schema) {
|
|
261
|
+
const result = flattenJsonUnionSchemaInternal(resolved, new Set(resolvingRefs).add(schema.$ref));
|
|
262
|
+
if (result.length > 1) return result;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const { anyOf: _anyOf, oneOf: _oneOf, ...rest } = schema;
|
|
266
|
+
const entries = Object.entries(rest).filter(([, val]) => val !== void 0);
|
|
267
|
+
for (const keyword of ["anyOf", "oneOf"]) if (schema[keyword]) return schema[keyword].flatMap((s) => {
|
|
268
|
+
s = ensureJsonSchemaObject(s);
|
|
269
|
+
const mergedSchema = {
|
|
270
|
+
...s,
|
|
271
|
+
...Object.fromEntries(entries.filter(([key]) => s[key] === void 0)),
|
|
272
|
+
$defs: schema.$defs
|
|
273
|
+
};
|
|
274
|
+
const conflicts = entries.filter(([key]) => s[key] !== void 0);
|
|
275
|
+
if (conflicts.length) mergedSchema.allOf = [...toArray(mergedSchema.allOf), Object.fromEntries(conflicts)];
|
|
276
|
+
return flattenJsonUnionSchemaInternal(mergedSchema, resolvingRefs);
|
|
277
|
+
});
|
|
278
|
+
return [schema];
|
|
343
279
|
}
|
|
344
280
|
/**
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
const logicItem1 = Object.fromEntries(Object.entries(ensureJsonSchemaObject(items1)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key)));
|
|
359
|
-
const logicItem2 = Object.fromEntries(Object.entries(ensureJsonSchemaObject(items2)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key)));
|
|
360
|
-
if (!isDeepEqual(logicItem1, logicItem2)) {
|
|
361
|
-
return undefined;
|
|
362
|
-
}
|
|
363
|
-
return [items2, arraySchema];
|
|
281
|
+
* Matches a union made of a single item schema and its array form.
|
|
282
|
+
*/
|
|
283
|
+
function matchArrayableJsonSchema(schema) {
|
|
284
|
+
const schemas = flattenJsonUnionSchema(schema);
|
|
285
|
+
if (schemas.length !== 2) return;
|
|
286
|
+
const arraySchema = schemas.find(isJsonArraySchema);
|
|
287
|
+
if (arraySchema === void 0) return;
|
|
288
|
+
const items1 = arraySchema.items ?? true;
|
|
289
|
+
const items2 = schemas.find((s) => s !== arraySchema);
|
|
290
|
+
const logicItem1 = Object.fromEntries(Object.entries(ensureJsonSchemaObject(items1)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key)));
|
|
291
|
+
const logicItem2 = Object.fromEntries(Object.entries(ensureJsonSchemaObject(items2)).filter(([key]) => JSON_SCHEMA_LOGIC_KEYWORDS.has(key)));
|
|
292
|
+
if (!isDeepEqual(logicItem1, logicItem2)) return;
|
|
293
|
+
return [items2, arraySchema];
|
|
364
294
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
return result;
|
|
295
|
+
function deduplicateJsonSchemas(schemas) {
|
|
296
|
+
const result = [];
|
|
297
|
+
for (const schema of schemas) {
|
|
298
|
+
if (result.some((i) => isDeepEqual(i, schema))) continue;
|
|
299
|
+
result.push(schema);
|
|
300
|
+
}
|
|
301
|
+
return result;
|
|
374
302
|
}
|
|
375
303
|
function withRootDefs(schema, $defs) {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
304
|
+
if (typeof schema === "boolean" || !$defs) return schema;
|
|
305
|
+
return {
|
|
306
|
+
...schema,
|
|
307
|
+
$defs
|
|
308
|
+
};
|
|
380
309
|
}
|
|
310
|
+
export { combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, deduplicateJsonSchemas, extractJsonObjectSchemaAdditionalProperties, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, isJsonObjectLikeSchema, matchArrayableJsonSchema };
|