scenv-zod is a utility library that integrates the Zod schema validation library with scenv, a package for managing environment variables and configuration. It provides a `parser` function that allows developers to define robust validation and coercion rules for their configuration variables using Zod schemas. This ensures type safety and data integrity when reading values from environment variables, CLI arguments, or default settings within scenv. Currently at version 0.4.0, it's a relatively new and actively developed package, primarily focused on enhancing `scenv`'s parsing capabilities. It differentiates itself by leveraging Zod's powerful schema definition language for parsing, offering granular control over data types, transformations (e.g., string to number/boolean), and comprehensive error handling. This makes it a strong choice for applications requiring strict and predictable configuration validation, reducing runtime errors caused by malformed environment variables. Its concise API simplifies complex validation logic, promoting cleaner and more maintainable configuration codebases.
npm install scenv-zodVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining environment variables with Zod validation, coercing strings to numbers and booleans, and handling potential errors during parsing using `scenv` and `scenv-zod`.
Always use Zod's `z.coerce.<type>()` methods (e.g., `z.coerce.number()`, `z.coerce.boolean()`) or explicit `.transform()` to convert string inputs to the desired type within your schema.
Use a `try...catch` block around `await variable.get()` to handle validation failures, or prefer `await variable.safeGet()` which returns a `{ success: boolean, data?, error? }` object, allowing for explicit error checking without exceptions.Regularly review the `zod` migration guide when upgrading its major version. Thoroughly test all configuration variable parsers after any `zod` or `scenv-zod` update.
Inspect the `error.errors` array in the `ZodError` for specific validation issues. Ensure string inputs are correctly coerced (e.g., `z.coerce.number()`) and that all rules (e.g., `min`, `max`, `email`) are met by the provided value.
Install the required peer dependencies using your package manager: `npm install scenv zod` or `pnpm add scenv zod`.
Verify that `import { scenv } from 'scenv';` is present and that the `scenv()` function is called with valid arguments, returning a variable object before `.get()` is invoked.