Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config Schema: Treat null or undefined as lack of value #8272

Merged
merged 2 commits into from Sep 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/classes/ConfigSchemaHandler/index.js
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,11 +99,9 @@ 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, userConfig, this.schema).map(
err => err.message
);
const messages = normalizeAjvErrors(validate.errors).map(err => err.message);
this.handleErrorMessages(messages);
}
}
Expand Down