DotThrow 2.0.2
dotnet add package DotThrow --version 2.0.2
NuGet\Install-Package DotThrow -Version 2.0.2
<PackageReference Include="DotThrow" Version="2.0.2" />
<PackageVersion Include="DotThrow" Version="2.0.2" />
<PackageReference Include="DotThrow" />
paket add DotThrow --version 2.0.2
#r "nuget: DotThrow, 2.0.2"
#:package DotThrow@2.0.2
#addin nuget:?package=DotThrow&version=2.0.2
#tool nuget:?package=DotThrow&version=2.0.2
DotThrow
Guard clauses and validation for .NET, with one set of checks. As a guard clause, a chain
of checks throws the right exception with the parameter name filled in. In collect mode,
the same checks record every error in a report that you can return from an API.
Validator<T> has the features of FluentValidation 12.
- More than 110 checks: strings, numbers, dates, collections, dictionaries, enums, URIs, IP addresses, streams and types.
- MIT licensed, no dependencies.
- Checks on numbers, strings, dates, enums and in-memory collections allocate nothing when they pass.
- Message templates with placeholders, and translations for 58 cultures in 54 languages.
- Rules from JSON or
IConfiguration(DotThrow.Dynamic), updated at run time. - Roslyn analyzers and code fixes in the package, for example for a chain without checks.
- Targets netstandard2.0, netstandard2.1, net462, net8.0, net9.0 and net10.0. Trimming and Native AOT safe on .NET 8 and later.
Install
dotnet add package DotThrow
Packages
The DotThrow packages share one version:
- DotThrow (this package): guard clauses, collect mode,
Validator<T>, dynamic rules, message translations and analyzers. No dependencies. - DotThrow.DependencyInjection: register validators in dependency injection, validate options classes, and dynamic validators from configuration.
- DotThrow.AspNetCore: validation for minimal API endpoints and MVC actions, and validation problem helpers.
Guard clauses
using DotThrow;
public sealed class Customer
{
public Customer(string name, string email, int age)
{
Name = name.Throw().IfNullOrWhiteSpace().IfLongerThan(100);
Email = email.Throw().IfNullOrWhiteSpace().IfNotEmailAddress();
Age = age.Throw().IfOutOfRange(0, 150);
}
public string Name { get; }
public string Email { get; }
public int Age { get; }
}
new Customer("Ada Lovelace", "ada@example.com", 200) throws:
System.ArgumentOutOfRangeException: 'age' must be between 0 and 150. (Parameter 'age')
Actual value was 200.
Collect every error
var form = new SignUpForm(Name: "", Email: "ada.example.com", Age: 12);
var report = new ValidationReport();
report.Check(form, f => f.Name).IfNullOrWhiteSpace();
report.Check(form, f => f.Email).IfNullOrWhiteSpace().IfNotEmailAddress();
report.Check(form, f => f.Age).IfLessThan(13, "You must be 13 or older.");
Console.WriteLine(report);
public sealed record SignUpForm(string Name, string Email, int Age);
Validation failed with 3 errors:
- Name: 'Name' must not be empty or white space.
- Email: 'Email' must be a valid email address.
- Age: You must be 13 or older.
In ASP.NET Core, Results.ValidationProblem(report.ToDictionary()) turns the report into
a standard 400 response.
Reusable validators
public sealed class SignUpFormValidator : Validator<SignUpForm>
{
public SignUpFormValidator()
{
For(x => x.Name, name => name.IfNullOrWhiteSpace().IfLongerThan(100));
For(x => x.Email, email => email.IfNullOrWhiteSpace().IfNotEmailAddress(), rule => rule
.WithMessage("Please check the email address."));
For(x => x.Age, age => age.IfLessThan(13, "You must be 13 or older."));
}
}
var validator = new SignUpFormValidator();
ValidationReport result = validator.Validate(new SignUpForm("Ada Lovelace", "ada@example.com", 36));
Validators also handle nested objects, collections, conditions, groups, severity, async
rules and polymorphic types, and DotThrow.Testing has test helpers with the names of
FluentValidation's TestHelper.
Learn more
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on DotThrow:
| Package | Downloads |
|---|---|
|
DotThrow.DependencyInjection
Microsoft.Extensions.DependencyInjection support for DotThrow validators: register validators by assembly scanning or one by one (AOT safe), validate options classes with a DotThrow validator (ValidateOnStart, named options), and register dynamic validators whose rules come from IConfiguration and reload when it changes. |
|
|
DotThrow.AspNetCore
ASP.NET Core support for DotThrow validators: a minimal API endpoint filter (WithValidation, Native AOT friendly), an MVC action filter, and ValidationReport to RFC 7807 ValidationProblemDetails helpers. |
GitHub repositories
This package is not used by any popular GitHub repositories.