New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

env-schema

Package Overview
Dependencies
Maintainers
13
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env-schema - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

test/custom-ajv.test.js

12

index.d.ts

@@ -0,1 +1,3 @@

import Ajv, { KeywordDefinition } from "ajv";
export type EnvSchemaData = {

@@ -10,9 +12,13 @@ [key: string]: unknown;

dotenv?: boolean | object;
ajv?: Ajv;
};
declare function loadAndValidateEnvironment(
_opts?: EnvSchemaOpt
): EnvSchemaData;
declare const loadAndValidateEnvironment: {
(_opts?: EnvSchemaOpt): EnvSchemaData;
keywords: {
separator: KeywordDefinition;
}
}
export default loadAndValidateEnvironment;
export { loadAndValidateEnvironment as envSchema };
'use strict'
const Ajv = require('ajv').default
const Ajv = require('ajv')

@@ -44,7 +44,8 @@ const separator = {

dotenv: { type: ['boolean', 'object'], default: false },
expandEnv: { type: ['boolean'], default: false }
expandEnv: { type: ['boolean'], default: false },
ajv: { type: 'object', additionalProperties: true }
}
}
const ajv = new Ajv({
const sharedAjvInstance = new Ajv({
allErrors: true,

@@ -58,3 +59,3 @@ removeAdditional: true,

const optsSchemaValidator = ajv.compile(optsSchema)
const optsSchemaValidator = sharedAjvInstance.compile(optsSchema)

@@ -97,2 +98,4 @@ function loadAndValidateEnvironment (_opts) {

const ajv = opts.ajv == null ? sharedAjvInstance : opts.ajv
const valid = ajv.validate(schema, merge)

@@ -111,1 +114,2 @@ if (!valid) {

module.exports.envSchema = loadAndValidateEnvironment
module.exports.envSchema.keywords = { separator }
{
"name": "env-schema",
"version": "3.0.1",
"version": "3.1.0",
"description": "Validate your env variable using Ajv and dotenv",
"main": "index.js",
"scripts": {
"test": "standard | snazzy && tap test/*.test.js && npm run typescript",
"test:ci": "standard | snazzy && tap test/*.test.js --coverage-report=lcovonly && npm run typescript",
"test": "standard | snazzy && tap test/*.test.js --no-check-coverage && npm run typescript",
"test:ci": "standard | snazzy && tap test/*.test.js --no-check-coverage --coverage-report=lcovonly && npm run typescript",
"typescript": "tsd"

@@ -31,12 +31,12 @@ },

"devDependencies": {
"fluent-json-schema": "^2.0.4",
"fluent-json-schema": "^3.0.0",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^16.0.0",
"tap": "^14.11.0",
"tsd": "^0.14.0"
"tap": "^15.0.2",
"tsd": "^0.17.0"
},
"dependencies": {
"ajv": "^7.1.1",
"dotenv": "^8.2.0",
"ajv": "^8.0.0",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0"

@@ -43,0 +43,0 @@ },

# env-schema
![CI workflow](https://github.com/fastify/env-schema/workflows/CI%20workflow/badge.svg)
![CI](https://github.com/fastify/env-schema/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/env-schema.svg?style=flat)](https://www.npmjs.com/package/env-schema)

@@ -9,3 +9,3 @@ [![Known Vulnerabilities](https://snyk.io/test/github/fastify/env-schema/badge.svg)](https://snyk.io/test/github/fastify/env-schema)

Utility to check environment variables using [JSON schema](https://json-schema.org/), [Ajv](http://npm.im/ajv) and
Utility to check environment variables using [JSON schema](https://json-schema.org/), [Ajv](http://npm.im/ajv), and
[dotenv](http://npm.im/dotenv).

@@ -37,4 +37,4 @@

schema: schema,
data: data // optional, default: process.env
dotenv: true // load .env if it's there, default: false
data: data, // optional, default: process.env
dotenv: true // load .env if it is there, default: false
})

@@ -46,6 +46,40 @@

It is possible to also use [fluent-json-schema](http://npm.im/fluent-json-schema):
Optionally, the user can supply their own ajv instance:
```js
const envSchema = require('env-schema')
const Ajv = require('ajv')
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'number',
default: 3000
}
}
}
const config = envSchema({
schema: schema,
data: data,
dotenv: true,
ajv: new Ajv({
allErrors: true,
removeAdditional: true,
useDefaults: true,
coerceTypes: true,
allowUnionTypes: true
})
})
console.log(config)
// output: { PORT: 3000 }
```
It is also possible to use [fluent-json-schema](http://npm.im/fluent-json-schema):
```js
const envSchema = require('env-schema')
const S = require('fluent-json-schema')

@@ -56,3 +90,3 @@

data: data, // optional, default: process.env
dotenv: true, // load .env if it's there, default: false
dotenv: true, // load .env if it is there, default: false
expandEnv: true, // use dotenv-expand, default: false

@@ -65,3 +99,3 @@ })

**NB:** support for additional properties in the schema is disabled for this plugin, with the `additionalProperties` flag set to `false` internally.
**NB** Support for additional properties in the schema is disabled for this plugin, with the `additionalProperties` flag set to `false` internally.

@@ -76,3 +110,3 @@ ### Custom keywords

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

@@ -101,3 +135,3 @@ Example:

data: data, // optional, default: process.env
dotenv: true // load .env if it's there, default: false
dotenv: true // load .env if it is there, default: false
})

@@ -108,2 +142,36 @@

The ajv keyword definition objects can be accessed through the property `keywords` on the `envSchema` function:
```js
const envSchema = require('env-schema')
const Ajv = require('ajv')
const schema = {
type: 'object',
properties: {
names: {
type: 'string',
separator: ','
}
}
}
const config = envSchema({
schema: schema,
data: data,
dotenv: true,
ajv: new Ajv({
allErrors: true,
removeAdditional: true,
useDefaults: true,
coerceTypes: true,
allowUnionTypes: true,
keywords: [envSchema.keywords.separator]
})
})
console.log(config)
// output: { PORT: 3000 }
```
## Acknowledgements

@@ -110,0 +178,0 @@

@@ -216,3 +216,3 @@ 'use strict'

isOk: false,
errorMessage: 'should have required property \'PORT\''
errorMessage: 'must have required property \'PORT\''
},

@@ -232,3 +232,3 @@ {

isOk: false,
errorMessage: 'should NOT have fewer than 1 items,should be object,should match exactly one schema in oneOf'
errorMessage: 'must NOT have fewer than 1 items,must be object,must match exactly one schema in oneOf'
},

@@ -309,3 +309,3 @@ {

isOk: false,
errorMessage: 'should have required property \'ALLOWED_HOSTS\''
errorMessage: 'must have required property \'ALLOWED_HOSTS\''
},

@@ -326,5 +326,5 @@ {

isOk: false,
errorMessage: `should have required property 'A'
should have required property 'B'
should have required property 'C'`
errorMessage: `must have required property 'A'
must have required property 'B'
must have required property 'C'`
}

@@ -331,0 +331,0 @@ ]

import { expectError, expectType } from "tsd";
import envSchema, { EnvSchemaData, EnvSchemaOpt, envSchema as envSchemaNamed, default as envSchemaDefault } from "../..";
import Ajv, { KeywordDefinition } from 'ajv'

@@ -58,1 +59,7 @@ const schema = {

expectType<EnvSchemaOpt>(optWithDotEnvOpt);
const optWithAjvInstance: EnvSchemaOpt = {
ajv: new Ajv()
};
expectType<EnvSchemaOpt>(optWithAjvInstance)
expectType<KeywordDefinition>(envSchema.keywords.separator)

Sorry, the diff of this file is not supported yet

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