@kubb/ast 5.20.1 → 5.20.3
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.cjs
CHANGED
|
@@ -215,47 +215,6 @@ const schemaTypes$1 = {
|
|
|
215
215
|
*/
|
|
216
216
|
never: "never"
|
|
217
217
|
};
|
|
218
|
-
// src/optionality.ts
|
|
219
|
-
/**
|
|
220
|
-
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
221
|
-
* non-required nullable field is nullish.
|
|
222
|
-
*/
|
|
223
|
-
function applyOptionality(schema, required) {
|
|
224
|
-
const nullable = schema.nullable ?? false;
|
|
225
|
-
return {
|
|
226
|
-
...schema,
|
|
227
|
-
optional: !required && !nullable ? true : void 0,
|
|
228
|
-
nullish: !required && nullable ? true : void 0
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
// src/guards.ts
|
|
232
|
-
/**
|
|
233
|
-
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* ```ts
|
|
237
|
-
* const schema = createSchema({ type: 'string' })
|
|
238
|
-
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
239
|
-
* ```
|
|
240
|
-
*/
|
|
241
|
-
function narrowSchema$1(node, type) {
|
|
242
|
-
return node?.type === type ? node : null;
|
|
243
|
-
}
|
|
244
|
-
__name(narrowSchema$1, "narrowSchema");
|
|
245
|
-
/**
|
|
246
|
-
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```ts
|
|
250
|
-
* if (ast.http.isOperation(node)) {
|
|
251
|
-
* console.log(node.method, node.path)
|
|
252
|
-
* }
|
|
253
|
-
* ```
|
|
254
|
-
*/
|
|
255
|
-
function isHttpOperationNode$1(node) {
|
|
256
|
-
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
257
|
-
}
|
|
258
|
-
__name(isHttpOperationNode$1, "isHttpOperationNode");
|
|
259
218
|
// src/mediaType/index.ts
|
|
260
219
|
var mediaType_exports = /* @__PURE__ */ __exportAll({
|
|
261
220
|
getBase: () => getBase,
|
|
@@ -283,162 +242,12 @@ function isStream(mediaType) {
|
|
|
283
242
|
const baseType = getBase(mediaType);
|
|
284
243
|
return streamMediaTypes.has(baseType) || baseType.startsWith("application/") && baseType.endsWith("+json-seq");
|
|
285
244
|
}
|
|
286
|
-
// src/
|
|
287
|
-
/**
|
|
288
|
-
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
289
|
-
*
|
|
290
|
-
* References are followed through their parsed `schema`. Circular references terminate without
|
|
291
|
-
* revisiting a node. Unions are not traversed because a property on one branch does not describe
|
|
292
|
-
* every branch.
|
|
293
|
-
*
|
|
294
|
-
* @example Faker discriminator narrowing
|
|
295
|
-
* ```ts
|
|
296
|
-
* const values = resolveSchemaProperties({ node: member, propertyName: union.discriminatorPropertyName }).flatMap(({ schema }) =>
|
|
297
|
-
* getSchemaLiteralValues(schema),
|
|
298
|
-
* )
|
|
299
|
-
* const typeName = `Extract<Pet, { type: ${values.map(JSON.stringify).join(' | ')} }>`
|
|
300
|
-
* ```
|
|
301
|
-
*
|
|
302
|
-
* @example Zod discriminated unions
|
|
303
|
-
* ```ts
|
|
304
|
-
* const canDiscriminate = union.members?.every(
|
|
305
|
-
* (member) => resolveSchemaProperties({ node: member, propertyName: 'type' }).length > 0,
|
|
306
|
-
* )
|
|
307
|
-
* const expression = canDiscriminate ? `z.discriminatedUnion('type', members)` : `z.union(members)`
|
|
308
|
-
* ```
|
|
309
|
-
*
|
|
310
|
-
* @example TypeScript property lookup
|
|
311
|
-
* ```ts
|
|
312
|
-
* const properties = resolveSchemaProperties({ node: schema, propertyName: 'status' })
|
|
313
|
-
* const statusSchemas = properties.map((property) => print(property.schema))
|
|
314
|
-
* ```
|
|
315
|
-
*/
|
|
316
|
-
function resolveSchemaProperties$1({ node, propertyName }) {
|
|
317
|
-
const properties = [];
|
|
318
|
-
walkSchema(node, "intersection", (current) => {
|
|
319
|
-
if (current.type !== "object") return;
|
|
320
|
-
const property = current.properties.find((candidate) => candidate.name === propertyName);
|
|
321
|
-
if (property) properties.push(property);
|
|
322
|
-
});
|
|
323
|
-
return properties;
|
|
324
|
-
}
|
|
325
|
-
__name(resolveSchemaProperties$1, "resolveSchemaProperties");
|
|
326
|
-
function walkSchema(node, through, visit) {
|
|
327
|
-
const visited = /* @__PURE__ */ new WeakSet();
|
|
328
|
-
function walk(current) {
|
|
329
|
-
if (!current || visited.has(current)) return;
|
|
330
|
-
visited.add(current);
|
|
331
|
-
visit(current);
|
|
332
|
-
if (current.type === "ref") return walk(current.schema);
|
|
333
|
-
if (current.type === through) for (const member of current.members ?? []) walk(member);
|
|
334
|
-
}
|
|
335
|
-
walk(node);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Reads the unique literal values represented by an enum, resolved reference, or union.
|
|
339
|
-
*
|
|
340
|
-
* Named enum values take precedence over simple enum values, matching how renderers consume
|
|
341
|
-
* `EnumSchemaNode`.
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```ts
|
|
345
|
-
* const values = getSchemaLiteralValues(enumNode)
|
|
346
|
-
* // ['cat', 'dog']
|
|
347
|
-
* ```
|
|
348
|
-
*/
|
|
349
|
-
function getSchemaLiteralValues$1(node) {
|
|
350
|
-
return memo(literalValues, node, () => {
|
|
351
|
-
const values = /* @__PURE__ */ new Set();
|
|
352
|
-
walkSchema(node, "union", (current) => {
|
|
353
|
-
if (current.type !== "enum") return;
|
|
354
|
-
for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
|
|
355
|
-
});
|
|
356
|
-
return Object.freeze([...values]);
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
__name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
|
|
360
|
-
const literalValues = memo.create({ weak: true });
|
|
361
|
-
function collectPropertyNames(node) {
|
|
362
|
-
const names = /* @__PURE__ */ new Set();
|
|
363
|
-
walkSchema(node, "intersection", (current) => {
|
|
364
|
-
if (current.type === "object") for (const property of current.properties) names.add(property.name);
|
|
365
|
-
});
|
|
366
|
-
return names;
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Infers an implicit discriminator: a property every member exposes with a distinct single
|
|
370
|
-
* literal value, the same shape a declared OpenAPI `discriminator` already narrows on.
|
|
371
|
-
*/
|
|
372
|
-
function inferDiscriminatorPropertyName$1(members) {
|
|
373
|
-
if (members.length < 2) return void 0;
|
|
374
|
-
let candidates;
|
|
375
|
-
for (const member of members) {
|
|
376
|
-
const names = collectPropertyNames(member);
|
|
377
|
-
candidates = candidates ? new Set([...candidates].filter((name) => names.has(name))) : names;
|
|
378
|
-
}
|
|
379
|
-
for (const name of candidates ?? []) {
|
|
380
|
-
const values = members.map((member) => {
|
|
381
|
-
const literals = new Set(resolveSchemaProperties$1({
|
|
382
|
-
node: member,
|
|
383
|
-
propertyName: name
|
|
384
|
-
}).flatMap((property) => getSchemaLiteralValues$1(property.schema)));
|
|
385
|
-
return literals.size === 1 ? [...literals][0] : void 0;
|
|
386
|
-
});
|
|
387
|
-
if (values.every((value) => value !== void 0) && new Set(values).size === members.length) return name;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
__name(inferDiscriminatorPropertyName$1, "inferDiscriminatorPropertyName");
|
|
391
|
-
// src/schema/index.ts
|
|
392
|
-
var schema_exports = /* @__PURE__ */ __exportAll({
|
|
393
|
-
applyOptionality: () => applyOptionality,
|
|
394
|
-
find: () => findSchema$1,
|
|
395
|
-
findProperties: () => resolveSchemaProperties$1,
|
|
396
|
-
getEmbeddedJson: () => getEmbeddedJson,
|
|
397
|
-
getLiterals: () => getSchemaLiteralValues$1,
|
|
398
|
-
getNumberBounds: () => getNumberBounds,
|
|
399
|
-
inferDiscriminator: () => inferDiscriminatorPropertyName$1,
|
|
400
|
-
isString: () => isString,
|
|
401
|
-
narrow: () => narrowSchema$1,
|
|
402
|
-
types: () => schemaTypes$1
|
|
403
|
-
});
|
|
404
|
-
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
405
|
-
"string",
|
|
406
|
-
"uuid",
|
|
407
|
-
"email",
|
|
408
|
-
"url",
|
|
409
|
-
"datetime"
|
|
410
|
-
]);
|
|
245
|
+
// src/schema/embeddedJson.ts
|
|
411
246
|
/** The schema of the JSON document a string holds (`contentMediaType` with `contentSchema`), else `undefined`. */
|
|
412
247
|
function getEmbeddedJson({ content }) {
|
|
413
248
|
if (!content?.schema || content.encoding || !content.mediaType) return void 0;
|
|
414
249
|
return isJson(content.mediaType) ? content.schema : void 0;
|
|
415
250
|
}
|
|
416
|
-
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
417
|
-
function isString(node) {
|
|
418
|
-
if (plainStringTypes.has(node.type)) return true;
|
|
419
|
-
const temporal = narrowSchema$1(node, "date") ?? narrowSchema$1(node, "time");
|
|
420
|
-
return temporal ? temporal.representation !== "date" : false;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* The tightest inclusive bounds of a number schema. An exclusive bound moves to the next point of `step` (`1` for an
|
|
424
|
-
* integer), an integer bound rounds inward, and an integer bound past `Number.MAX_SAFE_INTEGER` is left out, since it
|
|
425
|
-
* prints as a rounded or invalid literal.
|
|
426
|
-
*/
|
|
427
|
-
function getNumberBounds({ min, max, exclusive, integer = false, step }) {
|
|
428
|
-
const grid = step ?? (integer ? 1 : void 0);
|
|
429
|
-
const above = (bound) => grid ? (Math.floor(bound / grid) + 1) * grid : bound;
|
|
430
|
-
const below = (bound) => grid ? (Math.ceil(bound / grid) - 1) * grid : bound;
|
|
431
|
-
const tightest = (pick, bounds) => {
|
|
432
|
-
const usable = bounds.filter((bound) => bound !== void 0 && (!integer || Number.isSafeInteger(bound)));
|
|
433
|
-
return usable.length ? pick(...usable) : void 0;
|
|
434
|
-
};
|
|
435
|
-
const lower = tightest(Math.max, [min !== void 0 && integer ? Math.ceil(min) : min, exclusive?.min === void 0 ? void 0 : above(exclusive.min)]);
|
|
436
|
-
const upper = tightest(Math.min, [max !== void 0 && integer ? Math.floor(max) : max, exclusive?.max === void 0 ? void 0 : below(exclusive.max)]);
|
|
437
|
-
return {
|
|
438
|
-
...lower !== void 0 ? { min: lower } : {},
|
|
439
|
-
...upper !== void 0 ? { max: upper } : {}
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
251
|
// src/visitor.ts
|
|
443
252
|
/**
|
|
444
253
|
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
@@ -1026,6 +835,34 @@ function containsCircular(node, { circularSchemas, excludeName }) {
|
|
|
1026
835
|
for (const name of collectSchemaRefs$1(node)) if (name !== excludeName && circularSchemas.has(name)) return true;
|
|
1027
836
|
return false;
|
|
1028
837
|
}
|
|
838
|
+
// src/guards.ts
|
|
839
|
+
/**
|
|
840
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
841
|
+
*
|
|
842
|
+
* @example
|
|
843
|
+
* ```ts
|
|
844
|
+
* const schema = createSchema({ type: 'string' })
|
|
845
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
846
|
+
* ```
|
|
847
|
+
*/
|
|
848
|
+
function narrowSchema$1(node, type) {
|
|
849
|
+
return node?.type === type ? node : null;
|
|
850
|
+
}
|
|
851
|
+
__name(narrowSchema$1, "narrowSchema");
|
|
852
|
+
/**
|
|
853
|
+
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
854
|
+
*
|
|
855
|
+
* @example
|
|
856
|
+
* ```ts
|
|
857
|
+
* if (ast.http.isOperation(node)) {
|
|
858
|
+
* console.log(node.method, node.path)
|
|
859
|
+
* }
|
|
860
|
+
* ```
|
|
861
|
+
*/
|
|
862
|
+
function isHttpOperationNode$1(node) {
|
|
863
|
+
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
864
|
+
}
|
|
865
|
+
__name(isHttpOperationNode$1, "isHttpOperationNode");
|
|
1029
866
|
// src/http/requestBody.ts
|
|
1030
867
|
var requestBody_exports = /* @__PURE__ */ __exportAll({ getSchema: () => getSchema });
|
|
1031
868
|
/** The schema of the first request body entry, else `undefined` when the operation takes no body. */
|
|
@@ -1180,6 +1017,170 @@ function mergeSchema(node) {
|
|
|
1180
1017
|
function getName(input) {
|
|
1181
1018
|
return typeof input === "string" ? refName$1(input) : resolveRefName$1(input);
|
|
1182
1019
|
}
|
|
1020
|
+
// src/optionality.ts
|
|
1021
|
+
/**
|
|
1022
|
+
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
1023
|
+
* non-required nullable field is nullish.
|
|
1024
|
+
*/
|
|
1025
|
+
function applyOptionality(schema, required) {
|
|
1026
|
+
const nullable = schema.nullable ?? false;
|
|
1027
|
+
return {
|
|
1028
|
+
...schema,
|
|
1029
|
+
optional: !required && !nullable ? true : void 0,
|
|
1030
|
+
nullish: !required && nullable ? true : void 0
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
// src/utils/schemaProperties.ts
|
|
1034
|
+
/**
|
|
1035
|
+
* Finds every matching property exposed by an object, resolved reference, or intersection.
|
|
1036
|
+
*
|
|
1037
|
+
* References are followed through their parsed `schema`. Circular references terminate without
|
|
1038
|
+
* revisiting a node. Unions are not traversed because a property on one branch does not describe
|
|
1039
|
+
* every branch.
|
|
1040
|
+
*
|
|
1041
|
+
* @example Faker discriminator narrowing
|
|
1042
|
+
* ```ts
|
|
1043
|
+
* const values = resolveSchemaProperties({ node: member, propertyName: union.discriminatorPropertyName }).flatMap(({ schema }) =>
|
|
1044
|
+
* getSchemaLiteralValues(schema),
|
|
1045
|
+
* )
|
|
1046
|
+
* const typeName = `Extract<Pet, { type: ${values.map(JSON.stringify).join(' | ')} }>`
|
|
1047
|
+
* ```
|
|
1048
|
+
*
|
|
1049
|
+
* @example Zod discriminated unions
|
|
1050
|
+
* ```ts
|
|
1051
|
+
* const canDiscriminate = union.members?.every(
|
|
1052
|
+
* (member) => resolveSchemaProperties({ node: member, propertyName: 'type' }).length > 0,
|
|
1053
|
+
* )
|
|
1054
|
+
* const expression = canDiscriminate ? `z.discriminatedUnion('type', members)` : `z.union(members)`
|
|
1055
|
+
* ```
|
|
1056
|
+
*
|
|
1057
|
+
* @example TypeScript property lookup
|
|
1058
|
+
* ```ts
|
|
1059
|
+
* const properties = resolveSchemaProperties({ node: schema, propertyName: 'status' })
|
|
1060
|
+
* const statusSchemas = properties.map((property) => print(property.schema))
|
|
1061
|
+
* ```
|
|
1062
|
+
*/
|
|
1063
|
+
function resolveSchemaProperties$1({ node, propertyName }) {
|
|
1064
|
+
const properties = [];
|
|
1065
|
+
walkSchema(node, "intersection", (current) => {
|
|
1066
|
+
if (current.type !== "object") return;
|
|
1067
|
+
const property = current.properties.find((candidate) => candidate.name === propertyName);
|
|
1068
|
+
if (property) properties.push(property);
|
|
1069
|
+
});
|
|
1070
|
+
return properties;
|
|
1071
|
+
}
|
|
1072
|
+
__name(resolveSchemaProperties$1, "resolveSchemaProperties");
|
|
1073
|
+
function walkSchema(node, through, visit) {
|
|
1074
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
1075
|
+
function walk(current) {
|
|
1076
|
+
if (!current || visited.has(current)) return;
|
|
1077
|
+
visited.add(current);
|
|
1078
|
+
visit(current);
|
|
1079
|
+
if (current.type === "ref") return walk(current.schema);
|
|
1080
|
+
if (current.type === through) for (const member of current.members ?? []) walk(member);
|
|
1081
|
+
}
|
|
1082
|
+
walk(node);
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Reads the unique literal values represented by an enum, resolved reference, or union.
|
|
1086
|
+
*
|
|
1087
|
+
* Named enum values take precedence over simple enum values, matching how renderers consume
|
|
1088
|
+
* `EnumSchemaNode`.
|
|
1089
|
+
*
|
|
1090
|
+
* @example
|
|
1091
|
+
* ```ts
|
|
1092
|
+
* const values = getSchemaLiteralValues(enumNode)
|
|
1093
|
+
* // ['cat', 'dog']
|
|
1094
|
+
* ```
|
|
1095
|
+
*/
|
|
1096
|
+
function getSchemaLiteralValues$1(node) {
|
|
1097
|
+
return memo(literalValues, node, () => {
|
|
1098
|
+
const values = /* @__PURE__ */ new Set();
|
|
1099
|
+
walkSchema(node, "union", (current) => {
|
|
1100
|
+
if (current.type !== "enum") return;
|
|
1101
|
+
for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
|
|
1102
|
+
});
|
|
1103
|
+
return Object.freeze([...values]);
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
__name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
|
|
1107
|
+
const literalValues = memo.create({ weak: true });
|
|
1108
|
+
function collectPropertyNames(node) {
|
|
1109
|
+
const names = /* @__PURE__ */ new Set();
|
|
1110
|
+
walkSchema(node, "intersection", (current) => {
|
|
1111
|
+
if (current.type === "object") for (const property of current.properties) names.add(property.name);
|
|
1112
|
+
});
|
|
1113
|
+
return names;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Infers an implicit discriminator: a property every member exposes with a distinct single
|
|
1117
|
+
* literal value, the same shape a declared OpenAPI `discriminator` already narrows on.
|
|
1118
|
+
*/
|
|
1119
|
+
function inferDiscriminatorPropertyName$1(members) {
|
|
1120
|
+
if (members.length < 2) return void 0;
|
|
1121
|
+
let candidates;
|
|
1122
|
+
for (const member of members) {
|
|
1123
|
+
const names = collectPropertyNames(member);
|
|
1124
|
+
candidates = candidates ? new Set([...candidates].filter((name) => names.has(name))) : names;
|
|
1125
|
+
}
|
|
1126
|
+
for (const name of candidates ?? []) {
|
|
1127
|
+
const values = members.map((member) => {
|
|
1128
|
+
const literals = new Set(resolveSchemaProperties$1({
|
|
1129
|
+
node: member,
|
|
1130
|
+
propertyName: name
|
|
1131
|
+
}).flatMap((property) => getSchemaLiteralValues$1(property.schema)));
|
|
1132
|
+
return literals.size === 1 ? [...literals][0] : void 0;
|
|
1133
|
+
});
|
|
1134
|
+
if (values.every((value) => value !== void 0) && new Set(values).size === members.length) return name;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
__name(inferDiscriminatorPropertyName$1, "inferDiscriminatorPropertyName");
|
|
1138
|
+
// src/schema/index.ts
|
|
1139
|
+
var schema_exports = /* @__PURE__ */ __exportAll({
|
|
1140
|
+
applyOptionality: () => applyOptionality,
|
|
1141
|
+
find: () => findSchema$1,
|
|
1142
|
+
findProperties: () => resolveSchemaProperties$1,
|
|
1143
|
+
getEmbeddedJson: () => getEmbeddedJson,
|
|
1144
|
+
getLiterals: () => getSchemaLiteralValues$1,
|
|
1145
|
+
getNumberBounds: () => getNumberBounds,
|
|
1146
|
+
inferDiscriminator: () => inferDiscriminatorPropertyName$1,
|
|
1147
|
+
isString: () => isString,
|
|
1148
|
+
narrow: () => narrowSchema$1,
|
|
1149
|
+
types: () => schemaTypes$1
|
|
1150
|
+
});
|
|
1151
|
+
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
1152
|
+
"string",
|
|
1153
|
+
"uuid",
|
|
1154
|
+
"email",
|
|
1155
|
+
"url",
|
|
1156
|
+
"datetime"
|
|
1157
|
+
]);
|
|
1158
|
+
/** Whether a schema prints as a plain `string`: string-like types, and `date` or `time` with a `'string'` representation. */
|
|
1159
|
+
function isString(node) {
|
|
1160
|
+
if (plainStringTypes.has(node.type)) return true;
|
|
1161
|
+
const temporal = narrowSchema$1(node, "date") ?? narrowSchema$1(node, "time");
|
|
1162
|
+
return temporal ? temporal.representation !== "date" : false;
|
|
1163
|
+
}
|
|
1164
|
+
/**
|
|
1165
|
+
* The tightest inclusive bounds of a number schema. An exclusive bound moves to the next point of `step` (`1` for an
|
|
1166
|
+
* integer), an integer bound rounds inward, and an integer bound past `Number.MAX_SAFE_INTEGER` is left out, since it
|
|
1167
|
+
* prints as a rounded or invalid literal.
|
|
1168
|
+
*/
|
|
1169
|
+
function getNumberBounds({ min, max, exclusive, integer = false, step }) {
|
|
1170
|
+
const grid = step ?? (integer ? 1 : void 0);
|
|
1171
|
+
const above = (bound) => grid ? (Math.floor(bound / grid) + 1) * grid : bound;
|
|
1172
|
+
const below = (bound) => grid ? (Math.ceil(bound / grid) - 1) * grid : bound;
|
|
1173
|
+
const tightest = (pick, bounds) => {
|
|
1174
|
+
const usable = bounds.filter((bound) => bound !== void 0 && (!integer || Number.isSafeInteger(bound)));
|
|
1175
|
+
return usable.length ? pick(...usable) : void 0;
|
|
1176
|
+
};
|
|
1177
|
+
const lower = tightest(Math.max, [min !== void 0 && integer ? Math.ceil(min) : min, exclusive?.min === void 0 ? void 0 : above(exclusive.min)]);
|
|
1178
|
+
const upper = tightest(Math.min, [max !== void 0 && integer ? Math.floor(max) : max, exclusive?.max === void 0 ? void 0 : below(exclusive.max)]);
|
|
1179
|
+
return {
|
|
1180
|
+
...lower !== void 0 ? { min: lower } : {},
|
|
1181
|
+
...upper !== void 0 ? { max: upper } : {}
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1183
1184
|
// src/createPrinter.ts
|
|
1184
1185
|
/**
|
|
1185
1186
|
* Creates a schema printer: a function that takes a `SchemaNode` and emits
|