feat(samples): support multipleOf keyword (#8890)
This change is specific ti JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
This commit is contained in:
@@ -80,11 +80,12 @@ const sanitizeRef = (value) =>
|
|||||||
|
|
||||||
const objectContracts = ["maxProperties", "minProperties"]
|
const objectContracts = ["maxProperties", "minProperties"]
|
||||||
const arrayContracts = ["minItems", "maxItems"]
|
const arrayContracts = ["minItems", "maxItems"]
|
||||||
const numberContracts = [
|
const numberConstraints = [
|
||||||
"minimum",
|
"minimum",
|
||||||
"maximum",
|
"maximum",
|
||||||
"exclusiveMinimum",
|
"exclusiveMinimum",
|
||||||
"exclusiveMaximum",
|
"exclusiveMaximum",
|
||||||
|
"multipleOf",
|
||||||
]
|
]
|
||||||
const stringContracts = ["minLength", "maxLength"]
|
const stringContracts = ["minLength", "maxLength"]
|
||||||
|
|
||||||
@@ -104,7 +105,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
|
|||||||
"const",
|
"const",
|
||||||
...objectContracts,
|
...objectContracts,
|
||||||
...arrayContracts,
|
...arrayContracts,
|
||||||
...numberContracts,
|
...numberConstraints,
|
||||||
...stringContracts,
|
...stringContracts,
|
||||||
].forEach((key) => setIfNotDefinedInTarget(key))
|
].forEach((key) => setIfNotDefinedInTarget(key))
|
||||||
|
|
||||||
@@ -271,7 +272,7 @@ export const sampleFromSchemaGeneric = (
|
|||||||
type = "object"
|
type = "object"
|
||||||
} else if (items || schemaHasAny(arrayContracts)) {
|
} else if (items || schemaHasAny(arrayContracts)) {
|
||||||
type = "array"
|
type = "array"
|
||||||
} else if (schemaHasAny(numberContracts)) {
|
} else if (schemaHasAny(numberConstraints)) {
|
||||||
type = "number"
|
type = "number"
|
||||||
schema.type = "number"
|
schema.type = "number"
|
||||||
} else if (!usePlainValue && !schema.enum) {
|
} else if (!usePlainValue && !schema.enum) {
|
||||||
@@ -695,6 +696,7 @@ export const sampleFromSchemaGeneric = (
|
|||||||
value = primitive(schema)
|
value = primitive(schema)
|
||||||
if (typeof value === "number") {
|
if (typeof value === "number") {
|
||||||
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema
|
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema
|
||||||
|
const { multipleOf } = schema
|
||||||
const epsilon = Number.isInteger(value) ? 1 : Number.EPSILON
|
const epsilon = Number.isInteger(value) ? 1 : Number.EPSILON
|
||||||
let minValue = typeof minimum === "number" ? minimum : null
|
let minValue = typeof minimum === "number" ? minimum : null
|
||||||
let maxValue = typeof maximum === "number" ? maximum : null
|
let maxValue = typeof maximum === "number" ? maximum : null
|
||||||
@@ -711,8 +713,12 @@ export const sampleFromSchemaGeneric = (
|
|||||||
? Math.min(maxValue, exclusiveMaximum - epsilon)
|
? Math.min(maxValue, exclusiveMaximum - epsilon)
|
||||||
: exclusiveMaximum - epsilon
|
: exclusiveMaximum - epsilon
|
||||||
}
|
}
|
||||||
|
|
||||||
value = (minValue > maxValue && value) || minValue || maxValue || value
|
value = (minValue > maxValue && value) || minValue || maxValue || value
|
||||||
|
|
||||||
|
if (typeof multipleOf === "number" && multipleOf > 0) {
|
||||||
|
const remainder = value % multipleOf
|
||||||
|
value = remainder === 0 ? value : value + multipleOf - remainder
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
if (schema.maxLength !== null && schema.maxLength !== undefined) {
|
if (schema.maxLength !== null && schema.maxLength !== undefined) {
|
||||||
|
|||||||
@@ -1330,6 +1330,18 @@ describe("sampleFromSchema", () => {
|
|||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle multipleOf", () => {
|
||||||
|
const definition = {
|
||||||
|
type: "number",
|
||||||
|
minimum: 22,
|
||||||
|
multipleOf: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
const expected = 24
|
||||||
|
|
||||||
|
expect(sampleFromSchema(definition)).toStrictEqual(expected)
|
||||||
|
})
|
||||||
|
|
||||||
it("should handle minLength", () => {
|
it("should handle minLength", () => {
|
||||||
const definition = {
|
const definition = {
|
||||||
type: "string",
|
type: "string",
|
||||||
|
|||||||
Reference in New Issue
Block a user