Skip to content

Commit

Permalink
fix(Config Schema): Treat explicit null or undefined as no value
Browse files Browse the repository at this point in the history
Workaround for ajv-validator/ajv#1287
  • Loading branch information
medikoo committed Sep 21, 2020
1 parent 9bec422 commit e5e42ba
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/classes/ConfigSchemaHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ const normalizeSchemaObject = (object, instanceSchema) => {
}
};

// Normalizer is introduced to workaround https://github.com/ajv-validator/ajv/issues/1287
// normalizedObjectsMap allows to handle circular structures without issues
const normalizedObjectsMap = new WeakMap();
const normalizeUserConfig = object => {
if (normalizedObjectsMap.has(object)) return normalizedObjectsMap.get(object);
if (Array.isArray(object)) {
const normalizedObject = [];
normalizedObjectsMap.set(object, normalizedObject);
for (const value of object) {
normalizedObject.push(_.isObject(value) ? normalizeUserConfig(value) : value);
}
return normalizedObject;
}
const normalizedObject = Object.create(null);
normalizedObjectsMap.set(object, normalizedObject);
for (const [key, value] of Object.entries(object)) {
if (value == null) continue;
normalizedObject[key] = _.isObject(value) ? normalizeUserConfig(value) : value;
}
return normalizedObject;
};

class ConfigSchemaHandler {
constructor(serverless) {
this.serverless = serverless;
Expand Down Expand Up @@ -77,7 +99,7 @@ class ConfigSchemaHandler {
normalizeSchemaObject(this.schema, this.schema);
const validate = ajv.compile(this.schema);

validate(userConfig);
validate(normalizeUserConfig(userConfig));
if (validate.errors) {
const messages = normalizeAjvErrors(validate.errors).map(err => err.message);
this.handleErrorMessages(messages);
Expand Down

0 comments on commit e5e42ba

Please sign in to comment.