npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@philiprehberger/env-validator

v0.2.3

Published

Schema-based environment variable validation with type-safe accessors

Readme

@philiprehberger/env-validator

Schema-based environment variable validation with type-safe accessors.

Installation

npm install @philiprehberger/env-validator

Usage

import { createEnv } from '@philiprehberger/env-validator';

const env = createEnv({
  DATABASE_URL: { type: 'url', required: true },
  PORT: { type: 'port', default: 3000 },
  NODE_ENV: { type: 'enum', values: ['development', 'production', 'test'] },
  ENABLE_CACHE: { type: 'boolean', default: false },
  ADMIN_EMAIL: { type: 'email', required: true },
  MAX_CONNECTIONS: { type: 'number', default: 10 },
  FEATURE_FLAGS: { type: 'json', default: {} },
});

env.DATABASE_URL; // string (typed, validated)
env.PORT;         // number
env.ENABLE_CACHE; // boolean

Supported Types

| Type | Parses to | Validation | |------|-----------|------------| | string | string | Any non-empty string | | number | number | Valid number | | boolean | boolean | true/false/1/0/yes/no | | url | string | Valid URL | | email | string | Valid email format | | enum | string | One of specified values | | port | number | Integer 0–65535 | | json | unknown | Valid JSON |

Error Reporting

All validation errors are collected and reported at once:

// If DATABASE_URL and ADMIN_EMAIL are both missing:
// EnvValidationError: Environment validation failed:
//   Missing required environment variable: DATABASE_URL
//   Missing required environment variable: ADMIN_EMAIL

Custom Source

const env = createEnv(schema, {
  DATABASE_URL: 'postgres://localhost/mydb',
  PORT: '8080',
});

API Reference

createEnv<S extends Schema>(schema: S, source?: Record<string, string | undefined>): InferEnv<S>

Validates environment variables against the given schema and returns a type-safe object. Throws EnvValidationError if any validation fails. If source is omitted, reads from process.env.

Schema

A record mapping variable names to FieldConfig objects:

type Schema = Record<string, FieldConfig>;

FieldConfig

| Property | Type | Description | |----------|------|-------------| | type | 'string' \| 'number' \| 'boolean' \| 'url' \| 'email' \| 'enum' \| 'port' \| 'json' | Validation type. | | required | boolean | Whether the variable must be present. Default: false. | | default | Varies by type | Default value if missing. Makes the field effectively required in the output type. | | values | readonly string[] | (enum only) Allowed values. |

EnvValidationError

Thrown when one or more variables fail validation.

| Property | Type | Description | |----------|------|-------------| | message | string | Human-readable summary of all failures. | | errors | string[] | Array of individual error messages. |

InferEnv<S>

TypeScript utility type that infers the output type from a schema. Required fields and fields with defaults are non-optional; others are optional.

License

MIT