Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing
Step 5 of 7Standard Schema runtime config

Validated Runtime Config: Fail at Boot, Not in Checkout

Use a Standard Schema in runtimeConfig to declare required values, enums, and defaults, let Nuxt convert environment variables to the right types, and get one startup error that lists everything wrong.

Runtime config used to be typed from its defaults. A required secret was impossible to declare, an enum was a comment, and environment variables were cast with destr. Nuxt 4.7 fixes the type system and the boot.

What's new#

Any value in runtimeConfig can be a Standard Schema, such as Zod, ArkType, or Valibot:

nuxt.config.ts
import { z } from 'zod'

export default defineNuxtConfig({
  runtimeConfig: {
    apiSecret: z.string(), // required, so it must be set at runtime
    session: z.object({
      maxAge: z.number().int().default(60 * 60),
      secure: z.boolean().default(true),
    }),
    public: {
      mode: z.enum(['preview', 'production']).default('production'),
    },
  },
})

The schema sets the type, and its default is the default value. Environment variables are converted to that type when the server starts. If a value is missing or cannot be converted, the server does not start, and you get one error listing everything that is wrong.

The schema itself never ends up in your server bundle. Nuxt compiles it at build time into a tiny description — types, required values, enums, unions — and the runtime checks against that description.

Why you'd care#

A missing NUXT_API_SECRET should stop a deploy, not surface as undefined in a payment call three hours later. An enum should be an enum, not a string you validate in five places.

This is the difference between "typed" and "validated". TypeScript tells you the shape at build time. A schema tells you the value at boot time.

Before#

nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    apiSecret: '',
    mode: 'production',
  },
});

The type is string, the default is empty, and the only thing that notices a missing secret is the code that uses it.

After#

ts
// Required. No default, so the server refuses to start without it.
apiSecret: z.string(),

// Defaulted. The default is the value, not a fake empty string.
mode: z.enum(['preview', 'production']).default('production'),

// Nested. The environment variable is converted, not destr-cast.
session: z.object({
  maxAge: z.number().int().default(3600),
  secure: z.boolean().default(true),
}),

A bad boot looks like this:

terminal
NUXT_E8014: runtime config validation failed
  apiSecret: Required
  session.maxAge: Expected number, received "forever"

One error, every problem. Fix the environment, restart.

Environment variables#

Nuxt maps environment variables to the schema by path:

bash
NUXT_API_SECRET=...
NUXT_PUBLIC_MODE=preview
NUXT_SESSION_MAX_AGE=7200
NUXT_SESSION_SECURE=true

The values are converted to the schema's type. "7200" becomes the number 7200; "true" becomes the boolean true. That is a change from destr, which had surprising opinions about strings that looked like numbers.

With Nitro v3, runtime config environment variables are applied once when the server starts, not again for each request. The object useRuntimeConfig() returns without an event is not frozen. Treat it as read-only.

Modules can declare schemas too#

A module that contributes runtime config can use updateRuntimeConfig() with a schema:

module.ts
updateRuntimeConfig({
  myModule: {
    endpoint: z.string().url(),
    retries: z.number().int().min(0).default(2),
  },
});

The app gets the same boot-time validation for module options. No more "the module assumes endpoint exists" documentation.

Do it yourself#

  1. Pick one runtime config value that must exist.
  2. Replace its default with z.string().
  3. Remove the env var and run nuxt dev. The server should refuse to start with NUXT_E8014.
  4. Add the env var. The server should start.
  5. Add an enum and a defaulted number. Check the types in useRuntimeConfig().
  6. Add a nested object. Set one field from the environment.

Gotchas#

  • The schema is compiled, not shipped. Do not rely on custom refinements that need the full schema library at runtime. Nuxt keeps types, required values, enums, and unions.
  • The validation list is intentionally short. The goal is a fast boot, not a full JSON Schema engine.
  • z.string() with no default is required. If you want an optional secret, use .optional() and handle the absence explicitly.
  • Environment variables are converted, not cast. A string that cannot become a number is a boot error, not a silent NaN.
  • Nitro v3 applies env once. Do not mutate runtime config per request; it is shared.
  • Public values are public. A schema on public validates the value, it does not hide it.

Recap#

Runtime config is now a contract: a Standard Schema, a default, an environment conversion, and a boot that fails with every problem at once. The schema is compiled out of the bundle, so the cost is at build time, where it belongs.

Next: staticRoutes and the Client Diet.