https://github.com/jonsequitur/its.validation
A C# library for composing your core business rules and freeing them from framework-specific validation.
https://github.com/jonsequitur/its.validation
Last synced: 3 months ago
JSON representation
A C# library for composing your core business rules and freeing them from framework-specific validation.
- Host: GitHub
- URL: https://github.com/jonsequitur/its.validation
- Owner: jonsequitur
- License: other
- Created: 2014-11-08T22:43:43.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-24T00:55:52.000Z (about 9 years ago)
- Last Synced: 2025-04-13T16:06:17.219Z (3 months ago)
- Language: C#
- Homepage:
- Size: 356 KB
- Stars: 17
- Watchers: 7
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
### A C# library for composing your core business rules and freeing them from framework-specific validation.
[](https://ci.appveyor.com/project/jonsequitur/its-validation) [](https://www.nuget.org/packages/Its.Validation/)
Define a validation rule:
```csharp
var wontGoBadTooSoon = Validate.That(fruit => fruit.ExpirationDate > DateTime.Now.AddDays(5));
bool isValid = wontGoBadTooSoon.Check(lemon);
```Compose it into other rules:
```csharp
var basketWontGoBadTooSoon =
Validate.That(basket =>
basket.Fruits.Every(wontGoBadTooSoon));
```Get information from the rule for display:
```csharp
var wontGoBadTooSoon =
Validate.That(fruit =>
fruit.ExpirationDate.As("expiration") > DateTime.Now.AddDays(5));
```Transform and format the information for display:
```csharp
var wontGoBadTooSoon =
Validate.That(fruit =>
fruit.As("fruitname", f => f.Name).ExpirationDate.As("expiration") > DateTime.Now.AddDays(5.As("days_in_transit")))
.WithMessage("A {fruitname} that expires on {expiration:D} won't last for {days_in_transit} days.");// OR localized:
.WithMessage(Resources.BasketMustPreventScurvy);
```Combine rules:
```csharp
var plan = new ValidationPlan
{
basketHasFruit,
basketWontGoBadTooSoon
};
```Define rule dependencies:
```csharp
var plan = new ValidationPlan
{
basketHasFruit,
basketWontGoBadTooSoon.When(basketHasFruit)
};
```Test your validation plan:
```csharp
var basket = new FruitBasket();var failures = plan.Execute(basket);
failures.Count(f => f.Message == "Your basket must contain some fruit.")
.Should()
.Be(1);
```Extend the validation results with your own types:
```csharp
var upcCodeExists =
Validation.That(fruit => database.UpcCodes.Exists(upc => upc.Code == fruit.UpcCode))
.With(DatabaseErrors.UpcCodeDoesNotExist);
```