Skip to content

Commit

Permalink
fix(Config Schema): Ensure to preserve null properties in user config
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 7, 2020
1 parent fa6d72b commit c93a799
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -25,21 +25,32 @@ 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 normalizedObjectsSet = new WeakSet();
const normalizeUserConfig = object => {
if (normalizedObjectsSet.has(object)) return object;
normalizedObjectsSet.add(object);
if (Array.isArray(object)) {
for (const value of object) {
if (_.isObject(value)) normalizeUserConfig(value);
}
} else {
for (const [key, value] of Object.entries(object)) {
if (value == null) delete object[key];
else if (_.isObject(value)) normalizeUserConfig(value);
const normalizeUserConfig = userConfig => {
const normalizedObjectsSet = new WeakSet();
const nullPaths = [];
const normalizeObject = (object, path) => {
if (normalizedObjectsSet.has(object)) return;
normalizedObjectsSet.add(object);
if (Array.isArray(object)) {
for (const [index, value] of object.entries()) {
if (_.isObject(value)) normalizeObject(value, path.concat(index));
}
} else {
for (const [key, value] of Object.entries(object)) {
if (value == null) {
nullPaths.push(path.concat(key));
delete object[key];
} else if (_.isObject(value)) {
normalizeObject(value, path.concat(key));
}
}
}
}
return object;
};
normalizeObject(userConfig, []);
return { nullPaths };
};
const denormalizeUserConfig = (userConfig, { nullPaths }) => {
for (const nullPath of nullPaths) _.set(userConfig, nullPath, null);
};

class ConfigSchemaHandler {
Expand Down Expand Up @@ -96,7 +107,9 @@ class ConfigSchemaHandler {
normalizeSchemaObject(this.schema, this.schema);
const validate = ajv.compile(this.schema);

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

0 comments on commit c93a799

Please sign in to comment.