Skip to content

Commit

Permalink
Use zod for config validation (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: deptyped <[email protected]>
  • Loading branch information
imMohika and deptyped committed Mar 1, 2023
1 parent 48fd4f5 commit fe8a244
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"import"
],
"rules": {
"prettier/prettier": "error",
"prettier/prettier":[ "error", {
"endOfLine": "auto"
}],
"arrow-body-style": "off",
"prefer-arrow-callback": "off",
"consistent-return": "off",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
if: ${{ github.ref != 'refs/heads/main' }}
with:
context: .
push: true
push: ${{ !github.event.pull_request.head.repo.fork }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Build and Push Latest Docker Image
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ dist
# Data
data/

*.env
*.env

# Jetbrains IDE
.idea/
36 changes: 17 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@prisma/client": "4.10.1",
"callback-data": "1.0.2",
"dotenv": "16.0.3",
"envalid": "7.3.1",
"fastify": "4.13.0",
"grammy": "1.14.1",
"grammy-guard": "0.4.1",
Expand All @@ -36,7 +35,8 @@
"pino": "8.10.0",
"pino-pretty": "9.3.0",
"prom-client": "14.1.1",
"tsx": "3.12.3"
"tsx": "3.12.2",
"zod": "^3.20.2"
},
"devDependencies": {
"@types/debug": "4.1.7",
Expand Down
78 changes: 56 additions & 22 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
import "dotenv/config";
import { cleanEnv, json, num, str } from "envalid";
import { PollingOptions } from "grammy";
import z from "zod";

export const config = cleanEnv(process.env, {
NODE_ENV: str({ choices: ["development", "production"] }),
LOG_LEVEL: str({
choices: ["trace", "debug", "info", "warn", "error", "fatal", "silent"],
}),
DATABASE_URL: str(),
REDIS_URL: str(),
BOT_SERVER_HOST: str({
default: "0.0.0.0",
}),
BOT_SERVER_PORT: num({
default: 80,
}),
BOT_ALLOWED_UPDATES: json<PollingOptions["allowed_updates"]>({
default: [],
}),
BOT_TOKEN: str(),
BOT_WEBHOOK: str(),
BOT_ADMIN_USER_ID: num(),
const updates = [
"message",
"poll",
"poll_answer",
"my_chat_member",
"chat_member",
"chat_join_request",
"edited_message",
"channel_post",
"edited_channel_post",
"inline_query",
"chosen_inline_result",
"callback_query",
"shipping_query",
"pre_checkout_query",
] as const;

const configSchema = z.object({
NODE_ENV: z.enum(["development", "production"]),
LOG_LEVEL: z.enum([
"trace",
"debug",
"info",
"warn",
"error",
"fatal",
"silent",
]),
DATABASE_URL: z.string(),
REDIS_URL: z.string(),
BOT_SERVER_HOST: z.string().default("0.0.0.0"),
BOT_SERVER_PORT: z.coerce.number().positive().default(80),
BOT_ALLOWED_UPDATES: z.preprocess((v: unknown) => {
try {
return JSON.parse(String(v));
} catch (e) {
return null;
}
}, z.array(z.enum(updates))),
BOT_TOKEN: z.string(),
BOT_WEBHOOK: z.string().url(),
BOT_ADMIN_USER_ID: z.coerce.number().finite(),
});

export type Config = typeof config;
const parseConfig = (env: NodeJS.ProcessEnv) => {
const config = configSchema.parse(env);

return {
...config,
isDev: process.env.NODE_ENV === "development",
isProd: process.env.NODE_ENV === "production",
};
};

export type Config = ReturnType<typeof parseConfig>;

export const config = parseConfig(process.env);

0 comments on commit fe8a244

Please sign in to comment.