toeverything/AFFiNE · critical

Invalid config for module [${module}] with key [${key}] Valu

Error message

Invalid config for module [${module}] with key [${key}]
Value: ${JSON.stringify(defaultValue)}
Error: ${issue.message}

What it means

Thrown by getDefaultConfig() during server startup when a configuration value fails its Zod validation schema. The error message includes the module name, config key, the invalid value (JSON-serialized), and the specific Zod issue message. This is a fatal startup error — the server cannot boot with invalid configuration.

Source

Thrown at packages/backend/server/src/base/config/register.ts:370

  for (const [module, defs] of Object.entries(APP_CONFIG_DESCRIPTORS)) {
    const modulizedConfig = {};

    for (const [key, desc] of Object.entries(defs)) {
      let defaultValue = desc.default;

      if (desc.env) {
        const [env, parser] = desc.env;
        const envValue = envs[env];
        if (envValue) {
          defaultValue = parseEnvValue(envValue, parser);
        }
      }

      const { success, error } = desc.validate(defaultValue);

      if (!success) {
        throw new Error(
          error.issues
            .map(issue => {
              return `Invalid config for module [${module}] with key [${key}]
Value: ${JSON.stringify(defaultValue)}
Error: ${issue.message}`;
            })
            .join('\n')
        );
      }

      set(modulizedConfig, key, defaultValue);
    }

    // @ts-expect-error all keys are known
    config[module] = modulizedConfig;
  }

  CONFIG_JSON_PATHS.forEach(path => {

View on GitHub (pinned to 26c515e050)

Solutions

  1. Read the error message: it names the module, key, the bad value, and the Zod issue — fix the value to match the expected type/schema.
  2. Check the corresponding env var or JSON config path for the failing key and correct the value.
  3. Consult the config descriptor (APP_CONFIG_DESCRIPTORS) for the expected type and constraints of that key.
  4. After fixing, restart the server to re-run config validation.

Example fix

// before (.env)
MAILER_HOST="not a url"

// The Zod validation for this key expects a valid hostname/URL.
// after
MAILER_HOST="smtp://smtp.example.com:587"
Defensive patterns

Strategy: validation

Validate before calling

// Before starting the server, validate config in a dry-run:
// import { getDefaultConfig } from './config/register';
// try { getDefaultConfig(); } catch (e) { console.error(e.message); process.exit(1); }
//
// Or validate individual env vars:
function validateEnvVar(key: string, value: string | undefined, validator: (v: string) => boolean) {
  if (value && !validator(value)) {
    throw new Error(`Invalid value for ${key}: ${value}`);
  }
}
validateEnvVar('AFFINE_SERVER_PORT', process.env.AFFINE_SERVER_PORT, v => /^\d+$/.test(v));

Try / catch

try {
  const config = getDefaultConfig();
  // start server with config
} catch (e) {
  console.error('Configuration validation failed:', e.message);
  process.exit(1);
}

Prevention

When it happens

Trigger: Server boot when process.env or config JSON provides a value for a config key that fails desc.validate (a Zod safeParse). For example, an env var meant to be a URL that contains a non-URL string, or a numeric port that's actually a word. The error fires in the loop over APP_CONFIG_DESCRIPTORS.

Common situations: Typo in an environment variable value (e.g. PORT=abc instead of a number). JSON config override file with wrong types. Missing required config with an invalid default. Deploying with stale .env files from a previous version where the expected schema changed.

Related errors


AI-assisted analysis of toeverything/AFFiNE@26c515e050 (2026-08-12). Data as JSON: /api/errors/0ee9e267c2da2064. Report an issue: GitHub.