Skip to content

Commit

Permalink
change how formatting errors is done (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
bompi88 authored Feb 1, 2024
1 parent 66a244a commit 959f459
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-snails-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arundo/typed-env": minor
---

change how formatting errors is done
4 changes: 2 additions & 2 deletions src/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ZodError } from 'zod';
import { ZodIssue } from 'zod';
import { Replace, CamelKeys, ConstantKeys, KebabKeys, PascalKeys } from 'string-ts';

export type NamingConvention = 'camelcase' | 'pascalcase' | 'kebabcase' | 'constantcase' | 'default';

export type Options<TTransform, TPrefixRemoval> = {
transform?: TTransform;
formatErrorFn?: (error: ZodError) => string;
constructErrorFn?: (issues: ZodIssue[]) => Error;
excludePrefix?: TPrefixRemoval;
};

Expand Down
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ZodTypeAny, ZodError } from 'zod';
import { ZodTypeAny, ZodError, ZodIssue } from 'zod';
import { replace, camelKeys, pascalKeys, kebabKeys, constantKeys, Replace } from 'string-ts';
import { ConditionalType, NamingConvention, Options, PrefixRemoved } from './contracts';

const formatError = (error: ZodError) =>
`Environment variable validation failed:${error.issues
.map(issue => `\n\t'${issue.path.join(',')}': ${issue.message}`)
.join(',')}`;
const constructError = (issues: ZodIssue[]) =>
new Error(
`Environment variable validation failed:${issues
.map(issue => `\n\t'${issue.path.join(',')}': ${issue.message}`)
.join(',')}`,
);

const getEnvironment = () => {
if ((import.meta as any)?.env !== undefined) {
Expand Down Expand Up @@ -63,16 +65,21 @@ export const typeEnvironment = <
options: Options<TTransform, TPrefixRemoval> = {},
overrideEnv: Record<string, string | undefined> = getEnvironment(),
) => {
const { transform = 'default', formatErrorFn = formatError, excludePrefix = '' as TPrefixRemoval } = options;
const {
transform = 'default',
constructErrorFn: constructErrorFn = constructError,
excludePrefix = '' as TPrefixRemoval,
} = options;

try {
const parsed = schema.parse(overrideEnv);
type TSchemaOutput = TSchema['_output'];
const prefixRemoved = removePrefix(parsed, excludePrefix) as PrefixRemoved<TSchemaOutput, TPrefixRemoval>;
return changeCase(transform, prefixRemoved) as ConditionalType<TTransform, typeof prefixRemoved>;
} catch (error) {
if (error instanceof ZodError) {
throw new Error(formatErrorFn ? formatErrorFn(error) : formatError(error));
const zodErrors = (error as ZodError)?.issues || (error as ZodError)?.errors || [];
if (zodErrors.length > 0) {
throw constructErrorFn ? constructErrorFn(zodErrors) : constructError(zodErrors);
}
throw new Error('Environment variable validation failed');
}
Expand Down

0 comments on commit 959f459

Please sign in to comment.