fix(json-schema-2020-12-samples): fix constraints for integer example values (#9749)

Refs #9740
This commit is contained in:
Oliwia Rogala
2024-03-26 14:55:25 +01:00
committed by GitHub
parent a82f6448d4
commit c002e59791
3 changed files with 68 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import { integer as randomInteger } from "../core/random"
import formatAPI from "../api/formatAPI"
import int32Generator from "../generators/int32"
import int64Generator from "../generators/int64"
import { applyNumberConstraints } from "./number"
const generateFormat = (schema) => {
const { format } = schema
@@ -25,14 +26,18 @@ const generateFormat = (schema) => {
return randomInteger()
}
const integerType = (schema) => {
const { format } = schema
let generatedInteger
if (typeof format === "string") {
return generateFormat(schema)
generatedInteger = generateFormat(schema)
} else {
generatedInteger = randomInteger()
}
return randomInteger()
return applyNumberConstraints(generatedInteger, schema)
}
export default integerType

View File

@@ -26,7 +26,7 @@ const generateFormat = (schema) => {
return randomNumber()
}
const applyNumberConstraints = (number, constraints = {}) => {
export const applyNumberConstraints = (number, constraints = {}) => {
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = constraints
const { multipleOf } = constraints
const epsilon = Number.isInteger(number) ? 1 : Number.EPSILON

View File

@@ -1646,7 +1646,7 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minimum", () => {
it("should handle minimum for number", () => {
const definition = {
type: "number",
minimum: 5,
@@ -1657,7 +1657,7 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle exclusiveMinimum", () => {
it("should handle exclusiveMinimum for number", () => {
const definition = {
type: "number",
exclusiveMinimum: 5,
@@ -1667,7 +1667,7 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maximum", () => {
it("should handle maximum for number", () => {
const definition = {
type: "number",
maximum: -1,
@@ -1678,7 +1678,7 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle exclusiveMaximum", () => {
it("should handle exclusiveMaximum for number", () => {
const definition = {
type: "number",
exclusiveMaximum: -1,
@@ -1689,7 +1689,7 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle multipleOf", () => {
it("should handle multipleOf for number", () => {
const definition = {
type: "number",
minimum: 22,
@@ -1701,6 +1701,61 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toStrictEqual(expected)
})
it("should handle minimum for integer", () => {
const definition = {
type: "integer",
minimum: 5,
}
const expected = 5
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle exclusiveMinimum for integer", () => {
const definition = {
type: "integer",
exclusiveMinimum: 5,
}
const expected = 6
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maximum for integer", () => {
const definition = {
type: "integer",
maximum: -1,
}
const expected = -1
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle exclusiveMaximum for integer", () => {
const definition = {
type: "integer",
exclusiveMaximum: -1,
}
const expected = -2
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle multipleOf for integer", () => {
const definition = {
type: "integer",
minimum: 22,
multipleOf: 3,
}
const expected = 24
expect(sampleFromSchema(definition)).toStrictEqual(expected)
})
it("should handle minLength", () => {
const definition = {
type: "string",