Philiprehberger.EnvValidator 0.2.9

dotnet add package Philiprehberger.EnvValidator --version 0.2.9
                    
NuGet\Install-Package Philiprehberger.EnvValidator -Version 0.2.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.EnvValidator" Version="0.2.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.EnvValidator" Version="0.2.9" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.EnvValidator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.EnvValidator --version 0.2.9
                    
#r "nuget: Philiprehberger.EnvValidator, 0.2.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.EnvValidator@0.2.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.EnvValidator&version=0.2.9
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.EnvValidator&version=0.2.9
                    
Install as a Cake Tool

Philiprehberger.EnvValidator

CI NuGet Last updated

Declarative environment variable validation for .NET — attribute-based binding with type coercion.

Installation

dotnet add package Philiprehberger.EnvValidator

Usage

using Philiprehberger.EnvValidator;

// Define your config class
public class AppConfig
{
    [EnvVar("DATABASE_URL")]
    public string DatabaseUrl { get; set; } = "";

    [EnvVar("PORT", Default = "3000")]
    public int Port { get; set; }

    [EnvVar("LOG_LEVEL", Required = false, Choices = new[] { "debug", "info", "warn", "error" })]
    public string LogLevel { get; set; } = "info";

    [EnvVar("ENABLE_CACHE", Default = "true")]
    public bool EnableCache { get; set; }

    [EnvVar("REQUEST_TIMEOUT", Default = "00:00:30")]
    public TimeSpan RequestTimeout { get; set; }
}

// Validate from environment
var config = EnvValidator.Validate<AppConfig>();

// Validate from a dictionary (useful for testing)
var config = EnvValidator.Validate<AppConfig>(new Dictionary<string, string>
{
    ["DATABASE_URL"] = "postgresql://localhost/mydb",
    ["PORT"] = "8080",
});

Enum Support

Enum properties are parsed case-insensitively. Invalid values produce an error listing all valid options.

public enum Environment { Development, Staging, Production }

public class AppConfig
{
    [EnvVar("APP_ENV", Default = "Development")]
    public Environment AppEnv { get; set; }
}

Setting APP_ENV=production will correctly parse to Environment.Production.

Pattern Validation

Use the Pattern property to validate values against a regex before type conversion.

public class AppConfig
{
    [EnvVar("API_KEY", Pattern = @"^sk-[a-zA-Z0-9]{32}$")]
    public string ApiKey { get; set; } = "";

    [EnvVar("PORT", Pattern = @"^\d{4,5}$", Default = "3000")]
    public int Port { get; set; }
}

If the value doesn't match, validation fails with: Variable 'API_KEY' does not match required pattern '^sk-[a-zA-Z0-9]{32}$'.

Collection Parsing

Properties of type string[], int[], List<string>, or List<int> are parsed by splitting the value on a separator (comma by default). Whitespace around each element is trimmed.

public class AppConfig
{
    [EnvVar("ALLOWED_ORIGINS")]
    public string[] AllowedOrigins { get; set; } = [];

    [EnvVar("RETRY_DELAYS")]
    public List<int> RetryDelays { get; set; } = [];

    [EnvVar("TAGS", Separator = ";")]
    public List<string> Tags { get; set; } = [];
}

Setting ALLOWED_ORIGINS=https://example.com, https://app.example.com parses into a two-element string array. Use the Separator property to change the delimiter.

Error Handling

try
{
    var config = EnvValidator.Validate<AppConfig>();
}
catch (ValidationException ex)
{
    foreach (var error in ex.Errors)
        Console.WriteLine(error);
}

API

EnvValidator

Method Description
Validate<T>() Validate environment variables and bind to a typed class
Validate<T>(source) Validate from a custom dictionary source

EnvVarAttribute

Property Type Default Description
Name string Environment variable name
Required bool true Whether the variable must be set
Default string? null Default value if not set
Choices string[]? null Allowed values
Pattern string? null Regex pattern to match
Separator string "," Separator for list values

Supported Types

string, int, long, double, bool, Uri, TimeSpan, enums, string[], int[], List<string>, List<int>

Bool accepts: true/false, 1/0, yes/no, on/off

Attribute Properties

Property Type Default Description
Name string Environment variable name (constructor arg)
Required bool true Whether the variable must be present
Default string? null Fallback value when variable is missing
Choices string[]? null Restrict to allowed values
Pattern string? null Regex pattern the raw value must match
Separator string "," Delimiter for collection/array parsing

Development

dotnet build src/Philiprehberger.EnvValidator.csproj --configuration Release

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.9 124 4/1/2026
0.2.8 108 3/27/2026
0.2.7 110 3/25/2026
0.2.6 112 3/23/2026
0.2.5 107 3/21/2026
0.2.4 109 3/20/2026
0.2.3 121 3/16/2026
0.2.2 114 3/16/2026
0.2.1 113 3/16/2026
0.2.0 110 3/13/2026
0.1.1 117 3/11/2026
0.1.0 115 3/9/2026