Skip to main content

Schema Types

Schemas are useful but not required, envitron loads all envs from a file and those envs can still be used, only they won't be type safe Envitron supports the following schema types for environment variables:

String

schema.string();
schema.string({ optional: true });

Number

schema.number();
schema.number({ optional: true });

Boolean

schema.boolean();
schema.boolean({ optional: true });

Enum

schema.enum(['development', 'production', 'test'] as const);

Array

schema.array();
schema.array({ optional: true });

Custom

schema.custom((value) => {
// Your custom validation logic here
return transformedValue;
});

schema.custom(
(value) => {
// Your custom validation logic here
return transformedValue;
},
{ optional: true }
);

The custom validator allows you to define your own validation and transformation logic for environment variables. The validator function receives the raw environment value and should return the transformed value. The type of the env will be inferred from the return type of the function.

Example

const env = createEnvSchema((schema) => ({
// Divide the environment value by 2
CUSTOM_NUMBER: schema.custom((value) => Number(value) / 2),

// Transform a string to uppercase
CUSTOM_STRING: schema.custom((value) => String(value).toUpperCase()),

// Optional custom validator
OPTIONAL_CUSTOM: schema.custom((value) => Number(value) * 2, { optional: true }),
}));

Options

  • optional: true — makes the variable optional (value may be undefined)

Example

const env = createEnvSchema((schema) => ({
HOST: schema.string(),
PORT: schema.number(),
DEBUG: schema.boolean({ optional: true }),
NODE_ENV: schema.enum(['development', 'production', 'test'] as const),
ALLOWED_HOSTS: schema.array({ optional: true }),
CUSTOM_VALUE: schema.custom((value) => Number(value) / 2),
}));

See API Reference for usage details.