GithubHelp home page GithubHelp logo

jimmyhurrah / env-schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fastify/env-schema

0.0 0.0 0.0 28 KB

Validate your env variable using Ajv and dotenv

License: MIT License

JavaScript 88.23% Shell 0.24% TypeScript 11.53%

env-schema's Introduction

env-schema

Greenkeeper badge Build Status JavaScript Style Guide

Utility to check environment variables using JSON schema, Ajv and dotenv.

Install

npm install --save env-schema

Usage

const envSchema = require('env-schema')

const schema = {
  type: 'object',
  required: [ 'PORT' ],
  properties: {
    PORT: {
      type: 'string',
      default: 3000
    }
  }
}

const config = envSchema({
  schema: schema,
  data: data // optional, default: process.env
  dotenv: true // load .env if it's there, default: false
})

console.log(config)
// output: { PORT: 3000 }

It is possible to also use fluent-schema:

const envSchema = require('env-schema')
const S = require('fluent-schema')

const config = envSchema({
  schema: S.object().prop('port', S.string().default('3000').required()),
  data: data // optional, default: process.env
  dotenv: true // load .env if it's there, default: false
})

console.log(config)
// output: { PORT: 3000 }

NB: internally this plugin force to not have additional properties, so the additionalProperties flag is forced to be false

Custom keywords

This library supports the following Ajv custom keywords:

separator

Type: string

Applies to type: string

When present, the value provided will be split based by this value.

Example:

const envSchema = require('env-schema')

const schema = {
  type: 'object',
  required: [ 'ALLOWED_HOSTS' ],
  properties: {
    ALLOWED_HOSTS: {
      type: 'string',
      separator: ','
    }
  }
}

const data = {
  ALLOWED_HOSTS: '127.0.0.1,0.0.0.0'
}

const config = envSchema({
  schema: schema,
  data: data, // optional, default: process.env
  dotenv: true // load .env if it's there, default: false
}) 

// config.data => ['127.0.0.1', '0.0.0.0']

Acknowledgements

Kindly sponsored by Mia Platform and NearForm.

License

MIT

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.