Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 5 days 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 (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-24T00:55:52.000Z (over 8 years ago)
- Last Synced: 2024-11-01T14:43:16.885Z (12 days ago)
- Language: C#
- Homepage:
- Size: 356 KB
- Stars: 17
- Watchers: 8
- 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.
[![Build Status](https://ci.appveyor.com/api/projects/status/github/jonsequitur/Its.Validation?svg=true&branch=master)](https://ci.appveyor.com/project/jonsequitur/its-validation) [![NuGet Status](http://img.shields.io/nuget/v/Its.Validation.svg?style=flat)](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);
```