@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.
@@ -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
- * These utilities assume the schema has only one root-level `$defs` object
3
- * and exclusively use absolute JSON pointers for `$ref` values.
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
- * Moves a schema's `$defs` into the shared root map, renaming only the names whose bodies
11
- * conflict with an already promoted def, and returns the schema body rebased onto `basePath`.
12
- *
13
- * Refs inside the promoted def bodies are rewritten too: they share the schema's namespace, so
14
- * a rename left unapplied there would silently bind the body to another schema's defs.
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
- const { $defs, ...rootBody } = schema;
18
- const renameMap = new Map();
19
- /**
20
- * `basePath` is where `rootBody` lands in the combined document, so pointers resolved against
21
- * the original root are rebased onto it, while `#/$defs/` pointers follow the promoted defs.
22
- */
23
- const mapRef = (ref) => {
24
- if (ref === '#') {
25
- return basePath;
26
- }
27
- if (ref.startsWith('#/$defs/')) {
28
- const [encodedName, ...segments] = ref.slice('#/$defs/'.length).split('/');
29
- const newName = renameMap.get(decodeJsonPointerSegment(encodedName));
30
- return newName === undefined
31
- ? ref
32
- : ['#/$defs', encodeJsonPointerSegment(newName), ...segments].join('/');
33
- }
34
- if (ref.startsWith('#/') &&
35
- get(rootBody, ref.slice(2).split('/').map(decodeJsonPointerSegment)) !== undefined) {
36
- return `${basePath}/${ref.slice(2)}`;
37
- }
38
- return ref;
39
- };
40
- const defs = Object.entries($defs ?? {}).filter(([, def]) => def !== undefined);
41
- if (defs.length > 0) {
42
- const promoted = new Map();
43
- const reserved = new Set([...mergedDefs.keys(), ...defs.map(([name]) => name)]);
44
- /**
45
- * A def body must be rewritten into the merged namespace before it can be compared for
46
- * deduplication, and each rename invalidates every body pointing at the renamed def, so
47
- * keep rewriting until a full pass renames nothing.
48
- */
49
- for (let changed = true; changed;) {
50
- changed = false;
51
- for (const [name, def] of defs) {
52
- const body = mapJsonSchemaRefs(def, mapRef);
53
- promoted.set(name, body);
54
- if (renameMap.has(name) ||
55
- !mergedDefs.has(name) ||
56
- isDeepEqual(mergedDefs.get(name), body)) {
57
- continue;
58
- }
59
- let counter = 2;
60
- while (reserved.has(`${name}${counter}`)) {
61
- counter++;
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
- * Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
76
- */
77
- export function combineJsonSchemasWithComposition(keyword, schemas) {
78
- if (schemas.length <= 1) {
79
- return schemas[0] ?? true;
80
- }
81
- const mergedDefs = new Map();
82
- const compositionBranches = [];
83
- for (let i = 0; i < schemas.length; i++) {
84
- const schema = schemas[i];
85
- compositionBranches.push(typeof schema === 'boolean'
86
- ? schema
87
- : promoteJsonSchemaDefs(schema, mergedDefs, `#/${keyword}/${i}`));
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
- * Combines object property entries back into a single object schema.
97
- * When given, `additionalProperties` constrains every property the entries do not name.
98
- */
99
- export function combineJsonObjectSchemaEntries(entries, additionalProperties) {
100
- const properties = new Map();
101
- const required = [];
102
- const mergedDefs = new Map();
103
- for (const [name, propertySchema, optional] of entries) {
104
- if (!optional) {
105
- required.push(name);
106
- }
107
- properties.set(name, typeof propertySchema === 'boolean'
108
- ? propertySchema
109
- : promoteJsonSchemaDefs(propertySchema, mergedDefs, `#/properties/${encodeJsonPointerSegment(name)}`));
110
- }
111
- const schema = {
112
- type: 'object',
113
- properties: Object.fromEntries(properties),
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
- * Parses an object schema, or a composition of object schemas, into property entries.
131
- */
132
- export function extractJsonObjectSchemaEntries(schema) {
133
- schema = hoistRecursiveRefToDef(schema);
134
- if (typeof schema !== 'object') {
135
- return undefined;
136
- }
137
- const result = extractJsonObjectSchemaEntriesInternal(omit(schema, ['$defs']), schema.$defs, new Set());
138
- if (!result.objectLike) {
139
- return undefined;
140
- }
141
- return result.entries.map(([n, s, ...r]) => [n, withRootDefs(s, schema.$defs), ...r]);
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
- * Returns the `additionalProperties` an object schema declares at its root, following root-local refs,
145
- * with the root `$defs` attached so it can be passed to {@link combineJsonObjectSchemaEntries}.
146
- * Composition branches are not inspected, since their keyword only constrains that branch.
147
- */
148
- export function extractJsonObjectSchemaAdditionalProperties(schema) {
149
- schema = hoistRecursiveRefToDef(schema);
150
- if (typeof schema !== 'object') {
151
- return undefined;
152
- }
153
- const resolved = resolveJsonSchemaRootLocalRef(schema);
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
- 'properties',
161
- 'patternProperties',
162
- 'additionalProperties',
163
- 'required',
164
- 'propertyNames',
165
- 'minProperties',
166
- 'maxProperties',
127
+ "properties",
128
+ "patternProperties",
129
+ "additionalProperties",
130
+ "required",
131
+ "propertyNames",
132
+ "minProperties",
133
+ "maxProperties"
167
134
  ];
168
135
  /**
169
- * Returns true when an object can stand for every value the schema describes: the schema constrains objects,
170
- * by `type` or object keywords, composes only such schemas, or is unconstrained, since an object satisfies it.
171
- * Unlike {@link extractJsonObjectSchemaEntries}, an unconstrained schema counts as object-like.
172
- */
173
- export function isJsonObjectLikeSchema(schema) {
174
- return isJsonObjectLikeSchemaInternal(schema, typeof schema === 'object' ? schema.$defs : undefined, new Set());
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
- if (typeof schema === 'object' && typeof schema.$ref === 'string') {
178
- // a cycle never proves the schema describes an object
179
- if (followedRefs.has(schema.$ref)) {
180
- return false;
181
- }
182
- followedRefs = new Set(followedRefs).add(schema.$ref);
183
- }
184
- const resolved = resolveJsonSchemaRootLocalRef(schema, $defs);
185
- if (typeof resolved === 'boolean') {
186
- return true;
187
- }
188
- if (resolved.type !== undefined) {
189
- return resolved.type === 'object';
190
- }
191
- if (JSON_SCHEMA_OBJECT_KEYWORDS.some((keyword) => keyword in resolved)) {
192
- return true;
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
- if (typeof schema !== 'object') {
204
- return { entries: [], objectLike: false };
205
- }
206
- if (typeof schema.$ref === 'string') {
207
- if (resolvingRefs.has(schema.$ref)) {
208
- return { entries: [], objectLike: true };
209
- }
210
- const resolved = resolveJsonSchemaRootLocalRef(schema, $defs);
211
- if (resolved !== schema) {
212
- return extractJsonObjectSchemaEntriesInternal(resolved, $defs, new Set(resolvingRefs).add(schema.$ref));
213
- }
214
- }
215
- const sources = [];
216
- if (schema.properties) {
217
- sources.push({
218
- entries: Object.entries(schema.properties).map(([name, propertySchema]) => {
219
- return [
220
- name,
221
- propertySchema,
222
- !schema.required?.includes(name),
223
- ];
224
- }),
225
- source: 'direct',
226
- });
227
- }
228
- let objectLike = schema.type === 'object' ||
229
- schema.properties !== undefined ||
230
- schema.required !== undefined ||
231
- schema.additionalProperties !== undefined;
232
- for (const keyword of ['anyOf', 'oneOf', 'allOf']) {
233
- const branches = schema[keyword];
234
- if (branches === undefined) {
235
- continue;
236
- }
237
- const branchResults = branches.map((branch) => extractJsonObjectSchemaEntriesInternal(branch, $defs, resolvingRefs));
238
- if (branchResults.some((result) => !result.objectLike)) {
239
- return { entries: [], objectLike: false };
240
- }
241
- const entriesByName = new Map();
242
- for (const result of branchResults) {
243
- for (const entry of result.entries) {
244
- const entries = entriesByName.get(entry[0]);
245
- if (entries) {
246
- entries.push(entry);
247
- }
248
- else {
249
- entriesByName.set(entry[0], [entry]);
250
- }
251
- }
252
- }
253
- objectLike = true;
254
- sources.push({
255
- entries: Array.from(entriesByName.entries()).map(([name, entries]) => {
256
- const schemas = deduplicateJsonSchemas(entries.map((entry) => entry[1]));
257
- const required = keyword === 'allOf'
258
- ? entries.some((entry) => !entry[2])
259
- : branchResults.every((result) => {
260
- const entry = result.entries.find((item) => item[0] === name);
261
- return entry !== undefined && !entry[2];
262
- });
263
- return [
264
- name,
265
- schemas.length === 1
266
- ? schemas[0]
267
- : keyword === 'allOf'
268
- ? { allOf: schemas }
269
- : { anyOf: schemas },
270
- !required,
271
- ];
272
- }),
273
- source: keyword,
274
- });
275
- }
276
- const sourceEntries = new Map();
277
- for (const { entries, source } of sources) {
278
- for (const entry of entries) {
279
- const existing = sourceEntries.get(entry[0]);
280
- if (existing) {
281
- existing[source] = entry;
282
- }
283
- else {
284
- sourceEntries.set(entry[0], { [source]: entry });
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
- * Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
303
- */
304
- export function flattenJsonUnionSchema(schema) {
305
- return deduplicateJsonSchemas(flattenJsonUnionSchemaInternal(schema, new Set()));
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
- if (typeof schema !== 'object') {
309
- return [schema];
310
- }
311
- if (typeof schema.$ref === 'string') {
312
- if (resolvingRefs.has(schema.$ref)) {
313
- return [];
314
- }
315
- const resolved = resolveJsonSchemaRootLocalRef(schema);
316
- if (resolved !== schema) {
317
- const result = flattenJsonUnionSchemaInternal(resolved, new Set(resolvingRefs).add(schema.$ref));
318
- if (result.length > 1) {
319
- return result;
320
- }
321
- }
322
- }
323
- const { anyOf: _anyOf, oneOf: _oneOf, ...rest } = schema;
324
- const entries = Object.entries(rest).filter(([, val]) => val !== undefined);
325
- for (const keyword of ['anyOf', 'oneOf']) {
326
- if (schema[keyword]) {
327
- return schema[keyword].flatMap((s) => {
328
- s = ensureJsonSchemaObject(s);
329
- const mergedSchema = {
330
- ...s,
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
- * Matches a union made of a single item schema and its array form.
346
- */
347
- export function matchArrayableJsonSchema(schema) {
348
- const schemas = flattenJsonUnionSchema(schema);
349
- if (schemas.length !== 2) {
350
- return undefined;
351
- }
352
- const arraySchema = schemas.find(isJsonArraySchema);
353
- if (arraySchema === undefined) {
354
- return undefined;
355
- }
356
- const items1 = arraySchema.items ?? true;
357
- const items2 = schemas.find((s) => s !== arraySchema);
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
- export function deduplicateJsonSchemas(schemas) {
366
- const result = [];
367
- for (const schema of schemas) {
368
- if (result.some((i) => isDeepEqual(i, schema))) {
369
- continue;
370
- }
371
- result.push(schema);
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
- if (typeof schema === 'boolean' || !$defs) {
377
- return schema;
378
- }
379
- return { ...schema, $defs };
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 };