Skip to content
Best Practiceswarn by defaultrecommended presetastro-doctor/prefer-env-schema

prefer-env-schema

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

Why

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.

Examples

Incorrect

Documented variables without a schema

text
# .env.example
DATABASE_URL=
PUBLIC_API_URL=

Correct

Validate documented variables in astro.config.ts

typescript
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' }),
    },
  },
})

Configuration

Project audits are configured in doctor.config.ts:

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

All rules