dc63637a28c5cad7856872dabea7bae487836b82
Some checks failed
CI / test (push) Has been cancelled
- Created missing schema-validator.ts file - Added enum type validation with options support - Added 'yes'/'no' boolean value handling - Fixed port type detection to take priority over number
dotenv-types
A TypeScript library for type-safe environment variable parsing and validation.
Features
- Type-safe environment variable parsing
- Support for multiple variable types: string, number, boolean, port, email, url, json, enum
- Schema-based validation
- Automatic type inference
- Cleaned environment variables output
Installation
npm install dotenv-types
Usage
import { parseEnvSchema } from 'dotenv-types';
const schema = {
PORT: { type: 'port', required: true },
DB_HOST: { type: 'string', required: true },
DB_PORT: { type: 'port', required: true },
DEBUG: { type: 'boolean', default: false },
LOG_LEVEL: { type: 'enum', options: ['debug', 'info', 'warn', 'error'], default: 'info' }
};
const result = parseEnvSchema(schema);
if (result.isValid) {
console.log('Cleaned env:', result.cleanedEnv);
} else {
console.log('Validation errors:', result.errors);
}
API
parseEnvSchema(schema)
Parses environment variables according to the provided schema.
Parameters:
schema: An object defining expected environment variables
Returns: ValidationResult with:
isValid: Boolean indicating if all validations passederrors: Array of validation errorscleanedEnv: Object with cleaned/typed environment variables
Supported Types
string- Any string valuenumber- Numeric valuesboolean- true/false, 1/0, yes/noport- Valid port numbers (1-65535)email- Valid email addressesurl- Valid URLsjson- Valid JSON stringsenum- Value must match one of the provided options
License
MIT
Languages
TypeScript
96.3%
JavaScript
3.7%