Skip to content

Commit 97ba23a

Browse files
authored
Merge pull request #167 from jcsumlin/task/add-logging
Displays description on validation warnings/errors
2 parents f0d35c2 + 60e49a7 commit 97ba23a

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

__tests__/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ jest.mock('../src/validateInput');
1212
describe('validateEnvVars', () => {
1313
let processExitSpy: jest.SpyInstance;
1414
let consoleErrorSpy: jest.SpyInstance;
15+
let consoleLogSpy: jest.SpyInstance;
1516

1617
beforeEach(() => {
1718
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
19+
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
1820
processExitSpy = jest.spyOn(process, 'exit').mockImplementation();
1921
});
2022

@@ -145,4 +147,26 @@ describe('validateEnvVars', () => {
145147
validateEnvVars({ schema, envPath });
146148
}).not.toThrow();
147149
});
150+
151+
it('descriptions are logged on console warning', () => {
152+
const schema = z.object({
153+
OPTIONAL_1: z
154+
.string({ description: 'This is an optional variable' })
155+
.optional(),
156+
EXPECTED_2: z.string(),
157+
});
158+
const envPath = './__tests__/.env.test';
159+
160+
validateEnvVars({ schema, envPath });
161+
162+
expect(consoleLogSpy).toHaveBeenCalledTimes(3);
163+
expect(consoleLogSpy).toHaveBeenNthCalledWith(
164+
1,
165+
expect.stringContaining('This is an optional variable')
166+
);
167+
expect(consoleLogSpy).toHaveBeenNthCalledWith(
168+
2,
169+
expect.not.stringContaining('This is an optional variable')
170+
);
171+
});
148172
});

src/logParseResults.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ function logParseResults(
6262

6363
// loop over each variable and log the result
6464
Object.entries(schemaKeys).forEach(([varName, res]) => {
65+
// Try to get the description from the Zod option if present
66+
let description = '';
67+
if (typeof schema.shape[varName]?.description === 'string') {
68+
description = `\n\r - ${schema.shape[varName].description}`;
69+
}
6570
// parsing succeeded
6671
if (res.error === null && res.data !== '' && res.data !== 'undefined') {
6772
const varValue = logVars
@@ -72,13 +77,13 @@ function logParseResults(
7277
// no data, but parsing did not fail and the variable is optional
7378
else if (res.error === null && res.optional) {
7479
console.log(
75-
`${WARN_SYMBOL} ${varName} ${WARN_COLOR}'${res.data}'${RESET_COLOR}`
80+
`${WARN_SYMBOL} ${varName} ${WARN_COLOR}'${res.data}'${RESET_COLOR}${description}`
7681
);
7782
}
7883
// parsing failed
7984
else {
8085
console.error(
81-
`${ERR_SYMBOL} ${varName}: ${ERR_COLOR}${res.error}${RESET_COLOR}`
86+
`${ERR_SYMBOL} ${varName}: ${ERR_COLOR}${res.error}${RESET_COLOR}${description}`
8287
);
8388
error_count++;
8489
}

src/schemaTypes.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import {
33
enum as envEnum,
44
literal as envLiteral,
55
z,
6+
RawCreateParams,
67
} from 'zod';
78

8-
const nonEmpty = () =>
9-
z.string().min(1, { message: 'Variable cannot be empty' });
9+
const nonEmpty = (params?: RawCreateParams) =>
10+
z.string(params).min(1, { message: 'Variable cannot be empty' });
1011

11-
const envNonEmptyString = () =>
12-
nonEmpty().refine((val) => val != 'undefined', {
12+
const envNonEmptyString = (params?: RawCreateParams) =>
13+
nonEmpty(params).refine((val) => val != 'undefined', {
1314
message: `Variable cannot equal 'undefined'`,
1415
});
1516

16-
const envInteger = () =>
17-
nonEmpty().regex(/^-?\d+$/, {
17+
const envInteger = (params?: RawCreateParams) =>
18+
nonEmpty(params).regex(/^-?\d+$/, {
1819
message: 'Variable must be a valid integer',
1920
});
2021

0 commit comments

Comments
 (0)