Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
char0n marked this conversation as resolved.
Show resolved Hide resolved

const generateFormat = (schema) => {
const { format } = schema
Expand All @@ -25,14 +26,18 @@ const generateFormat = (schema) => {

return randomInteger()
}

const integerType = (schema) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
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