The minimalistic document archiving platform.
refactor(config): introduce booleanishSchema for boolean coercion in config (#183)
Corentin Thomasseta0797beb148f
diff --git a/apps/papra-server/src/modules/app/auth/auth.config.ts b/apps/papra-server/src/modules/app/auth/auth.config.tsindex e8dfc900..4ba26078 100644--- a/apps/papra-server/src/modules/app/auth/auth.config.ts+++ b/apps/papra-server/src/modules/app/auth/auth.config.ts@@ -1,5 +1,6 @@import type { ConfigDefinition } from 'figue';import { z } from 'zod';+import { booleanishSchema } from '../../config/config.schemas';export const authConfig = {secret: {@@ -10,59 +11,33 @@ export const authConfig = {},isRegistrationEnabled: {doc: 'Whether registration is enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'true',+ schema: booleanishSchema,+ default: true,env: 'AUTH_IS_REGISTRATION_ENABLED',},isPasswordResetEnabled: {doc: 'Whether password reset is enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'true',+ schema: booleanishSchema,+ default: true,env: 'AUTH_IS_PASSWORD_RESET_ENABLED',},isEmailVerificationRequired: {doc: 'Whether email verification is required',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'AUTH_IS_EMAIL_VERIFICATION_REQUIRED',},showLegalLinksOnAuthPage: {doc: 'Whether to show Papra legal links on the auth pages (terms of service, privacy policy), useless for self-hosted instances',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'AUTH_SHOW_LEGAL_LINKS',},providers: {github: {isEnabled: {doc: 'Whether Github OAuth is enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,env: 'AUTH_PROVIDERS_GITHUB_IS_ENABLED',},clientId: {@@ -81,13 +56,8 @@ export const authConfig = {google: {isEnabled: {doc: 'Whether Google OAuth is enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'AUTH_PROVIDERS_GOOGLE_IS_ENABLED',},clientId: {diff --git a/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts b/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.tsindex 00730710..a0e78842 100644--- a/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts+++ b/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts@@ -1,5 +1,6 @@import type { Database } from '../../database/database.types';import { describe, expect, test } from 'vitest';+import { overrideConfig } from '../../../config/config.test-utils';import { createInMemoryDatabase } from '../../database/database.test-utils';import { createServer } from '../../server';@@ -8,7 +9,7 @@ describe('health check routes e2e', () => {describe('the /api/health is a publicly accessible route that provides health information about the server', () => {test('when the database is healthy, the /api/health returns 200', async () => {const { db } = await createInMemoryDatabase();- const { app } = await createServer({ db });+ const { app } = await createServer({ db, config: overrideConfig() });const response = await app.request('/api/health');@@ -27,7 +28,7 @@ describe('health check routes e2e', () => {},} as unknown as Database;- const { app } = await createServer({ db });+ const { app } = await createServer({ db, config: overrideConfig() });const response = await app.request('/api/health');diff --git a/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts b/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.tsindex b2ef53b5..4e1ca387 100644--- a/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts+++ b/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts@@ -1,11 +1,12 @@import { describe, expect, test } from 'vitest';+import { overrideConfig } from '../../../config/config.test-utils';import { createInMemoryDatabase } from '../../database/database.test-utils';import { createServer } from '../../server';describe('ping routes e2e', () => {test('the /api/ping is a publicly accessible route that always returns a 200 with a status ok', async () => {const { db } = await createInMemoryDatabase();- const { app } = await createServer({ db });+ const { app } = await createServer({ db, config: overrideConfig() });const response = await app.request('/api/ping');diff --git a/apps/papra-server/src/modules/config/config.schemas.test.ts b/apps/papra-server/src/modules/config/config.schemas.test.tsnew file mode 100644index 00000000..b5a38847--- /dev/null+++ b/apps/papra-server/src/modules/config/config.schemas.test.ts@@ -0,0 +1,27 @@+import { describe, expect, test } from 'vitest';+import { booleanishSchema } from './config.schemas';++describe('config schemas', () => {+ describe('booleanishSchema', () => {+ test('a zod schema that validates and coerces a string to a boolean, used in the config where we accept env variables and pojo values', () => {+ expect(booleanishSchema.parse(true)).toBe(true);+ expect(booleanishSchema.parse('true')).toBe(true);+ expect(booleanishSchema.parse('TRUE')).toBe(true);+ expect(booleanishSchema.parse('True')).toBe(true);+ expect(booleanishSchema.parse(' True ')).toBe(true);+ expect(booleanishSchema.parse('1')).toBe(true);+ expect(booleanishSchema.parse(1)).toBe(true);++ expect(booleanishSchema.parse('false')).toBe(false);+ expect(booleanishSchema.parse('FALSE')).toBe(false);+ expect(booleanishSchema.parse('False')).toBe(false);+ expect(booleanishSchema.parse(' False ')).toBe(false);+ expect(booleanishSchema.parse(false)).toBe(false);+ expect(booleanishSchema.parse('foo')).toBe(false);+ expect(booleanishSchema.parse('0')).toBe(false);+ expect(booleanishSchema.parse(-1)).toBe(false);+ expect(booleanishSchema.parse(0)).toBe(false);+ expect(booleanishSchema.parse(2)).toBe(false);+ });+ });+});diff --git a/apps/papra-server/src/modules/config/config.schemas.ts b/apps/papra-server/src/modules/config/config.schemas.tsnew file mode 100644index 00000000..dc25aca2--- /dev/null+++ b/apps/papra-server/src/modules/config/config.schemas.ts@@ -0,0 +1,9 @@+import { z } from 'zod';++export const booleanishSchema = z+ .coerce+ .string()+ .trim()+ .toLowerCase()+ .transform(x => ['true', '1'].includes(x))+ .pipe(z.boolean());diff --git a/apps/papra-server/src/modules/config/config.test-utils.ts b/apps/papra-server/src/modules/config/config.test-utils.tsindex 81c20ae4..875cace2 100644--- a/apps/papra-server/src/modules/config/config.test-utils.ts+++ b/apps/papra-server/src/modules/config/config.test-utils.ts@@ -5,7 +5,7 @@ import { loadDryConfig } from './config';export { overrideConfig };-function overrideConfig(config: DeepPartial<Config>) {+function overrideConfig(config: DeepPartial<Config> | undefined = {}) {const { config: defaultConfig } = loadDryConfig();return merge({}, defaultConfig, config);diff --git a/apps/papra-server/src/modules/config/config.ts b/apps/papra-server/src/modules/config/config.tsindex eaf349e0..15b433b8 100644--- a/apps/papra-server/src/modules/config/config.ts+++ b/apps/papra-server/src/modules/config/config.ts@@ -16,6 +16,7 @@ import { organizationPlansConfig } from '../plans/plans.config';import { createLogger } from '../shared/logger/logger';import { subscriptionsConfig } from '../subscriptions/subscriptions.config';import { tasksConfig } from '../tasks/tasks.config';+import { booleanishSchema } from './config.schemas';export const configDefinition = {env: {@@ -54,13 +55,8 @@ export const configDefinition = {},servePublicDir: {doc: 'Whether to serve the public directory',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'SERVER_SERVE_PUBLIC_DIR',},},diff --git a/apps/papra-server/src/modules/emails/emails.config.ts b/apps/papra-server/src/modules/emails/emails.config.tsindex d73bca9e..38b935ac 100644--- a/apps/papra-server/src/modules/emails/emails.config.ts+++ b/apps/papra-server/src/modules/emails/emails.config.ts@@ -1,5 +1,6 @@import type { ConfigDefinition } from 'figue';import { z } from 'zod';+import { booleanishSchema } from '../config/config.schemas';export const emailsConfig = {resendApiKey: {@@ -16,13 +17,8 @@ export const emailsConfig = {},dryRun: {doc: 'Whether to run the email service in dry run mode',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'EMAILS_DRY_RUN',},} as const satisfies ConfigDefinition;diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.config.tsindex 9d8646ac..5837cf8d 100644--- a/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts@@ -1,5 +1,6 @@import type { ConfigDefinition } from 'figue';import { z } from 'zod';+import { booleanishSchema } from '../config/config.schemas';import { intakeEmailDrivers } from './drivers/intake-emails.drivers';import { owlrelayIntakeEmailDriverConfig } from './drivers/owlrelay/owlrelay.intake-email-driver.config';import { RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME } from './drivers/random-username/random-username.intake-email-driver';@@ -8,13 +9,8 @@ import { randomUsernameIntakeEmailDriverConfig } from './drivers/random-usernameexport const intakeEmailsConfig = {isEnabled: {doc: 'Whether intake emails are enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'false',+ schema: booleanishSchema,+ default: false,env: 'INTAKE_EMAILS_IS_ENABLED',},driver: {diff --git a/apps/papra-server/src/modules/plans/plans.config.ts b/apps/papra-server/src/modules/plans/plans.config.tsindex 5615cfd4..b4f61357 100644--- a/apps/papra-server/src/modules/plans/plans.config.ts+++ b/apps/papra-server/src/modules/plans/plans.config.ts@@ -1,16 +1,12 @@import type { ConfigDefinition } from 'figue';import { z } from 'zod';+import { booleanishSchema } from '../config/config.schemas';export const organizationPlansConfig = {isFreePlanUnlimited: {doc: 'Whether the free plan is unlimited, meaning it has no limits on the number of documents, tags, and organizations, basically always true for self-hosted instances',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'true',+ schema: booleanishSchema,+ default: true,env: 'IS_FREE_PLAN_UNLIMITED',},plusPlanPriceId: {diff --git a/apps/papra-server/src/modules/tasks/tasks.config.ts b/apps/papra-server/src/modules/tasks/tasks.config.tsindex 2325ad58..baddf12f 100644--- a/apps/papra-server/src/modules/tasks/tasks.config.ts+++ b/apps/papra-server/src/modules/tasks/tasks.config.ts@@ -1,17 +1,13 @@import type { ConfigDefinition } from 'figue';import { z } from 'zod';+import { booleanishSchema } from '../config/config.schemas';export const tasksConfig = {hardDeleteExpiredDocuments: {enabled: {doc: 'Whether the task to hard delete expired "soft deleted" documents is enabled',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'true',+ schema: booleanishSchema,+ default: true,env: 'DOCUMENTS_HARD_DELETE_EXPIRED_DOCUMENTS_ENABLED',},cron: {@@ -22,13 +18,8 @@ export const tasksConfig = {},runOnStartup: {doc: 'Whether the task to hard delete expired "soft deleted" documents should run on startup',- schema: z- .string()- .trim()- .toLowerCase()- .transform(x => x === 'true')- .pipe(z.boolean()),- default: 'true',+ schema: booleanishSchema,+ default: true,env: 'DOCUMENTS_HARD_DELETE_EXPIRED_DOCUMENTS_RUN_ON_STARTUP',},},