{"library":"scenv-zod","title":"scenv-zod","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install scenv-zod"],"cli":null},"imports":["import { parser } from 'scenv-zod';","import { scenv } from 'scenv';","import { z } from 'zod';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { scenv } from \"scenv\";\nimport { parser } from \"scenv-zod\";\nimport { z } from \"zod\";\n\nconst port = scenv(\"Port\", {\n  key: \"port\",\n  env: \"PORT\",\n  default: 3000,\n  parser: parser(z.coerce.number().min(1).max(65535))\n});\n\nconst debug = scenv(\"Debug\", {\n  key: \"debug\",\n  env: \"DEBUG\",\n  default: false,\n  parser: parser(\n    z.union([z.boolean(), z.literal(\"true\"), z.literal(\"false\")])\n      .transform((v) => v === true || v === \"true\")\n  )\n});\n\n// Simulate environment variables for demonstration\nprocess.env.PORT = process.env.PORT ?? '8080';\nprocess.env.DEBUG = process.env.DEBUG ?? 'true';\n\nasync function runConfig() {\n  try {\n    const portNum = await port.get();   // number\n    const isDebug = await debug.get();  // boolean\n    console.log(`Application Port: ${portNum} (Type: ${typeof portNum})`);\n    console.log(`Debug Mode: ${isDebug} (Type: ${typeof isDebug})`);\n  } catch (error) {\n    if (error instanceof z.ZodError) {\n      console.error('Validation failed:', error.errors);\n    } else {\n      console.error('An unexpected error occurred:', error);\n    }\n  }\n}\n\nrunConfig();","lang":"typescript","description":"Demonstrates defining environment variables with Zod validation, coercing strings to numbers and booleans, and handling potential errors during parsing using `scenv` and `scenv-zod`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}