Merge pull request #37 from gsi-kevincarrera/fix/backend-always-return-true

Fix: Implement function to correctly handle boolean strings with zod
This commit is contained in:
mrjvs 2024-01-25 22:27:15 +01:00 committed by GitHub
commit f58c2c86e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 11 deletions

View File

@ -2,6 +2,7 @@ import { devFragment } from '@/config/fragments/dev';
import { dockerFragment } from '@/config/fragments/docker'; import { dockerFragment } from '@/config/fragments/docker';
import { createConfigLoader } from 'neat-config'; import { createConfigLoader } from 'neat-config';
import { z } from 'zod'; import { z } from 'zod';
import { booleanSchema } from './schema';
const fragments = { const fragments = {
dev: devFragment, dev: devFragment,
@ -13,7 +14,7 @@ export const ormConfigSchema = z.object({
// connection URL for postgres database // connection URL for postgres database
connection: z.string(), connection: z.string(),
// whether to use SSL for the connection // whether to use SSL for the connection
ssl: z.coerce.boolean().default(false), ssl: booleanSchema.default(false),
}), }),
}); });

View File

@ -1,5 +1,7 @@
import { z } from 'zod'; import { z } from 'zod';
export const booleanSchema = z.preprocess((val) => val === 'true', z.boolean());
export const configSchema = z.object({ export const configSchema = z.object({
server: z server: z
.object({ .object({
@ -11,13 +13,13 @@ export const configSchema = z.object({
// disable cross origin restrictions, allow any site. // disable cross origin restrictions, allow any site.
// overwrites the cors option above // overwrites the cors option above
allowAnySite: z.coerce.boolean().default(false), allowAnySite: booleanSchema.default(false),
// should it trust reverse proxy headers? (for ip gathering) // should it trust reverse proxy headers? (for ip gathering)
trustProxy: z.coerce.boolean().default(false), trustProxy: booleanSchema.default(false),
// should it trust cloudflare headers? (for ip gathering, cloudflare has priority) // should it trust cloudflare headers? (for ip gathering, cloudflare has priority)
trustCloudflare: z.coerce.boolean().default(false), trustCloudflare: booleanSchema.default(false),
// prefix for where the instance is run on. for example set it to /backend if you're hosting it on example.com/backend // prefix for where the instance is run on. for example set it to /backend if you're hosting it on example.com/backend
// if this is set, do not apply url rewriting before proxing // if this is set, do not apply url rewriting before proxing
@ -30,7 +32,7 @@ export const configSchema = z.object({
format: z.enum(['json', 'pretty']).default('pretty'), format: z.enum(['json', 'pretty']).default('pretty'),
// show debug logs? // show debug logs?
debug: z.coerce.boolean().default(false), debug: booleanSchema.default(false),
}) })
.default({}), .default({}),
postgres: z.object({ postgres: z.object({
@ -38,19 +40,19 @@ export const configSchema = z.object({
connection: z.string(), connection: z.string(),
// run all migrations on boot of the application // run all migrations on boot of the application
migrateOnBoot: z.coerce.boolean().default(false), migrateOnBoot: booleanSchema.default(false),
// try to sync the schema on boot, useful for development // try to sync the schema on boot, useful for development
// will always keep the database schema in sync with the connected database // will always keep the database schema in sync with the connected database
// it is extremely destructive, do not use it EVER in production // it is extremely destructive, do not use it EVER in production
syncSchema: z.coerce.boolean().default(false), syncSchema: booleanSchema.default(false),
// Enable debug logging for MikroORM - Outputs queries and entity management logs // Enable debug logging for MikroORM - Outputs queries and entity management logs
// Do NOT use in production, leaks all sensitive data // Do NOT use in production, leaks all sensitive data
debugLogging: z.coerce.boolean().default(false), debugLogging: booleanSchema.default(false),
// Enable SSL for the postgres connection // Enable SSL for the postgres connection
ssl: z.coerce.boolean().default(false), ssl: booleanSchema.default(false),
}), }),
crypto: z.object({ crypto: z.object({
// session secret. used for signing session tokens // session secret. used for signing session tokens
@ -65,7 +67,7 @@ export const configSchema = z.object({
captcha: z captcha: z
.object({ .object({
// enabled captchas on register // enabled captchas on register
enabled: z.coerce.boolean().default(false), enabled: booleanSchema.default(false),
// captcha secret // captcha secret
secret: z.string().min(1).optional(), secret: z.string().min(1).optional(),
@ -76,7 +78,7 @@ export const configSchema = z.object({
ratelimits: z ratelimits: z
.object({ .object({
// enabled captchas on register // enabled captchas on register
enabled: z.coerce.boolean().default(false), enabled: booleanSchema.default(false),
redisUrl: z.string().optional(), redisUrl: z.string().optional(),
}) })
.default({}), .default({}),