UNPKG

3.88 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getSanitizedEnv = exports.testOnlySymbol = void 0;
4var errors_1 = require("./errors");
5var reporter_1 = require("./reporter");
6exports.testOnlySymbol = Symbol('envalid - test only');
7/**
8 * Validate a single env var, given a spec object
9 *
10 * @throws EnvError - If validation is unsuccessful
11 * @return - The cleaned value
12 */
13function validateVar(_a) {
14 var spec = _a.spec, name = _a.name, rawValue = _a.rawValue;
15 if (typeof spec._parse !== 'function') {
16 throw new errors_1.EnvError("Invalid spec for \"".concat(name, "\""));
17 }
18 var value = spec._parse(rawValue);
19 if (spec.choices) {
20 if (!Array.isArray(spec.choices)) {
21 throw new TypeError("\"choices\" must be an array (in spec for \"".concat(name, "\")"));
22 }
23 else if (!spec.choices.includes(value)) {
24 throw new errors_1.EnvError("Value \"".concat(value, "\" not in choices [").concat(spec.choices, "]"));
25 }
26 }
27 if (value == null)
28 throw new errors_1.EnvError("Invalid value for env var \"".concat(name, "\""));
29 return value;
30}
31// Format a string error message for when a required env var is missing
32function formatSpecDescription(spec) {
33 var egText = spec.example ? " (eg. \"".concat(spec.example, "\")") : '';
34 var docsText = spec.docs ? ". See ".concat(spec.docs) : '';
35 return "".concat(spec.desc).concat(egText).concat(docsText);
36}
37var readRawEnvValue = function (env, k) {
38 return env[k];
39};
40var isTestOnlySymbol = function (value) { return value === exports.testOnlySymbol; };
41/**
42 * Perform the central validation/sanitization logic on the full environment object
43 */
44function getSanitizedEnv(environment, specs, options) {
45 if (options === void 0) { options = {}; }
46 var cleanedEnv = {};
47 var castedSpecs = specs;
48 var errors = {};
49 var varKeys = Object.keys(castedSpecs);
50 var rawNodeEnv = readRawEnvValue(environment, 'NODE_ENV');
51 for (var _i = 0, varKeys_1 = varKeys; _i < varKeys_1.length; _i++) {
52 var k = varKeys_1[_i];
53 var spec = castedSpecs[k];
54 var rawValue = readRawEnvValue(environment, k);
55 // If no value was given and default/devDefault were provided, return the appropriate default
56 // value without passing it through validation
57 if (rawValue === undefined) {
58 // Use devDefault values only if NODE_ENV was explicitly set, and isn't 'production'
59 var usingDevDefault = rawNodeEnv && rawNodeEnv !== 'production' && spec.hasOwnProperty('devDefault');
60 if (usingDevDefault) {
61 cleanedEnv[k] = spec.devDefault;
62 if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') {
63 throw new errors_1.EnvMissingError(formatSpecDescription(spec));
64 }
65 continue;
66 }
67 if ('default' in spec) {
68 cleanedEnv[k] = spec.default;
69 continue;
70 }
71 }
72 try {
73 if (rawValue === undefined) {
74 cleanedEnv[k] = undefined;
75 throw new errors_1.EnvMissingError(formatSpecDescription(spec));
76 }
77 else {
78 cleanedEnv[k] = validateVar({ name: k, spec: spec, rawValue: rawValue });
79 }
80 }
81 catch (err) {
82 if ((options === null || options === void 0 ? void 0 : options.reporter) === null)
83 throw err;
84 if (err instanceof Error)
85 errors[k] = err;
86 }
87 }
88 var reporter = (options === null || options === void 0 ? void 0 : options.reporter) || reporter_1.defaultReporter;
89 reporter({ errors: errors, env: cleanedEnv });
90 return cleanedEnv;
91}
92exports.getSanitizedEnv = getSanitizedEnv;
93//# sourceMappingURL=core.js.map
\No newline at end of file