𝗗𝗲𝗳𝗶𝗻𝗲 𝗢𝗻𝗰𝗲, 𝗧𝗿𝘂𝘀𝘁 𝗘𝘃𝗲𝗿𝘆𝘄𝗵𝗲𝗿𝗲: 𝗖𝘁𝗿𝗼𝗘𝗻𝘃 𝗗𝗲𝗲𝗽 𝗗𝗶𝘃𝗲

The CtroEnv core API uses four validator factories. You can use them to secure your environment variables with ease.

Here is how they work:

• string() — Use this for text. You can add rules for URLs, emails, ports, or specific lengths. It supports regex for custom patterns.

• number() — Use this for numeric values. It converts strings like "3000" into actual numbers. You can enforce integers, positive values, or specific ranges.

• boolean() — Use this for true/false values. It handles strings like "true" or numbers like "1" automatically. This is perfect for feature flags.

• pick() — Use this to restrict values to a specific list. It creates literal types so your code knows exactly which strings are allowed. It even suggests corrections if you make a typo.

Useful methods for every validator:

CtroEnv works across different environments:

When validation fails, CtroEnv does not just stop. It collects every single error and shows them all at once. You get a clear report with the key, the message, and even suggestions on how to fix it.

The final output is a frozen object. This means your configuration is read-only and safe from accidental changes during runtime.

Source: https://dev.to/ctrotech/define-once-trust-everywhere-ctroenv-deep-dive-3bjl