@kubb/ast 5.20.1 → 5.20.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +193 -192
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +193 -192
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -2463,6 +2463,8 @@ declare function transform$1<T extends Node>(node: T, options: TransformOptions)
|
|
|
2463
2463
|
* ```
|
|
2464
2464
|
*/
|
|
2465
2465
|
declare function collect$1<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
2466
|
+
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
2467
|
+
declare function getEmbeddedJson({ content }: Pick<StringSchemaNode, 'content'>): SchemaNode | undefined;
|
|
2466
2468
|
/**
|
|
2467
2469
|
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
2468
2470
|
*
|
|
@@ -2518,8 +2520,6 @@ declare function inferDiscriminatorPropertyName$1(members: ReadonlyArray<SchemaN
|
|
|
2518
2520
|
declare namespace index_d_exports$10 {
|
|
2519
2521
|
export { applyOptionality, findSchema$1 as find, resolveSchemaProperties$1 as findProperties, getEmbeddedJson, getSchemaLiteralValues$1 as getLiterals, getNumberBounds, inferDiscriminatorPropertyName$1 as inferDiscriminator, isString, narrowSchema$1 as narrow, schemaTypes$1 as types };
|
|
2520
2522
|
}
|
|
2521
|
-
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
2522
|
-
declare function getEmbeddedJson({ content }: Pick<StringSchemaNode, 'content'>): SchemaNode | undefined;
|
|
2523
2523
|
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
2524
2524
|
declare function isString(node: SchemaNode): boolean;
|
|
2525
2525
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -2463,6 +2463,8 @@ declare function transform$1<T extends Node>(node: T, options: TransformOptions)
|
|
|
2463
2463
|
* ```
|
|
2464
2464
|
*/
|
|
2465
2465
|
declare function collect$1<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
2466
|
+
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
2467
|
+
declare function getEmbeddedJson({ content }: Pick<StringSchemaNode, 'content'>): SchemaNode | undefined;
|
|
2466
2468
|
/**
|
|
2467
2469
|
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
2468
2470
|
*
|
|
@@ -2518,8 +2520,6 @@ declare function inferDiscriminatorPropertyName$1(members: ReadonlyArray<SchemaN
|
|
|
2518
2520
|
declare namespace index_d_exports$10 {
|
|
2519
2521
|
export { applyOptionality, findSchema$1 as find, resolveSchemaProperties$1 as findProperties, getEmbeddedJson, getSchemaLiteralValues$1 as getLiterals, getNumberBounds, inferDiscriminatorPropertyName$1 as inferDiscriminator, isString, narrowSchema$1 as narrow, schemaTypes$1 as types };
|
|
2520
2522
|
}
|
|
2521
|
-
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
2522
|
-
declare function getEmbeddedJson({ content }: Pick<StringSchemaNode, 'content'>): SchemaNode | undefined;
|
|
2523
2523
|
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
2524
2524
|
declare function isString(node: SchemaNode): boolean;
|
|
2525
2525
|
/**
|
package/dist/index.js
CHANGED
|
@@ -194,47 +194,6 @@ const schemaTypes$1 = {
|
|
|
194
194
|
*/
|
|
195
195
|
never: "never"
|
|
196
196
|
};
|
|
197
|
-
// src/optionality.ts
|
|
198
|
-
/**
|
|
199
|
-
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
200
|
-
* non-required nullable field is nullish.
|
|
201
|
-
*/
|
|
202
|
-
function applyOptionality(schema, required) {
|
|
203
|
-
const nullable = schema.nullable ?? false;
|
|
204
|
-
return {
|
|
205
|
-
...schema,
|
|
206
|
-
optional: !required && !nullable ? true : void 0,
|
|
207
|
-
nullish: !required && nullable ? true : void 0
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
// src/guards.ts
|
|
211
|
-
/**
|
|
212
|
-
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```ts
|
|
216
|
-
* const schema = createSchema({ type: 'string' })
|
|
217
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
|
-
function narrowSchema$1(node, type) {
|
|
221
|
-
return node?.type === type ? node : null;
|
|
222
|
-
}
|
|
223
|
-
__name(narrowSchema$1, "narrowSchema");
|
|
224
|
-
/**
|
|
225
|
-
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
226
|
-
*
|
|
227
|
-
* @example
|
|
228
|
-
* ```ts
|
|
229
|
-
* if (ast.http.isOperation(node)) {
|
|
230
|
-
* console.log(node.method, node.path)
|
|
231
|
-
* }
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
function isHttpOperationNode$1(node) {
|
|
235
|
-
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
236
|
-
}
|
|
237
|
-
__name(isHttpOperationNode$1, "isHttpOperationNode");
|
|
238
197
|
// src/mediaType/index.ts
|
|
239
198
|
var mediaType_exports = /* @__PURE__ */ __exportAll({
|
|
240
199
|
getBase: () => getBase,
|
|
@@ -262,162 +221,12 @@ function isStream(mediaType) {
|
|
|
262
221
|
const baseType = getBase(mediaType);
|
|
263
222
|
return streamMediaTypes.has(baseType) || baseType.startsWith("application/") && baseType.endsWith("+json-seq");
|
|
264
223
|
}
|
|
265
|
-
// src/
|
|
266
|
-
/**
|
|
267
|
-
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
268
|
-
*
|
|
269
|
-
* References are followed through their parsed `schema`. Circular references terminate without
|
|
270
|
-
* revisiting a node. Unions are not traversed because a property on one branch does not describe
|
|
271
|
-
* every branch.
|
|
272
|
-
*
|
|
273
|
-
* @example Faker discriminator narrowing
|
|
274
|
-
* ```ts
|
|
275
|
-
* const values = resolveSchemaProperties({ node: member, propertyName: union.discriminatorPropertyName }).flatMap(({ schema }) =>
|
|
276
|
-
* getSchemaLiteralValues(schema),
|
|
277
|
-
* )
|
|
278
|
-
* const typeName = `Extract<Pet, { type: ${values.map(JSON.stringify).join(' | ')} }>`
|
|
279
|
-
* ```
|
|
280
|
-
*
|
|
281
|
-
* @example Zod discriminated unions
|
|
282
|
-
* ```ts
|
|
283
|
-
* const canDiscriminate = union.members?.every(
|
|
284
|
-
* (member) => resolveSchemaProperties({ node: member, propertyName: 'type' }).length > 0,
|
|
285
|
-
* )
|
|
286
|
-
* const expression = canDiscriminate ? `z.discriminatedUnion('type', members)` : `z.union(members)`
|
|
287
|
-
* ```
|
|
288
|
-
*
|
|
289
|
-
* @example TypeScript property lookup
|
|
290
|
-
* ```ts
|
|
291
|
-
* const properties = resolveSchemaProperties({ node: schema, propertyName: 'status' })
|
|
292
|
-
* const statusSchemas = properties.map((property) => print(property.schema))
|
|
293
|
-
* ```
|
|
294
|
-
*/
|
|
295
|
-
function resolveSchemaProperties$1({ node, propertyName }) {
|
|
296
|
-
const properties = [];
|
|
297
|
-
walkSchema(node, "intersection", (current) => {
|
|
298
|
-
if (current.type !== "object") return;
|
|
299
|
-
const property = current.properties.find((candidate) => candidate.name === propertyName);
|
|
300
|
-
if (property) properties.push(property);
|
|
301
|
-
});
|
|
302
|
-
return properties;
|
|
303
|
-
}
|
|
304
|
-
__name(resolveSchemaProperties$1, "resolveSchemaProperties");
|
|
305
|
-
function walkSchema(node, through, visit) {
|
|
306
|
-
const visited = /* @__PURE__ */ new WeakSet();
|
|
307
|
-
function walk(current) {
|
|
308
|
-
if (!current || visited.has(current)) return;
|
|
309
|
-
visited.add(current);
|
|
310
|
-
visit(current);
|
|
311
|
-
if (current.type === "ref") return walk(current.schema);
|
|
312
|
-
if (current.type === through) for (const member of current.members ?? []) walk(member);
|
|
313
|
-
}
|
|
314
|
-
walk(node);
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Reads the unique literal values represented by an enum, resolved reference, or union.
|
|
318
|
-
*
|
|
319
|
-
* Named enum values take precedence over simple enum values, matching how renderers consume
|
|
320
|
-
* `EnumSchemaNode`.
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* ```ts
|
|
324
|
-
* const values = getSchemaLiteralValues(enumNode)
|
|
325
|
-
* // ['cat', 'dog']
|
|
326
|
-
* ```
|
|
327
|
-
*/
|
|
328
|
-
function getSchemaLiteralValues$1(node) {
|
|
329
|
-
return memo(literalValues, node, () => {
|
|
330
|
-
const values = /* @__PURE__ */ new Set();
|
|
331
|
-
walkSchema(node, "union", (current) => {
|
|
332
|
-
if (current.type !== "enum") return;
|
|
333
|
-
for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
|
|
334
|
-
});
|
|
335
|
-
return Object.freeze([...values]);
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
__name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
|
|
339
|
-
const literalValues = memo.create({ weak: true });
|
|
340
|
-
function collectPropertyNames(node) {
|
|
341
|
-
const names = /* @__PURE__ */ new Set();
|
|
342
|
-
walkSchema(node, "intersection", (current) => {
|
|
343
|
-
if (current.type === "object") for (const property of current.properties) names.add(property.name);
|
|
344
|
-
});
|
|
345
|
-
return names;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Infers an implicit discriminator: a property every member exposes with a distinct single
|
|
349
|
-
* literal value, the same shape a declared OpenAPI `discriminator` already narrows on.
|
|
350
|
-
*/
|
|
351
|
-
function inferDiscriminatorPropertyName$1(members) {
|
|
352
|
-
if (members.length < 2) return void 0;
|
|
353
|
-
let candidates;
|
|
354
|
-
for (const member of members) {
|
|
355
|
-
const names = collectPropertyNames(member);
|
|
356
|
-
candidates = candidates ? new Set([...candidates].filter((name) => names.has(name))) : names;
|
|
357
|
-
}
|
|
358
|
-
for (const name of candidates ?? []) {
|
|
359
|
-
const values = members.map((member) => {
|
|
360
|
-
const literals = new Set(resolveSchemaProperties$1({
|
|
361
|
-
node: member,
|
|
362
|
-
propertyName: name
|
|
363
|
-
}).flatMap((property) => getSchemaLiteralValues$1(property.schema)));
|
|
364
|
-
return literals.size === 1 ? [...literals][0] : void 0;
|
|
365
|
-
});
|
|
366
|
-
if (values.every((value) => value !== void 0) && new Set(values).size === members.length) return name;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
__name(inferDiscriminatorPropertyName$1, "inferDiscriminatorPropertyName");
|
|
370
|
-
// src/schema/index.ts
|
|
371
|
-
var schema_exports = /* @__PURE__ */ __exportAll({
|
|
372
|
-
applyOptionality: () => applyOptionality,
|
|
373
|
-
find: () => findSchema$1,
|
|
374
|
-
findProperties: () => resolveSchemaProperties$1,
|
|
375
|
-
getEmbeddedJson: () => getEmbeddedJson,
|
|
376
|
-
getLiterals: () => getSchemaLiteralValues$1,
|
|
377
|
-
getNumberBounds: () => getNumberBounds,
|
|
378
|
-
inferDiscriminator: () => inferDiscriminatorPropertyName$1,
|
|
379
|
-
isString: () => isString,
|
|
380
|
-
narrow: () => narrowSchema$1,
|
|
381
|
-
types: () => schemaTypes$1
|
|
382
|
-
});
|
|
383
|
-
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
384
|
-
"string",
|
|
385
|
-
"uuid",
|
|
386
|
-
"email",
|
|
387
|
-
"url",
|
|
388
|
-
"datetime"
|
|
389
|
-
]);
|
|
224
|
+
// src/schema/embeddedJson.ts
|
|
390
225
|
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
391
226
|
function getEmbeddedJson({ content }) {
|
|
392
227
|
if (!content?.schema || content.encoding || !content.mediaType) return void 0;
|
|
393
228
|
return isJson(content.mediaType) ? content.schema : void 0;
|
|
394
229
|
}
|
|
395
|
-
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
396
|
-
function isString(node) {
|
|
397
|
-
if (plainStringTypes.has(node.type)) return true;
|
|
398
|
-
const temporal = narrowSchema$1(node, "date") ?? narrowSchema$1(node, "time");
|
|
399
|
-
return temporal ? temporal.representation !== "date" : false;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* The tightest inclusive bounds of a number schema. An exclusive bound moves to the next point of `step` (`1` for an
|
|
403
|
-
* integer), an integer bound rounds inward, and an integer bound past `Number.MAX_SAFE_INTEGER` is left out, since it
|
|
404
|
-
* prints as a rounded or invalid literal.
|
|
405
|
-
*/
|
|
406
|
-
function getNumberBounds({ min, max, exclusive, integer = false, step }) {
|
|
407
|
-
const grid = step ?? (integer ? 1 : void 0);
|
|
408
|
-
const above = (bound) => grid ? (Math.floor(bound / grid) + 1) * grid : bound;
|
|
409
|
-
const below = (bound) => grid ? (Math.ceil(bound / grid) - 1) * grid : bound;
|
|
410
|
-
const tightest = (pick, bounds) => {
|
|
411
|
-
const usable = bounds.filter((bound) => bound !== void 0 && (!integer || Number.isSafeInteger(bound)));
|
|
412
|
-
return usable.length ? pick(...usable) : void 0;
|
|
413
|
-
};
|
|
414
|
-
const lower = tightest(Math.max, [min !== void 0 && integer ? Math.ceil(min) : min, exclusive?.min === void 0 ? void 0 : above(exclusive.min)]);
|
|
415
|
-
const upper = tightest(Math.min, [max !== void 0 && integer ? Math.floor(max) : max, exclusive?.max === void 0 ? void 0 : below(exclusive.max)]);
|
|
416
|
-
return {
|
|
417
|
-
...lower !== void 0 ? { min: lower } : {},
|
|
418
|
-
...upper !== void 0 ? { max: upper } : {}
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
230
|
// src/visitor.ts
|
|
422
231
|
/**
|
|
423
232
|
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
@@ -1005,6 +814,34 @@ function containsCircular(node, { circularSchemas, excludeName }) {
|
|
|
1005
814
|
for (const name of collectSchemaRefs$1(node)) if (name !== excludeName && circularSchemas.has(name)) return true;
|
|
1006
815
|
return false;
|
|
1007
816
|
}
|
|
817
|
+
// src/guards.ts
|
|
818
|
+
/**
|
|
819
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* ```ts
|
|
823
|
+
* const schema = createSchema({ type: 'string' })
|
|
824
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
825
|
+
* ```
|
|
826
|
+
*/
|
|
827
|
+
function narrowSchema$1(node, type) {
|
|
828
|
+
return node?.type === type ? node : null;
|
|
829
|
+
}
|
|
830
|
+
__name(narrowSchema$1, "narrowSchema");
|
|
831
|
+
/**
|
|
832
|
+
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* ```ts
|
|
836
|
+
* if (ast.http.isOperation(node)) {
|
|
837
|
+
* console.log(node.method, node.path)
|
|
838
|
+
* }
|
|
839
|
+
* ```
|
|
840
|
+
*/
|
|
841
|
+
function isHttpOperationNode$1(node) {
|
|
842
|
+
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
843
|
+
}
|
|
844
|
+
__name(isHttpOperationNode$1, "isHttpOperationNode");
|
|
1008
845
|
// src/http/requestBody.ts
|
|
1009
846
|
var requestBody_exports = /* @__PURE__ */ __exportAll({ getSchema: () => getSchema });
|
|
1010
847
|
/** The schema of the first request body entry, else `undefined` when the operation takes no body. */
|
|
@@ -1159,6 +996,170 @@ function mergeSchema(node) {
|
|
|
1159
996
|
function getName(input) {
|
|
1160
997
|
return typeof input === "string" ? refName$1(input) : resolveRefName$1(input);
|
|
1161
998
|
}
|
|
999
|
+
// src/optionality.ts
|
|
1000
|
+
/**
|
|
1001
|
+
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
1002
|
+
* non-required nullable field is nullish.
|
|
1003
|
+
*/
|
|
1004
|
+
function applyOptionality(schema, required) {
|
|
1005
|
+
const nullable = schema.nullable ?? false;
|
|
1006
|
+
return {
|
|
1007
|
+
...schema,
|
|
1008
|
+
optional: !required && !nullable ? true : void 0,
|
|
1009
|
+
nullish: !required && nullable ? true : void 0
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
// src/utils/schemaProperties.ts
|
|
1013
|
+
/**
|
|
1014
|
+
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
1015
|
+
*
|
|
1016
|
+
* References are followed through their parsed `schema`. Circular references terminate without
|
|
1017
|
+
* revisiting a node. Unions are not traversed because a property on one branch does not describe
|
|
1018
|
+
* every branch.
|
|
1019
|
+
*
|
|
1020
|
+
* @example Faker discriminator narrowing
|
|
1021
|
+
* ```ts
|
|
1022
|
+
* const values = resolveSchemaProperties({ node: member, propertyName: union.discriminatorPropertyName }).flatMap(({ schema }) =>
|
|
1023
|
+
* getSchemaLiteralValues(schema),
|
|
1024
|
+
* )
|
|
1025
|
+
* const typeName = `Extract<Pet, { type: ${values.map(JSON.stringify).join(' | ')} }>`
|
|
1026
|
+
* ```
|
|
1027
|
+
*
|
|
1028
|
+
* @example Zod discriminated unions
|
|
1029
|
+
* ```ts
|
|
1030
|
+
* const canDiscriminate = union.members?.every(
|
|
1031
|
+
* (member) => resolveSchemaProperties({ node: member, propertyName: 'type' }).length > 0,
|
|
1032
|
+
* )
|
|
1033
|
+
* const expression = canDiscriminate ? `z.discriminatedUnion('type', members)` : `z.union(members)`
|
|
1034
|
+
* ```
|
|
1035
|
+
*
|
|
1036
|
+
* @example TypeScript property lookup
|
|
1037
|
+
* ```ts
|
|
1038
|
+
* const properties = resolveSchemaProperties({ node: schema, propertyName: 'status' })
|
|
1039
|
+
* const statusSchemas = properties.map((property) => print(property.schema))
|
|
1040
|
+
* ```
|
|
1041
|
+
*/
|
|
1042
|
+
function resolveSchemaProperties$1({ node, propertyName }) {
|
|
1043
|
+
const properties = [];
|
|
1044
|
+
walkSchema(node, "intersection", (current) => {
|
|
1045
|
+
if (current.type !== "object") return;
|
|
1046
|
+
const property = current.properties.find((candidate) => candidate.name === propertyName);
|
|
1047
|
+
if (property) properties.push(property);
|
|
1048
|
+
});
|
|
1049
|
+
return properties;
|
|
1050
|
+
}
|
|
1051
|
+
__name(resolveSchemaProperties$1, "resolveSchemaProperties");
|
|
1052
|
+
function walkSchema(node, through, visit) {
|
|
1053
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
1054
|
+
function walk(current) {
|
|
1055
|
+
if (!current || visited.has(current)) return;
|
|
1056
|
+
visited.add(current);
|
|
1057
|
+
visit(current);
|
|
1058
|
+
if (current.type === "ref") return walk(current.schema);
|
|
1059
|
+
if (current.type === through) for (const member of current.members ?? []) walk(member);
|
|
1060
|
+
}
|
|
1061
|
+
walk(node);
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Reads the unique literal values represented by an enum, resolved reference, or union.
|
|
1065
|
+
*
|
|
1066
|
+
* Named enum values take precedence over simple enum values, matching how renderers consume
|
|
1067
|
+
* `EnumSchemaNode`.
|
|
1068
|
+
*
|
|
1069
|
+
* @example
|
|
1070
|
+
* ```ts
|
|
1071
|
+
* const values = getSchemaLiteralValues(enumNode)
|
|
1072
|
+
* // ['cat', 'dog']
|
|
1073
|
+
* ```
|
|
1074
|
+
*/
|
|
1075
|
+
function getSchemaLiteralValues$1(node) {
|
|
1076
|
+
return memo(literalValues, node, () => {
|
|
1077
|
+
const values = /* @__PURE__ */ new Set();
|
|
1078
|
+
walkSchema(node, "union", (current) => {
|
|
1079
|
+
if (current.type !== "enum") return;
|
|
1080
|
+
for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
|
|
1081
|
+
});
|
|
1082
|
+
return Object.freeze([...values]);
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
__name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
|
|
1086
|
+
const literalValues = memo.create({ weak: true });
|
|
1087
|
+
function collectPropertyNames(node) {
|
|
1088
|
+
const names = /* @__PURE__ */ new Set();
|
|
1089
|
+
walkSchema(node, "intersection", (current) => {
|
|
1090
|
+
if (current.type === "object") for (const property of current.properties) names.add(property.name);
|
|
1091
|
+
});
|
|
1092
|
+
return names;
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Infers an implicit discriminator: a property every member exposes with a distinct single
|
|
1096
|
+
* literal value, the same shape a declared OpenAPI `discriminator` already narrows on.
|
|
1097
|
+
*/
|
|
1098
|
+
function inferDiscriminatorPropertyName$1(members) {
|
|
1099
|
+
if (members.length < 2) return void 0;
|
|
1100
|
+
let candidates;
|
|
1101
|
+
for (const member of members) {
|
|
1102
|
+
const names = collectPropertyNames(member);
|
|
1103
|
+
candidates = candidates ? new Set([...candidates].filter((name) => names.has(name))) : names;
|
|
1104
|
+
}
|
|
1105
|
+
for (const name of candidates ?? []) {
|
|
1106
|
+
const values = members.map((member) => {
|
|
1107
|
+
const literals = new Set(resolveSchemaProperties$1({
|
|
1108
|
+
node: member,
|
|
1109
|
+
propertyName: name
|
|
1110
|
+
}).flatMap((property) => getSchemaLiteralValues$1(property.schema)));
|
|
1111
|
+
return literals.size === 1 ? [...literals][0] : void 0;
|
|
1112
|
+
});
|
|
1113
|
+
if (values.every((value) => value !== void 0) && new Set(values).size === members.length) return name;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
__name(inferDiscriminatorPropertyName$1, "inferDiscriminatorPropertyName");
|
|
1117
|
+
// src/schema/index.ts
|
|
1118
|
+
var schema_exports = /* @__PURE__ */ __exportAll({
|
|
1119
|
+
applyOptionality: () => applyOptionality,
|
|
1120
|
+
find: () => findSchema$1,
|
|
1121
|
+
findProperties: () => resolveSchemaProperties$1,
|
|
1122
|
+
getEmbeddedJson: () => getEmbeddedJson,
|
|
1123
|
+
getLiterals: () => getSchemaLiteralValues$1,
|
|
1124
|
+
getNumberBounds: () => getNumberBounds,
|
|
1125
|
+
inferDiscriminator: () => inferDiscriminatorPropertyName$1,
|
|
1126
|
+
isString: () => isString,
|
|
1127
|
+
narrow: () => narrowSchema$1,
|
|
1128
|
+
types: () => schemaTypes$1
|
|
1129
|
+
});
|
|
1130
|
+
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
1131
|
+
"string",
|
|
1132
|
+
"uuid",
|
|
1133
|
+
"email",
|
|
1134
|
+
"url",
|
|
1135
|
+
"datetime"
|
|
1136
|
+
]);
|
|
1137
|
+
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
1138
|
+
function isString(node) {
|
|
1139
|
+
if (plainStringTypes.has(node.type)) return true;
|
|
1140
|
+
const temporal = narrowSchema$1(node, "date") ?? narrowSchema$1(node, "time");
|
|
1141
|
+
return temporal ? temporal.representation !== "date" : false;
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* The tightest inclusive bounds of a number schema. An exclusive bound moves to the next point of `step` (`1` for an
|
|
1145
|
+
* integer), an integer bound rounds inward, and an integer bound past `Number.MAX_SAFE_INTEGER` is left out, since it
|
|
1146
|
+
* prints as a rounded or invalid literal.
|
|
1147
|
+
*/
|
|
1148
|
+
function getNumberBounds({ min, max, exclusive, integer = false, step }) {
|
|
1149
|
+
const grid = step ?? (integer ? 1 : void 0);
|
|
1150
|
+
const above = (bound) => grid ? (Math.floor(bound / grid) + 1) * grid : bound;
|
|
1151
|
+
const below = (bound) => grid ? (Math.ceil(bound / grid) - 1) * grid : bound;
|
|
1152
|
+
const tightest = (pick, bounds) => {
|
|
1153
|
+
const usable = bounds.filter((bound) => bound !== void 0 && (!integer || Number.isSafeInteger(bound)));
|
|
1154
|
+
return usable.length ? pick(...usable) : void 0;
|
|
1155
|
+
};
|
|
1156
|
+
const lower = tightest(Math.max, [min !== void 0 && integer ? Math.ceil(min) : min, exclusive?.min === void 0 ? void 0 : above(exclusive.min)]);
|
|
1157
|
+
const upper = tightest(Math.min, [max !== void 0 && integer ? Math.floor(max) : max, exclusive?.max === void 0 ? void 0 : below(exclusive.max)]);
|
|
1158
|
+
return {
|
|
1159
|
+
...lower !== void 0 ? { min: lower } : {},
|
|
1160
|
+
...upper !== void 0 ? { max: upper } : {}
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1162
1163
|
// src/createPrinter.ts
|
|
1163
1164
|
/**
|
|
1164
1165
|
* Creates a schema printer: a function that takes a `SchemaNode` and emits
|