refactor(samples): add better support for exclusive number ranges (#8886)

This change is specific ot  JSON Schema 2020-12
and OpenAPI 3.1.0.

Refs #8577
This commit is contained in:
Vladimír Gorej
2023-06-06 15:02:15 +02:00
committed by GitHub
parent 836659d8ac
commit f2b0d4af41

View File

@@ -694,22 +694,25 @@ export const sampleFromSchemaGeneric = (
// display schema default // display schema default
value = primitive(schema) value = primitive(schema)
if (typeof value === "number") { if (typeof value === "number") {
let minValue = null
let maxValue = null
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema
if (typeof minimum === "number") { const epsilon = Number.isInteger(value) ? 1 : Number.EPSILON
minValue = minimum let minValue = typeof minimum === "number" ? minimum : null
let maxValue = typeof maximum === "number" ? maximum : null
if (typeof exclusiveMinimum === "number") {
minValue =
minValue !== null
? Math.max(minValue, exclusiveMinimum + epsilon)
: exclusiveMinimum + epsilon
} }
if (typeof exclusiveMinimum === "number" && exclusiveMinimum > minValue) { if (typeof exclusiveMaximum === "number") {
minValue = exclusiveMinimum + 1 maxValue =
maxValue !== null
? Math.min(maxValue, exclusiveMaximum - epsilon)
: exclusiveMaximum - epsilon
} }
if (typeof maximum === "number") {
maxValue = maximum value = (minValue > maxValue && value) || minValue || maxValue || value
}
if (typeof exclusiveMaximum === "number" && exclusiveMaximum < maxValue) {
maxValue = exclusiveMaximum - 1
}
value = minValue || maxValue || value
} }
if (typeof value === "string") { if (typeof value === "string") {
if (schema.maxLength !== null && schema.maxLength !== undefined) { if (schema.maxLength !== null && schema.maxLength !== undefined) {