GithubHelp home page GithubHelp logo

envzo's Introduction

Envzo

Name:

  • environment
  • zod

Heavily inspired by...

I wanted to create something almost the same as envsafe and envalid, except I disliked the fact that imports were global.

For example, one would import { str } from 'envalid' when I felt that would clog my global imports.

I set out to create a simple and modular alternative to envsafe and envalid that would eliminate those global variables, allow validators to be built and reused, and allow string interpolation with types.

Note: Some portions of my code, such as the built-in validators, was pulled directly from envsafe! KATT's code is excellent and I love everything he works on!

Usage

import { envzo } from 'envzo';

// Pull port number from PORT environment variable
const env = envzo.parse(process.env, ({ port }) => ({
    listenPort: port({ key: 'PORT', default: 1234 })
}))

console.log(env.listenPort);

String templating can be used.

import { envzo } from 'envzo';

const env = envzo.parse(process.env, v => ({
    mongo: `mongodb://${v.host({ key: 'MONGO_HOST' })}/${v.string({ key: 'MONGO_DB' })}`
}))

You can also define custom validators.

import { Envzo } from 'envzo';

export const envzo = new Envzo({
    validators: {
        powerOfTwo: Envzo.makeValidator<number>(({ input, errors, parse }) => {
            // Use the `number` validator to parse into a number
            const num = parse.number(input);
            if (Math.log2(num) % 1 !== 0) {
                // not a power of two!
                throw errors.invalid('Power of Two', input);
            }
            return num;
        })
    }
});

const env = envzo.parse(process.env, v => ({
    num: v.powerOfTwo({ key: 'NUMBER' })
}))

// num is a power of two
const { num } = env;

envzo's People

Contributors

cseitz avatar

Watchers

 avatar

envzo's Issues

OR validator

  • Catch errors from validators within
  • The first one to validate successfully is used
  • If none validate successfully, all captured errors will be thrown

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.