Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/error-handlers/typeConstEnum.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const typeConstEnumErrorHandler = {

const keywordJson = new Set();
const constValueJson = /** @type string */ (normalizedErrors["https://json-schema.org/keyword/const"][schemaLocation].value);
if (allowedTypes.has(jsonTypeOf(constValueJson))) {
if (isAllowedType(constValueJson, allowedTypes)) {
keywordJson.add(constValueJson);
} else {
typeFiltered = true;
Expand All @@ -70,7 +70,7 @@ const typeConstEnumErrorHandler = {
const keywordJson = new Set();
const enumValuesJson = /** @type string[] */ (normalizedErrors["https://json-schema.org/keyword/enum"][schemaLocation].value);
for (const enumValueJson of enumValuesJson) {
if (allowedTypes.has(jsonTypeOf(enumValueJson))) {
if (isAllowedType(enumValueJson, allowedTypes)) {
keywordJson.add(enumValueJson);
} else {
typeFiltered = true;
Expand Down Expand Up @@ -127,6 +127,13 @@ const typeConstEnumErrorHandler = {
}
};

/** @type (json: string, allowedTypes: Set<string>) => boolean */
const isAllowedType = (json, allowedTypes) => {
const type = jsonTypeOf(json);
return allowedTypes.has(type)
|| (type === "number" && allowedTypes.has("integer") && Number.isInteger(JSON.parse(json)));
};

/**
* @param {string} value
* @returns {string}
Expand Down
35 changes: 35 additions & 0 deletions src/test-suite/tests/const.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,41 @@
}
]
},
{
"description": "const with integer type preserves integers",
"compatibility": "6",
"schema": { "type": "integer", "const": 1 },
"instance": "hello",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "1" },
"instanceLocation": "#",
"schemaLocations": ["#/const"]
}
]
},
{
"description": "const with integer type rejects fractions",
"compatibility": "6",
"schema": { "type": "integer", "const": 1.5 },
"instance": "hello",
"errors": [
{
"messageId": "boolean-schema-message",
"messageParams": {},
"instanceLocation": "#",
"schemaLocations": ["#/type", "#/const"]
}
]
},
{
"description": "const with integer type passes",
"compatibility": "6",
"schema": { "type": "integer", "const": -1 },
"instance": -1,
"errors": []
},
{
"description": "const pass",
"compatibility": "6",
Expand Down
162 changes: 162 additions & 0 deletions src/test-suite/tests/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,168 @@
}
]
},
{
"description": "enum with integer type preserves integers",
"schema": { "type": "integer", "enum": [1, 2] },
"instance": "hello",
"errors": [
{
"messageId": "enum-message",
"messageParams": { "expected": { "or": ["1", "2"] }, "count": 2 },
"instanceLocation": "#",
"schemaLocations": ["#/enum"]
}
]
},
{
"description": "enum with integer type filters fractions",
"schema": { "type": "integer", "enum": [1, 1.5] },
"instance": "hello",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "1" },
"instanceLocation": "#",
"schemaLocations": ["#/type", "#/enum"]
}
]
},
{
"description": "enum with number type preserves integers and fractions",
"schema": { "type": "number", "enum": [1, 1.5] },
"instance": "hello",
"errors": [
{
"messageId": "enum-message",
"messageParams": { "expected": { "or": ["1", "1.5"] }, "count": 2 },
"instanceLocation": "#",
"schemaLocations": ["#/enum"]
}
]
},
{
"description": "enum with string type preserves strings",
"schema": { "type": "string", "enum": ["a", "b"] },
"instance": 42,
"errors": [
{
"messageId": "enum-message",
"messageParams": { "expected": { "or": ["\"a\"", "\"b\""] }, "count": 2 },
"instanceLocation": "#",
"schemaLocations": ["#/enum"]
}
]
},
{
"description": "enum with integer and string types filters fractions",
"schema": { "type": ["integer", "string"], "enum": [1, "a", 1.5] },
"instance": null,
"errors": [
{
"messageId": "enum-message",
"messageParams": { "expected": { "or": ["1", "\"a\""] }, "count": 2 },
"instanceLocation": "#",
"schemaLocations": ["#/type", "#/enum"]
}
]
},
{
"description": "enum with integer type filters fractions when combined with number type",
"schema": {
"allOf": [
{ "type": "number" },
{ "type": "integer", "enum": [1, 1.5] }
]
},
"instance": "hello",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "1" },
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/type", "#/allOf/1/type", "#/allOf/1/enum"]
}
]
},
{
"description": "enum with number type filters fractions when combined with integer type",
"schema": {
"allOf": [
{ "type": "integer" },
{ "type": "number", "enum": [1, 1.5] }
]
},
"instance": "hello",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "1" },
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/type", "#/allOf/1/type", "#/allOf/1/enum"]
}
]
},
{
"description": "enum with integer type preserves negative integers and zero at nested locations",
"schema": {
"properties": {
"value": { "type": "integer", "enum": [-2, 0, -1.5] }
}
},
"instance": { "value": "hello" },
"errors": [
{
"messageId": "enum-message",
"messageParams": { "expected": { "or": ["-2", "0"] }, "count": 2 },
"instanceLocation": "#/value",
"schemaLocations": ["#/properties/value/type", "#/properties/value/enum"]
}
]
},
{
"description": "enum with integer type handles exponential notation",
"schema": { "type": "integer", "enum": [1e21, 1e-7] },
"instance": "hello",
"errors": [
{
"messageId": "const-message",
"messageParams": { "expected": "1e+21" },
"instanceLocation": "#",
"schemaLocations": ["#/type", "#/enum"]
}
]
},
{
"description": "enum with integer type rejects fractions and numeric strings",
"schema": { "type": "integer", "enum": [1.5, "1"] },
"instance": "hello",
"errors": [
{
"messageId": "boolean-schema-message",
"messageParams": {},
"instanceLocation": "#",
"schemaLocations": ["#/type", "#/enum"]
}
]
},
{
"description": "enum with integer type passes",
"schema": { "type": "integer", "enum": [-2, 0, 1.5] },
"instance": 0,
"errors": []
},
{
"description": "enum with number type passes for fractions",
"schema": { "type": "number", "enum": [1, 1.5] },
"instance": 1.5,
"errors": []
},
{
"description": "enum with integer and string types passes for strings",
"schema": { "type": ["integer", "string"], "enum": [1, "a", 1.5] },
"instance": "a",
"errors": []
},
{
"description": "enum pass",
"schema": {
Expand Down
Loading