Swallow.Validation
0.3.2
See the version list below for details.
dotnet add package Swallow.Validation --version 0.3.2
NuGet\Install-Package Swallow.Validation -Version 0.3.2
<PackageReference Include="Swallow.Validation" Version="0.3.2" />
paket add Swallow.Validation --version 0.3.2
#r "nuget: Swallow.Validation, 0.3.2"
// Install Swallow.Validation as a Cake Addin #addin nuget:?package=Swallow.Validation&version=0.3.2 // Install Swallow.Validation as a Cake Tool #tool nuget:?package=Swallow.Validation&version=0.3.2
Swallow.Validation
A lightweight, fluent-style validation library for C#. But what does that mean? That means minimal dependencies (none, actually) and great programmer experience.
Keep in mind that Swallow.Validation is in a rather early stage. As long as the version is 0.x.y
, it
is not particularily "production-ready", as the validation might change... a lot. It should work out
the way it is now, but there are no guarantees that you won't have to adjust stuff over and over when
updating, especially when it's a minor version bump. Patches should be fine.
Example
You can take a look at the test suite for examples. But in general, a validation is executed as follows:
var validationResult = Validation.Check()
.That(() => someValue).Satisfies(v => v.IsFoo(), "be foo").When(v => v.IsBar(), "bar is quuz")
.That(() => someClass.SomeMember).IsNotNull()
.Then(() => true);
if (validationResult.Errors.Any())
{
Console.WriteLine("Oops, something did not validate!");
foreach (var error in validationResult.Errors)
{
Console.WriteLine(error.ToString());
}
}
This will produce the following output, given that v.IsBar()
returns true, v.IsFoo()
returns
false, someClass.SomeMember
is null
and someValue.ToString()
returns "someValue"
:
Oops, something did not validate!
someValue must be foo when bar is quuz but was 'someValue'
someClass.SomeMember must not be null
Custom errors
To make errors presentable to a user (for example by translating the technical english to the UI language), Swallow.Validation supports custom error types containing information not as a string, but as typed fields.
There are a bunch of predefined custom errors, such as EmptyCollectionValidationError
, RegexValidationError
and RangeValidationError
. You can define your own errors, too! Create your class, inherit from ValidationError
and you're good to go. The property name and actual value will be filled in automatically, no need to
take them in a constructor and assign them.
It also is a good idea to have your custom error override ToString()
, to allow the error to be written
as log message or simply be viewed nicely while debugging.
Custom validation rules
Always specifying a predicate and error message by calling Satisfies
is not very DRY and prone to
typos and differences. To help just that, there are a bunch of extensions like IsInRange
or Matches
that check predefined predicates on assertions and produce certain custom errors. Swallow.Validation
comes with a basic set of these out-of-the-box, but you can (and should, probably!) add your own extensions
to fill the gaps in your domain. Write an extension taking IAssertable<T>
as this
and returning
IConditionable<TYourError>
to have a fancy extension! As an example:
public static class FooExtensions
{
public static IConditionable<FooError> IsAFoo<T>(this IAssertable<T> assertion)
{
return assertion.Satisfies(x => x is Foo, _ => new FooError());
}
}
Whoa, where you got that fancy badge from?
Check out Buildstats for fancy badges like this one.
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 was computed. 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. |
.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 was computed. |
.NET Framework | net461 was computed. net462 was computed. 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. |
This package has no dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Swallow.Validation:
Package | Downloads |
---|---|
Swallow.Validation.ServiceCollection
A lightweight, fluent-style validation library |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
7.0.0 | 2,058 | 1/20/2024 |
6.0.1 | 1,097 | 7/2/2023 |
6.0.0 | 2,121 | 1/22/2023 |
5.0.1 | 742 | 11/13/2022 |
4.2.0 | 868 | 6/20/2022 |
4.1.1 | 613 | 5/29/2022 |
4.1.0 | 798 | 2/13/2022 |
4.0.0 | 545 | 2/13/2022 |
3.1.0 | 22,355 | 6/5/2021 |
3.0.0 | 461 | 5/20/2021 |
2.0.0 | 483 | 3/6/2021 |
1.5.0 | 2,255 | 11/23/2020 |
1.4.0 | 442 | 9/4/2020 |
1.3.0 | 616 | 7/20/2020 |
1.2.0 | 1,403 | 6/4/2020 |
1.1.1 | 491 | 5/9/2020 |
1.1.0 | 468 | 5/5/2020 |
1.0.0 | 575 | 4/29/2020 |
0.12.3 | 510 | 4/5/2020 |
0.12.2 | 473 | 4/3/2020 |
0.12.1 | 11,273 | 3/16/2020 |
0.12.0 | 503 | 3/14/2020 |
0.11.2 | 580 | 2/29/2020 |
0.11.1 | 552 | 2/16/2020 |
0.11.0 | 530 | 2/13/2020 |
0.10.3 | 521 | 2/12/2020 |
0.10.2 | 575 | 1/31/2020 |
0.10.1 | 530 | 1/27/2020 |
0.10.0 | 535 | 1/5/2020 |
0.9.1 | 483 | 1/3/2020 |
0.8.0 | 481 | 12/21/2019 |
0.7.3 | 4,947 | 11/27/2019 |
0.7.2 | 488 | 11/9/2019 |
0.7.1 | 478 | 11/9/2019 |
0.7.0 | 502 | 11/9/2019 |
0.6.1 | 485 | 10/27/2019 |
0.5.3 | 489 | 10/23/2019 |
0.5.2 | 477 | 10/21/2019 |
0.5.1 | 475 | 10/20/2019 |
0.4.2 | 512 | 9/14/2019 |
0.4.1 | 506 | 9/7/2019 |
0.3.3 | 512 | 9/7/2019 |
0.3.2 | 557 | 8/28/2019 |
0.3.1 | 531 | 8/24/2019 |
0.2.4 | 502 | 8/24/2019 |
0.2.3 | 578 | 8/24/2019 |