Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
refactor: improve env validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed May 30, 2023
1 parent 9a6af43 commit 4b4624c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { z, ZodError } from "zod";
import { formatZodError } from "~/lib/utils";

const snowflake = z.string().regex(/^\d+$/);
const snowflake = z
.string()
.regex(/^\d+$/, "Should be a snowflake, not a generic string");

const env = z.object({
NODE_ENV: z.string().min(1),
Expand Down
15 changes: 12 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ export const successEmbed = (title: string, description: string) => {
export const formatZodError = (err: ZodError) => {
const issues = err.issues;
let ret = red(
bold(`${issues.length} validation error${issues.length > 1 ? "s" : ""}!\n`)
bold(
`${issues.length} validation error${issues.length !== 1 ? "s" : ""}!\n`
)
);

for (const issue of issues) {
ret += `${blue(issue.path.join(" > "))} ${dim("::")} ${issue.message}\n`;
ret +=
`${blue(issue.path.join(" > "))} ` +
`${dim("::")} ` +
issue.code +
" " +
`${dim("::")} ` +
issue.message +
"\n";
}

return ret;
return ret.trim();
};

/** https://stackoverflow.com/a/14919494 */
Expand Down

0 comments on commit 4b4624c

Please sign in to comment.