From 85c5cf538cc0faf68aed524fa19974c7c39e8c50 Mon Sep 17 00:00:00 2001 From: Marco Martins Date: Tue, 18 May 2021 20:02:10 +0200 Subject: [PATCH] Accumulate erros on decoding --- package.json | 2 +- src/index.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 9619cd8..913e694 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "environment-decoder", "description": "A decoder for the process.env", "author": "Marco Daniel Martins ", - "version": "1.0.0", + "version": "1.1.0", "license": "MIT", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 711c2e0..df19f36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,14 +60,21 @@ export const environmentDecoder = (schemaType: S): DecodeType => { throw `Missing environment variables: \n${missing.join(`\n`)}\n` } - return schema + const decoderErrors = schema .map(([key, decoder]: [string, any]) => { try { - const value = environment[key] - return [key, decoder(value)] + decoder(environment[key]) + return false } catch (message) { - throw `Error for environment "${key}": ${message}\n` + return `${key}: ${message}` } }) - .reduce((acc, [key, value]) => ({...acc, [key]: value}), {}) + .filter((decoder) => decoder) + if (decoderErrors.length) { + throw `Decoder errors: \n${decoderErrors.join(`\n`)}\n` + } + + return schema + .map(([key, decoder]: [string, any]) => [key, decoder(environment[key])]) + .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}) }