From b35598449068a21faf6eb8ea77d08b26751488b3 Mon Sep 17 00:00:00 2001 From: Andres Marmol Date: Mon, 23 Oct 2023 18:23:57 -0400 Subject: [PATCH] fix: correct expandEnv bug when passing in a new object in the data option --- index.js | 7 ++++--- test/expand.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b9a792c..996277f 100644 --- a/index.js +++ b/index.js @@ -68,15 +68,16 @@ function envSchema (_opts) { /* istanbul ignore else */ if (env) { - if (expandEnv) { - require('dotenv-expand').expand({ parsed: process.env }) - } data.unshift(process.env) } const merge = {} data.forEach(d => Object.assign(merge, d)) + if (expandEnv) { + require('dotenv-expand').expand({ ignoreProcessEnv: true, parsed: merge }) + } + const ajv = chooseAjvInstance(sharedAjvInstance, opts.ajv) const valid = ajv.validate(schema, merge) diff --git a/test/expand.test.js b/test/expand.test.js index ef5f175..e33d065 100644 --- a/test/expand.test.js +++ b/test/expand.test.js @@ -46,6 +46,30 @@ const tests = [ confExpected: { EXPANDED_VALUE_FROM_DOTENV: 'the password is password!' } + }, + { + name: 'simple object - ok - expandEnv works when passed an arbitrary new object based on process.env as data', + schema: { + type: 'object', + properties: { + URL: { + type: 'string' + }, + K8S_NAMESPACE: { + type: 'string' + } + } + }, + expandEnv: true, + isOk: true, + data: { + ...process.env, + K8S_NAMESPACE: 'hello' + }, + confExpected: { + URL: 'https://prefix.hello.pluto.my.domain.com', + K8S_NAMESPACE: 'hello' + } } ]