Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 7, 2023
1 parent 0ee1392 commit 7c5a40e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ValidationError.js
Expand Up @@ -544,10 +544,16 @@ class ValidationError extends Error {

if (schema.enum) {
const enumValues = /** @type {Array<any>} */ (schema.enum)
.map((item) => JSON.stringify(item))
.map((item) => {
if (item === null && schema.undefinedAsNull) {
return `${JSON.stringify(item)} | undefined`;
}

return JSON.stringify(item);
})
.join(" | ");

return `${enumValues}${schema.undefinedAsNull ? " | undefined" : ""}`;
return `${enumValues}`;
}

if (typeof schema.const !== "undefined") {
Expand Down
1 change: 1 addition & 0 deletions src/keywords/undefinedAsNull.js
Expand Up @@ -19,6 +19,7 @@ function addUndefinedAsNullKeyword(ajv) {
const idx = dataCxt.parentDataProperty;

if (typeof dataCxt.parentData[idx] === "undefined") {
// eslint-disable-next-line no-param-reassign
dataCxt.parentData[dataCxt.parentDataProperty] = null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/index.test.js
Expand Up @@ -3022,6 +3022,7 @@ describe("Validation", () => {
createFailedTestCase(
"array with enum and undefined #2",
{
// eslint-disable-next-line no-undefined
arrayStringAndEnum: ["foo", "bar", undefined, 1],
},
(msg) => expect(msg).toMatchSnapshot()
Expand All @@ -3030,6 +3031,7 @@ describe("Validation", () => {
createFailedTestCase(
"array with enum and undefined #3",
{
// eslint-disable-next-line no-undefined
arrayStringAndEnumAndNoUndefined: ["foo", "bar", undefined],
},
(msg) => expect(msg).toMatchSnapshot()
Expand Down

0 comments on commit 7c5a40e

Please sign in to comment.