From c002e59791907086d603b6bb9e6663e96a8aefec Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Tue, 26 Mar 2024 14:55:25 +0100 Subject: [PATCH] fix(json-schema-2020-12-samples): fix constraints for integer example values (#9749) Refs #9740 --- .../fn/types/integer.js | 9 ++- .../fn/types/number.js | 2 +- .../plugins/json-schema-2020-12-samples/fn.js | 65 +++++++++++++++++-- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/types/integer.js b/src/core/plugins/json-schema-2020-12-samples/fn/types/integer.js index f5159a46789..fd801bced1c 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/types/integer.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/types/integer.js @@ -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 diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/types/number.js b/src/core/plugins/json-schema-2020-12-samples/fn/types/number.js index afc6720e0e9..d246a94d186 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/types/number.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/types/number.js @@ -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 diff --git a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js index 4d45ad21c4e..8225a180178 100644 --- a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js +++ b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js @@ -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",