env-schema
Advanced tools
Comparing version 4.1.0 to 4.2.0
15
index.js
@@ -15,4 +15,14 @@ 'use strict' | ||
errors: false, | ||
compile: (schema) => (data, { parentData: pData, parentDataProperty: pDataProperty }) => { | ||
pData[pDataProperty] = data === '' ? [] : data.split(schema) | ||
compile: (schema) => (data, dataPath, parentData, parentDataProperty) => { | ||
// In some cases parentData and parentDataProperty will be undefined. | ||
// We need to fall back to the dataPath object to provide those values. | ||
if (parentData && parentDataProperty) { | ||
parentData[parentDataProperty] = data === '' ? [] : data.split(schema) | ||
} else { | ||
const { | ||
parentData: pData, | ||
parentDataProperty: pDataProperty | ||
} = dataPath | ||
pData[pDataProperty] = data === '' ? [] : data.split(schema) | ||
} | ||
} | ||
@@ -70,3 +80,2 @@ } | ||
/* istanbul ignore else */ | ||
if (opts.env) { | ||
@@ -73,0 +82,0 @@ if (opts.expandEnv) { |
{ | ||
"name": "env-schema", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Validate your env variable using Ajv and dotenv", | ||
"main": "index.js", | ||
"types": "types/index.d.ts", | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"test": "npm run test:unit && npm run test:typescript", | ||
"test:unit": "tap", | ||
"test:typescript": "tsd" | ||
"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" | ||
}, | ||
@@ -32,21 +30,19 @@ "repository": { | ||
"homepage": "https://github.com/fastify/env-schema#readme", | ||
"dependencies": { | ||
"ajv": "^8.0.0", | ||
"dotenv": "^16.0.0", | ||
"dotenv-expand": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@sinclair/typebox": "^0.24.43", | ||
"ajv-formats": "^2.1.1", | ||
"fluent-json-schema": "^3.0.0", | ||
"pre-commit": "^1.2.2", | ||
"snazzy": "^9.0.0", | ||
"standard": "^17.0.0", | ||
"standard": "^16.0.0", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.24.1" | ||
"tsd": "^0.19.0" | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
] | ||
"dependencies": { | ||
"ajv": "^8.0.0", | ||
"dotenv": "^16.0.0", | ||
"dotenv-expand": "^8.0.3" | ||
}, | ||
"tsd": { | ||
"directory": "test/types" | ||
} | ||
} |
# env-schema | ||
[![CI](https://github.com/fastify/env-schema/workflows/CI/badge.svg)](https://github.com/fastify/env-schema/actions/workflows/ci.yml) | ||
![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) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/env-schema/badge.svg)](https://snyk.io/test/github/fastify/env-schema) | ||
[![Coverage Status](https://coveralls.io/repos/github/fastify/env-schema/badge.svg?branch=master)](https://coveralls.io/github/fastify/env-schema?branch=master) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
@@ -13,3 +15,3 @@ | ||
``` | ||
npm i env-schema | ||
npm install --save env-schema | ||
``` | ||
@@ -113,3 +115,3 @@ | ||
const config = envSchema({ | ||
schema: S.object().prop('PORT', S.number().default(3000).required()), | ||
schema: S.object().prop('port', S.number().default(3000).required()), | ||
data: data, // optional, default: process.env | ||
@@ -159,3 +161,3 @@ dotenv: true, // load .env if it is there, default: false | ||
dotenv: true // load .env if it is there, default: false | ||
}) | ||
}) | ||
@@ -204,3 +206,3 @@ // config.data => ['127.0.0.1', '0.0.0.0'] | ||
```ts | ||
import { envSchema, JSONSchemaType } from 'env-schema' | ||
import envSchema from 'env-schema'; | ||
@@ -211,3 +213,3 @@ interface Env { | ||
const schema: JSONSchemaType<Env> = { | ||
const schema = { | ||
type: 'object', | ||
@@ -223,24 +225,7 @@ required: [ 'PORT' ], | ||
const config = envSchema({ | ||
schema | ||
const config = envSchema<Env>({ | ||
schema, | ||
}) | ||
``` | ||
You can also use a `JSON Schema` library like `typebox`: | ||
```ts | ||
import { envSchema } from 'env-schema' | ||
import { Static, Type } from '@sinclair/typebox' | ||
const schema = Type.Object({ | ||
PORT: Type.Number({ default: 3000 }) | ||
}) | ||
type Schema = Static<typeof schema> | ||
const config = envSchema<Schema>({ | ||
schema | ||
}) | ||
``` | ||
If no type is specified the `config` will have the `EnvSchemaData` type. | ||
@@ -247,0 +232,0 @@ |
Sorry, the diff of this file is not supported yet
32354
7
15
1009
241
+ Addeddotenv-expand@8.0.3(transitive)
- Removeddotenv-expand@9.0.0(transitive)
Updateddotenv-expand@^8.0.3