Skip to content

Commit

Permalink
Merge pull request #13 from webpack/feat-undefined-for-enum
Browse files Browse the repository at this point in the history
feat: added `undefinedAsNull` for enums
  • Loading branch information
TheLarkInn committed Jun 13, 2023
2 parents dd695e2 + b7ab93f commit ac7e6c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions format-schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ const PROPERTIES = [
"enum",

"absolutePath",

"undefinedAsNull",

"minLength",

"minimum",
Expand Down
21 changes: 19 additions & 2 deletions precompile-schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ ajv.addKeyword({
},
});

ajv.addKeyword({
keyword: "undefinedAsNull",
schemaType: "boolean",

code(ctx) {
// Nothing, just to avoid failing
},
});
ajv.removeKeyword("enum");
ajv.addKeyword({
keyword: "enum",
schemaType: "array",
$data: true,

code(ctx) {
const { data, schema } = ctx;
const { data, schema, parentSchema } = ctx;
for (const item of schema) {
if (typeof item === "object" && item !== null) {
throw new Error(
Expand All @@ -81,8 +89,17 @@ ajv.addKeyword({
);
}
}

ctx.fail(
schema.map((x) => _`${data} !== ${x}`).reduce((a, b) => _`${a} && ${b}`)
schema
.map((x) => {
if (x === null && parentSchema.undefinedAsNull) {
return _`${data} !== null && ${data} !== undefined`;
}

return _`${data} !== ${x}`;
})
.reduce((a, b) => _`${a} && ${b}`)
);
},
});
Expand Down
1 change: 1 addition & 0 deletions schemas-lint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ for (const filename of schemas) {
"oneOf",
"anyOf",
"absolutePath",
"undefinedAsNull",
"description",
"enum",
"minLength",
Expand Down

0 comments on commit ac7e6c4

Please sign in to comment.