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

Fix a bug where defaults would be modified on validation #252

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/252.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where the default configuration would be overwritten on validation
7 changes: 7 additions & 0 deletions spec/integ/config-validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ describe("ConfigValidator", function() {
baz: 100
}
});
// Ensure defaults isn't modified
expect(defaults).toEqual({
flibble: "wibble",
foo: {
baz: 100
}
});
});

it("should throw an error for invalid configs", function() {
Expand Down
9 changes: 6 additions & 3 deletions src/components/config-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import extend from "extend";

type Schema = any;

interface ValidationError extends Error {
_validationErrors?: validator.ValidationError[];
}

export class ConfigValidator {

/**
Expand Down Expand Up @@ -50,13 +54,12 @@ export class ConfigValidator {
console.error(`The field ${error.field} is ${error.value}` +
` which ${error.message}`);
});
// This is a bit awful, but it's how we return validation errors.
const e: any = new Error("Failed to validate file");
const e: ValidationError = new Error("Failed to validate file");
e._validationErrors = js.errors;
throw e;
}
// mux in the default config
return extend(true, defaultConfig, inputConfig);
return extend(true, {}, defaultConfig, inputConfig);
}

private static loadFromFile(filename: string): Schema {
Expand Down