Skip to content

Commit c588230

Browse files
committed
fix(json-schema-2020-12-samples): fix constraints for integer example values
1 parent 3afed64 commit c588230

File tree

3 files changed

+67
-8
lines changed
  • src/core/plugins/json-schema-2020-12-samples/fn/types
  • test/unit/core/plugins/json-schema-2020-12-samples

3 files changed

+67
-8
lines changed

src/core/plugins/json-schema-2020-12-samples/fn/types/integer.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { integer as randomInteger } from "../core/random"
55
import formatAPI from "../api/formatAPI"
66
import int32Generator from "../generators/int32"
77
import int64Generator from "../generators/int64"
8+
import { applyNumberConstraints } from "./number"
89

910
const generateFormat = (schema) => {
1011
const { format } = schema
@@ -27,12 +28,15 @@ const generateFormat = (schema) => {
2728
}
2829
const integerType = (schema) => {
2930
const { format } = schema
31+
let generatedNumber
3032

3133
if (typeof format === "string") {
32-
return generateFormat(schema)
34+
generatedNumber = generateFormat(schema)
35+
} else {
36+
generatedNumber = randomInteger()
3337
}
3438

35-
return randomInteger()
39+
return applyNumberConstraints(generatedNumber, schema)
3640
}
3741

3842
export default integerType

src/core/plugins/json-schema-2020-12-samples/fn/types/number.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const generateFormat = (schema) => {
2626
return randomNumber()
2727
}
2828

29-
const applyNumberConstraints = (number, constraints = {}) => {
29+
export const applyNumberConstraints = (number, constraints = {}) => {
3030
const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = constraints
3131
const { multipleOf } = constraints
3232
const epsilon = Number.isInteger(number) ? 1 : Number.EPSILON

test/unit/core/plugins/json-schema-2020-12-samples/fn.js

+60-5
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ describe("sampleFromSchema", () => {
16461646
expect(sampleFromSchema(definition)).toEqual(expected)
16471647
})
16481648

1649-
it("should handle minimum", () => {
1649+
it("should handle minimum for number", () => {
16501650
const definition = {
16511651
type: "number",
16521652
minimum: 5,
@@ -1657,7 +1657,7 @@ describe("sampleFromSchema", () => {
16571657
expect(sampleFromSchema(definition)).toEqual(expected)
16581658
})
16591659

1660-
it("should handle exclusiveMinimum", () => {
1660+
it("should handle exclusiveMinimum for number", () => {
16611661
const definition = {
16621662
type: "number",
16631663
exclusiveMinimum: 5,
@@ -1667,7 +1667,7 @@ describe("sampleFromSchema", () => {
16671667
expect(sampleFromSchema(definition)).toEqual(expected)
16681668
})
16691669

1670-
it("should handle maximum", () => {
1670+
it("should handle maximum for number", () => {
16711671
const definition = {
16721672
type: "number",
16731673
maximum: -1,
@@ -1678,7 +1678,7 @@ describe("sampleFromSchema", () => {
16781678
expect(sampleFromSchema(definition)).toEqual(expected)
16791679
})
16801680

1681-
it("should handle exclusiveMaximum", () => {
1681+
it("should handle exclusiveMaximum for number", () => {
16821682
const definition = {
16831683
type: "number",
16841684
exclusiveMaximum: -1,
@@ -1689,7 +1689,7 @@ describe("sampleFromSchema", () => {
16891689
expect(sampleFromSchema(definition)).toEqual(expected)
16901690
})
16911691

1692-
it("should handle multipleOf", () => {
1692+
it("should handle multipleOf for number", () => {
16931693
const definition = {
16941694
type: "number",
16951695
minimum: 22,
@@ -1701,6 +1701,61 @@ describe("sampleFromSchema", () => {
17011701
expect(sampleFromSchema(definition)).toStrictEqual(expected)
17021702
})
17031703

1704+
it("should handle minimum for integer", () => {
1705+
const definition = {
1706+
type: "integer",
1707+
minimum: 5,
1708+
}
1709+
1710+
const expected = 5
1711+
1712+
expect(sampleFromSchema(definition)).toEqual(expected)
1713+
})
1714+
1715+
it("should handle exclusiveMinimum for integer", () => {
1716+
const definition = {
1717+
type: "integer",
1718+
exclusiveMinimum: 5,
1719+
}
1720+
const expected = 6
1721+
1722+
expect(sampleFromSchema(definition)).toEqual(expected)
1723+
})
1724+
1725+
it("should handle maximum for integer", () => {
1726+
const definition = {
1727+
type: "integer",
1728+
maximum: -1,
1729+
}
1730+
1731+
const expected = -1
1732+
1733+
expect(sampleFromSchema(definition)).toEqual(expected)
1734+
})
1735+
1736+
it("should handle exclusiveMaximum for integer", () => {
1737+
const definition = {
1738+
type: "integer",
1739+
exclusiveMaximum: -1,
1740+
}
1741+
1742+
const expected = -2
1743+
1744+
expect(sampleFromSchema(definition)).toEqual(expected)
1745+
})
1746+
1747+
it("should handle multipleOf for integer", () => {
1748+
const definition = {
1749+
type: "integer",
1750+
minimum: 22,
1751+
multipleOf: 3,
1752+
}
1753+
1754+
const expected = 24
1755+
1756+
expect(sampleFromSchema(definition)).toStrictEqual(expected)
1757+
})
1758+
17041759
it("should handle minLength", () => {
17051760
const definition = {
17061761
type: "string",

0 commit comments

Comments
 (0)