Skip to content

Commit

Permalink
feat(config): validate unknown options
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 10, 2020
1 parent 62e2445 commit 5c2cc53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/config/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ export default function constructAndValidateConfiguration(
logLevel,
cache,
onComplete,
...unknownKeys
} = configToValidate;

const config = { ...configToValidate };
const errors: string[] = [];

// Validate no unknown options were given
const unsupportedOptions = Object.keys(unknownKeys);
if (unsupportedOptions.length) {
errors.push(
`Options [${unsupportedOptions.join(', ')}] are not supported`
);
}

// Required fields
if (!repositories || !repositories.length) {
errors.push(`Missing repositories.`);
Expand Down
15 changes: 13 additions & 2 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ const DEFAULT_CONFIGURATION: ConfigToValidate = {

describe('Config validator', () => {
test('repositories are required', () => {
validator({ ...DEFAULT_CONFIGURATION, repositories: undefined! });
const [validationError] = getConsoleLogCalls();
const config = { ...DEFAULT_CONFIGURATION, repositories: undefined! };
validator(config);

const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Configuration validation errors:');
expect(validationError).toMatch('- Missing repositories.');
});

test('additional options are unsupported', () => {
const key = 'someMistypedKey';
const config = { ...DEFAULT_CONFIGURATION, [key]: true };
validator(config);

const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Configuration validation errors:');
expect(validationError).toMatch(`- Options [${key}] are not supported`);
});
});

0 comments on commit 5c2cc53

Please sign in to comment.