Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

env-schema

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env-schema

Validate your env variable using Ajv and dotenv

  • 6.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
166K
decreased by-3.06%
Maintainers
0
Weekly downloads
 
Created

What is env-schema?

The env-schema npm package is used to validate environment variables against a schema. It ensures that the environment variables are present and conform to the expected types and constraints, providing a robust way to manage configuration in Node.js applications.

What are env-schema's main functionalities?

Schema Validation

This feature allows you to define a schema for your environment variables and validate them. The code sample demonstrates how to define a schema with required properties and default values, and then validate the environment variables against this schema.

const envSchema = require('env-schema');
const schema = {
  type: 'object',
  required: ['PORT', 'HOST'],
  properties: {
    PORT: {
      type: 'number',
      default: 3000
    },
    HOST: {
      type: 'string',
      default: 'localhost'
    }
  }
};
const config = envSchema({ schema: schema });
console.log(config);

Default Values

This feature allows you to specify default values for environment variables. If the environment variable is not set, the default value will be used. The code sample shows how to set default values for the PORT and HOST environment variables.

const envSchema = require('env-schema');
const schema = {
  type: 'object',
  properties: {
    PORT: {
      type: 'number',
      default: 3000
    },
    HOST: {
      type: 'string',
      default: 'localhost'
    }
  }
};
const config = envSchema({ schema: schema });
console.log(config);

Type Coercion

This feature allows you to coerce environment variables to the specified types. The code sample demonstrates how to coerce the PORT environment variable to a number and the DEBUG environment variable to a boolean.

const envSchema = require('env-schema');
const schema = {
  type: 'object',
  properties: {
    PORT: {
      type: 'number'
    },
    DEBUG: {
      type: 'boolean'
    }
  }
};
process.env.PORT = '3000';
process.env.DEBUG = 'true';
const config = envSchema({ schema: schema });
console.log(config);

Other packages similar to env-schema

Keywords

FAQs

Package last updated on 23 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc