Skip to content

Commit

Permalink
Improve log_level schema format check
Browse files Browse the repository at this point in the history
Modify config-validators-spec to expect the correct error when testing log_level
  • Loading branch information
busma13 committed Jan 3, 2024
1 parent bbc212f commit 2d61b34
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 1 addition & 4 deletions packages/job-components/src/job-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ export function jobSchema(context: Context): convict.Schema<any> {
default: undefined,
doc: 'the log level to be set on all loggers associated with the job',
format(level: unknown) {
if (typeof level !== 'string') {
throw new Error('must be of type string');
}
const logLevelStrings = Object.keys(logLevels);
if (!logLevelStrings.includes(level)) {
if (typeof level !== 'string' || !logLevelStrings.includes(level)) {
throw new Error(`must be one of the following: ${logLevelStrings}`);
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/job-components/test/config-validators-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ describe('when using native clustering', () => {
});

describe('when testing log_level with an invalid config', () => {
const logLevelStrings = Object.keys(logLevels);

it('should throw an error when given non-string', () => {
const schema = jobSchema(context);
const job = {
Expand All @@ -602,7 +604,7 @@ describe('when using native clustering', () => {
},
],
};
expect(() => validateJobConfig(schema, job)).toThrow('must be of type string');
expect(() => validateJobConfig(schema, job)).toThrow(`must be one of the following: ${logLevelStrings}`);
});

it('should throw an error when given a string that isn\'t a log level', () => {
Expand All @@ -618,7 +620,6 @@ describe('when using native clustering', () => {
},
],
};
const logLevelStrings = Object.keys(logLevels);
expect(() => validateJobConfig(schema, job)).toThrow(`must be one of the following: ${logLevelStrings}`);
});
});
Expand Down

0 comments on commit 2d61b34

Please sign in to comment.