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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@profusion/validated-env-schema

v0.1.1

Published

Validate environment variables against JSON Schema7

Downloads

113

Readme

Validated Environments using JSON Schema

This script eases the usage of environment variables in your applications by using Ajv in coerce mode.

It will take care to check the variable in various complex formats, including arrays and objects, applying all of the JSON Schema rules, which includes:

  • required properties
  • default values
  • minimum and maximum elements in an array
  • string patterns

The sanitized values are serialized to strings and are written back to the variable, while the invalid variables are deleted. One can trust only valid variables exist after the process finishes.

The parse, serialize and post-validate of properties can be customized, this allows non-JSON encoding to be supported (such as comma-separated lists, booleans using 'y/n', etc).

The resulting object will have TypeScript signature based on the JSON schema, so prop: { type: 'number' } results in prop: number.

JSON types can be converted to high-level types with convert functions, they will also define the type of the resulting object. For instance the JSON type can be a string (ISO8601 formatted date) while the converted type can be Date, this will reflect in the TypeScript object. (Note that the converted values are NOT serialized/written back to the container/process.env)

Install

yarn add @profusion/validated-env-schema

Usage

import validateEnvVars from '@profusion/validated-env-schema';

const values = validateEnvVars({
  properties: {
    AUTH_TOKEN: { type: 'string' },
    TTL: { type: 'number' },
  },
  required: ['AUTH_TOKEN'],
  type: 'object',
} as const);
console.log('values:', values.AUTH_TOKEN, values.TTL);
// values.AUTH_TOKEN is type: string
// values.TTL is type: number | undefined (not int required array)

See more complete usage example.

NOTE: This package doesn't load the environment variables from files, they must be present when the validation call is executed.

Debug messages can be enabled with VALIDATED_ENV_SCHEMA_DEBUG=true, this will print out extensive debug.

JSON Schema String Formats

In order to use { "type": "string", "format": "..."}, one must install ajv-formats:

yarn add ajv-formats

Usage with dotenv and others

The packages such as dotenv can be used with this software without any specific configuration, just require those packages, either explicit or using node -r dotenv/config app.js

Similar Packages

This package is similar to fastify/env-schema, as it uses Ajv in the same way, but the usage interface is different.

This project's validateEnvVars() takes an optional customize object that can handle parse, serialization, post-validation and conversion to native type for each property. It will also properly type the returned object, will remove invalid properties and throw errors.

License

Open source - MIT.