From 2d61b349b1ac2328ef5a3d7c34b323339dc8025b Mon Sep 17 00:00:00 2001 From: busma13 Date: Wed, 3 Jan 2024 07:19:03 -0700 Subject: [PATCH] Improve log_level schema format check Modify config-validators-spec to expect the correct error when testing log_level --- packages/job-components/src/job-schemas.ts | 5 +---- packages/job-components/test/config-validators-spec.ts | 5 +++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/job-components/src/job-schemas.ts b/packages/job-components/src/job-schemas.ts index f5f9c27438e..fb9105442ec 100644 --- a/packages/job-components/src/job-schemas.ts +++ b/packages/job-components/src/job-schemas.ts @@ -214,11 +214,8 @@ export function jobSchema(context: Context): convict.Schema { 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}`); } } diff --git a/packages/job-components/test/config-validators-spec.ts b/packages/job-components/test/config-validators-spec.ts index b64d4d8e8d8..9a08c471881 100644 --- a/packages/job-components/test/config-validators-spec.ts +++ b/packages/job-components/test/config-validators-spec.ts @@ -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 = { @@ -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', () => { @@ -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}`); }); });