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