Skip to content

Commit

Permalink
fix(json-schema-2020-12-samples): fix constraints for integer example…
Browse files Browse the repository at this point in the history
… values (#9749)

Refs #9740
  • Loading branch information
glowcloud committed Mar 26, 2024
1 parent a82f644 commit c002e59
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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
Expand Down
65 changes: 60 additions & 5 deletions test/unit/core/plugins/json-schema-2020-12-samples/fn.js
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down

0 comments on commit c002e59

Please sign in to comment.