Git Product home page Git Product logo

its.validation's Introduction

A C# library for composing your core business rules and freeing them from framework-specific validation.

Build Status NuGet Status

Define a validation rule:

var wontGoBadTooSoon = Validate.That<Fruit>(fruit => fruit.ExpirationDate > DateTime.Now.AddDays(5));
bool isValid = wontGoBadTooSoon.Check(lemon);

Compose it into other rules:

var basketWontGoBadTooSoon = 
  Validate.That<FruitBasket>(basket =>
    basket.Fruits.Every(wontGoBadTooSoon));

Get information from the rule for display:

var wontGoBadTooSoon = 
  Validate.That<Fruit>(fruit => 
    fruit.ExpirationDate.As("expiration") > DateTime.Now.AddDays(5));

Transform and format the information for display:

var wontGoBadTooSoon = 
  Validate.That<Fruit>(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:

var plan = new ValidationPlan<FruitBasket>
               {
                    basketHasFruit,
                    basketWontGoBadTooSoon
               };

Define rule dependencies:

var plan = new ValidationPlan<FruitBasket>
               {
                    basketHasFruit,               
                    basketWontGoBadTooSoon.When(basketHasFruit)
               };

Test your validation plan:

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:

var upcCodeExists = 
  Validation.That<Fruit>(fruit => database.UpcCodes.Exists(upc => upc.Code == fruit.UpcCode))
    .With<DatabaseError>(DatabaseErrors.UpcCodeDoesNotExist);

its.validation's People

Contributors

jonsequitur avatar nathanboktae avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.