Skip to main content
Version: Next

wasp/env

Helper for defining the validation schemas of your app's environment variables.

Functions​

defineEnvValidationSchema()​

defineEnvValidationSchema<Schema>(schema): Schema

Defines a Zod schema to validate your client or server env vars.

It returns the schema unchanged, and only exists to make sure the schema is type-checked. Export the schema from a file in src/ and pass it to the client.envValidationSchema or server.envValidationSchema field of the app spec.

Wasp merges your schema with the built-in validation schema of the Wasp-defined env vars, and exposes the validated result as the env object from wasp/client and wasp/server.

Type Parameters​

Schema​

Schema extends ZodObject<$ZodLooseShape, $strip>

Parameters​

schema​

Schema

A Zod object schema describing your env vars.

Returns​

Schema

Example​

// src/env.ts
import * as z from "zod";
import { defineEnvValidationSchema } from "wasp/env";

export const serverEnvValidationSchema = defineEnvValidationSchema(
z.object({
STRIPE_API_KEY: z.string({ error: "STRIPE_API_KEY is required." }),
}),
);

export const clientEnvValidationSchema = defineEnvValidationSchema(
z.object({
REACT_APP_ANALYTICS_ID: z.string({
error: "REACT_APP_ANALYTICS_ID is required.",
}),
REACT_APP_NAME: z.string().default("TODO App"),
}),
);
// main.wasp.ts
import { app } from "@wasp/spec";
import {
clientEnvValidationSchema,
serverEnvValidationSchema,
} from "./src/env" with { type: "ref" };

export default app({
name: "myApp",
client: {
envValidationSchema: clientEnvValidationSchema,
},
server: {
envValidationSchema: serverEnvValidationSchema,
},
// ...
});

See​

Custom env var validations