UNPKG

znv

Version:

Parse your environment with Zod schemas

132 lines 5.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.inferSchemas = void 0; exports.resolveDefaultValueForSpec = resolveDefaultValueForSpec; exports.parseEnvImpl = parseEnvImpl; const z = __importStar(require("zod")); const preprocessors_js_1 = require("./preprocessors.js"); const reporter_js_1 = require("./reporter.js"); /** * Since there might be a provided default value of `null` or `undefined`, we * return a tuple that also indicates whether we found a default. */ function resolveDefaultValueForSpec(defaults, nodeEnv) { if (defaults) { if (nodeEnv != null && Object.hasOwn(defaults, nodeEnv)) { return [true, defaults[nodeEnv]]; } if ("_" in defaults) return [true, defaults["_"]]; } return [false, undefined]; } /** * Mostly an internal convenience function for testing. Returns the input * parameter unchanged, but with the same inference used in `parseEnv` applied. */ const inferSchemas = (schemas) => schemas; exports.inferSchemas = inferSchemas; /** * Parses the passed environment object using the provided map of Zod schemas * and returns the immutably-typed, parsed environment. * * This version of `parseEnv` is intended for internal use and requires a * reporter or token formatters to be passed in. The versions exported in * `index.js` and `compat.js` provide defaults for this third parameter, making * it optional. */ function parseEnvImpl(env, schemas, reporterOrTokenFormatters) { const reporter = typeof reporterOrTokenFormatters === "function" ? reporterOrTokenFormatters : (0, reporter_js_1.makeDefaultReporter)(reporterOrTokenFormatters); const parsed = {}; const errors = []; for (const [key, schemaOrSpec] of Object.entries(schemas)) { const envValue = env[key]; let defaultUsed = false; let defaultValue; try { if (schemaOrSpec instanceof z.ZodType) { if (envValue == null && schemaOrSpec instanceof z.ZodDefault) { defaultUsed = true; defaultValue = schemaOrSpec._def.defaultValue(); // we "unwrap" the default value ourselves and pass it to the schema. // in the very unlikely case that the value isn't stable AND // validation fails, this ensures the default value we report is the // one that was actually used. // (consider `z.number().gte(0.5).default(() => Math.random())` -- if // we invoked the default getter and got 0.7, and then ran the parser // against a missing env var and it generated another default of 0.4, // we'd report a default value that _should_ have passed.) parsed[key] = schemaOrSpec.parse(defaultValue, { errorMap: reporter_js_1.errorMap }); } else { parsed[key] = (0, preprocessors_js_1.getSchemaWithPreprocessor)(schemaOrSpec).parse(envValue, { errorMap: reporter_js_1.errorMap }); } } else if (envValue == null) { [defaultUsed, defaultValue] = resolveDefaultValueForSpec(schemaOrSpec.defaults, env["NODE_ENV"]); if (defaultUsed) { parsed[key] = schemaOrSpec.schema.parse(defaultValue, { errorMap: reporter_js_1.errorMap }); } else { // if there's no default, pass our envValue through the // schema-with-preprocessor (it's an edge case, but our schema might // accept `null`, and the preprocessor will convert `undefined` to // `null` for us). parsed[key] = (0, preprocessors_js_1.getSchemaWithPreprocessor)(schemaOrSpec.schema).parse(envValue, { errorMap: reporter_js_1.errorMap }); } } else { parsed[key] = (0, preprocessors_js_1.getSchemaWithPreprocessor)(schemaOrSpec.schema).parse(envValue, { errorMap: reporter_js_1.errorMap }); } } catch (e) { errors.push({ key, receivedValue: envValue, error: e, defaultUsed, defaultValue, }); } } if (errors.length > 0) { throw new Error(reporter(errors, schemas)); } return parsed; } //# sourceMappingURL=parse-env.js.map