Skip to contentView the source on GitHub

prefer-env-schema

Define an Astro env schema for variables documented in .env.example.

Best Practiceswarning by default Project auditrecommended audit

astro-doctor/prefer-env-schema

Why it matters

An env schema validates required variables at startup and generates TypeScript types for server and client imports. Without one, documented variables can be missing, malformed, or used under the wrong name without an early error.

See the difference

Incorrect

Documented variables without a schema

textIncorrect example
# .env.example
DATABASE_URL=
PUBLIC_API_URL=

Correct

Validate documented variables in astro.config.ts

typescriptCorrect example
import { defineConfig, envField } from 'astro/config'

export default defineConfig({
  env: {
    schema: {
      DATABASE_URL: envField.string({ context: 'server', access: 'secret' }),
      PUBLIC_API_URL: envField.string({ context: 'client', access: 'public' }),
    },
  },
})

Configure this check

Project audits are configured in doctor.config.ts:

typescriptdoctor.config.ts
export default {
  rules: {
    'astro-doctor/prefer-env-schema': 'error',
  },
}

Use 'error', 'warn', or 'off'. The configuration guide covers presets and per-file overrides.

Keep exploring best practices

Browse every rule and project audit