Skip to content

Commit

Permalink
fix: process boolean variables correctly with zod (#1756)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanes authored Jan 30, 2025
1 parent 455cc94 commit ae55aa7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions packages/bff/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import 'dotenv/config';
import z from 'zod';

const stringToBoolean = (val: unknown): boolean | unknown => {
if (typeof val === 'string') {
if (val.toLowerCase() === 'true') return true;
if (val.toLowerCase() === 'false') return false;
}
return false;
};

const envVariables = z.object({
// todo: rather use version here instead of git_sha
GIT_SHA: z.string().default('v6.1.5'),
HOST: z.string().default('0.0.0.0'),
DB_CONNECTION_STRING: z.string().default('postgres://postgres:mysecretpassword@localhost:5432/dialogporten'),
APPLICATIONINSIGHTS_CONNECTION_STRING: z.string().optional(),
APPLICATIONINSIGHTS_ENABLED: z.coerce.boolean().default(false),
APPLICATIONINSIGHTS_ENABLED: z.preprocess(stringToBoolean, z.boolean().default(false)),
PORT: z.coerce.number().default(3000),
OIDC_URL: z.string().default('test.idporten.no'),
HOSTNAME: z.string().default('http://localhost'),
SESSION_SECRET: z.string().min(32).default('SecretHereSecretHereSecretHereSecretHereSecretHereSecretHereSecretHere'),
ENABLE_HTTPS: z.boolean().default(false),
ENABLE_HTTPS: z.preprocess(stringToBoolean, z.boolean().default(false)),
COOKIE_MAX_AGE: z.coerce.number().default(30 * 24 * 60 * 60 * 1000),
COOKIE_SECURE: z.coerce.boolean().default(false),
COOKIE_HTTP_ONLY: z.coerce.boolean().default(false),
COOKIE_SECURE: z.preprocess(stringToBoolean, z.boolean().default(false)),
COOKIE_HTTP_ONLY: z.preprocess(stringToBoolean, z.boolean().default(false)),
REDIS_CONNECTION_STRING: z.string().default('redis://:mysecretpassword@127.0.0.1:6379/0'),
CLIENT_ID: z.string(),
CLIENT_SECRET: z.string(),
MIGRATION_RUN: z.coerce.boolean().default(false),
MIGRATION_RUN: z.preprocess(stringToBoolean, z.boolean().default(false)),
DIALOGPORTEN_URL: z.string().default('https://altinn-dev-api.azure-api.net/dialogporten/graphql'),
CONTAINER_APP_REPLICA_NAME: z.string().default(''),
ENABLE_GRAPHIQL: z.coerce.boolean().default(true),
ENABLE_GRAPHIQL: z.preprocess(stringToBoolean, z.boolean().default(false)),
});

const env = envVariables.parse(process.env);
Expand Down
2 changes: 1 addition & 1 deletion packages/bff/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import redisClient from './redisClient.ts';
const { version, port, host, oidc_url, hostname, client_id, client_secret, redisConnectionString } = config;

const startServer = async (): Promise<void> => {
const { secret, enableHttps, cookie: cookieConfig, enableGraphiql } = config;
const { secret, cookie: cookieConfig, enableGraphiql } = config;

const server = Fastify({
ignoreTrailingSlash: true,
Expand Down

0 comments on commit ae55aa7

Please sign in to comment.