Giter VIP home page Giter VIP logo

bytes2you.validation's Introduction

Bytes2you.Validation

Fast, extensible, intuitive and easy-to-use C# portable library providing fluent APIs for argument validation. Gives everything you need to implement defensive programming (http://veskokolev.com/2014/04/27/defensive-programming-technique-the-way-to-fast-failure/) in your .NET applications.

Supported Frameworks

The supported frameworks are: - .NET Framework 4 and higher; - Silverlight 4 and higher; - Windows Phone 7 and higher; - .NET for Windows Store apps; - Xbox 360.

Installation

The library is available in the NuGet Gallery here. Just type: PM> Install-Package Bytes2you.Validation

API

Guard.WhenArgument(argument, "argument").Rule1().Rule2()...RuleN().Throw();

Based on the argument type there are different things that we can validate that make sense for that type (such as IsEmpty validation for string/guid/collection, etc.) and the library throws the right excpetion (ArgumentException, ArgumentNullException, ArgumentOutOfRangeException) following the best practicies.

Examples: Instead of...

public void SomeMethod(string stringArgument)
{
    if (stringArgument == null)
    {
        throw new ArgumentNullException("stringArgument");
    }
    
    if (stringArgument == "xxx")
    {
        throw new ArgumentException("Argument is equal to <xxx>.", "stringArgument");
    }
}

...we can use

public void SomeMethod(string stringArgument)
{
    Guard.WhenArgument(stringArgument, "stringArgument").IsNullOrEmpty().IsEqual("xxx").Throw();
    // Which means - when stringArgument is null or empty OR is equal to "xxx" we will throw exception. If it is null, we will throw ArgumentNullException. If it is equal to "xxx", we will throw ArgumentException.
}

Rules List

Guard.WhenArgument<T>(argument, "argument")

For all T:

  .IsEqual(value);
  .IsNotEqual(value);

When T is class:

  .IsNull();
  .IsNotNull();
  .IsInstanceOfType(type);
  .IsNotInstanceOfType(type);

When T is Nullable:

  .IsNull();
  .IsNotNull();

When T is bool:

  .IsTrue();
  .IsFalse();

When T is IComparable:

  .IsLessThan(bound);
  .IsGreaterThan(bound);
  .IsLessThanOrEqual(bound);
  .IsGreaterThanOrEqual(bound);

When T is IEnumerable:

  .IsNullOrEmpty();
  .IsNotNullOrEmpty();

When T is Guid:

  .IsEmptyGuid();
  .IsNotEmptyGuid();

When T is string:

  .IsEmpty();
  .IsNotEmpty();
  .IsNullOrEmpty();
  .IsNotNullOrEmpty();
  .IsNullOrWhiteSpace();
  .IsNotNullOrWhiteSpace();
  .IsEqual(value, comparisonType);
  .IsNotEqual(value, comparisonType);

When T is float/double:

  .IsNaN();
  .IsNotNaN();
  .IsInfinity();
  .IsNotInfinity();
  .IsPositiveInfinity();
  .IsNotPositiveInfinity();
  .IsNegativeInfinity();
  .IsNotNegativeInfinity();

When T is byte/short:

  .IsEqual(value);
  .IsNotEqual(value);
  .IsLessThan(bound);
  .IsGreaterThan(bound);
  .IsLessThanOrEqual(bound);
  .IsGreaterThanOrEqual(bound);

Stable

The codebase is fully covered with unit tests (100% code coverage). Performance tests are also presented ensuring that the library will not affect the performance of your applications.

Extensibility

One can easily add new rules (see ValidationRules namespace) and expose them thought fluent API extensions (see FluentExtensions namespace).

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.