Skip to content

Commit

Permalink
fix(parameter): add missing allOf macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mooyoul committed Nov 4, 2020
1 parent 2777682 commit 7310131
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,42 @@ function usePatchedAjv() {
removeAdditional: "all",
});

ajvInstance.removeKeyword("oneOf");
ajvInstance.addKeyword("oneOf", {
compile: (schemas) => (data) => {
for (const schema of schemas) {
const validator = ajv.compile(schema);
const valid = validator(_.cloneDeep(data));
// Patch oneOf / anyOf keywords to support `removeAdditional` behavior like Joi
["oneOf", "anyOf"].forEach((keyword) => {
ajvInstance.removeKeyword(keyword);
ajvInstance.addKeyword(keyword, {
compile: (schemas) => (data) => {
for (const schema of schemas) {
const validator = ajv.compile(schema);
const valid = validator(_.cloneDeep(data));

if (valid) {
validator(data);
return valid;
if (valid) {
validator(data);
return valid;
}
}
}
return false;
return false;
},
modifying: true,
metaSchema: {
type: "array",
items: [{ type: "object" }],
},
errors: false,
});
});

// Patch allOf keywords to support `removeAdditional` behavior like Joi
ajvInstance.removeKeyword("allOf");
ajvInstance.addKeyword("allOf", {
macro: (schema) => {
return _.merge({}, ...schema);
},
modifying: true,
metaSchema: {
type: "array",
items: [{ type: "object" }],
},
errors: false,
errors: true,
});

return ajvInstance;
Expand Down

0 comments on commit 7310131

Please sign in to comment.