Giter Club home page Giter Club logo

pozitrondev.validations's Introduction

  NuGetNuGet

  Build Status

  Azure DevOps coverage

PozitronDev Validations

Nuget package for validating various inputs against invalid values. If validation fails appropriate exception is thrown.

Usage

Validation rules are implemented as extension methods to ValidateFor (IValidate<T>) clause. Therefore, distinct validation methods are available based on the type of the object being extended.

All validation methods accept optional parameterName argument. If the argument is ommited, the name of the underlying type is used to construct the exception messages.

Inline usages are possible too, since all extensions return the input object.

  • Argument validation example
    public void CreateCart(Customer customer)
    {
        customer.ValidateFor().Null();

        // create cart here
    }
  • Inline usage example:
    public async Task<IEnumerable<Company>> GetCompanies()
    {
        return (await dbContext.Companies
                                .Where(x => x.Name == "MyCompany")
                                .OrderBy(x => x.Name)
                                .ToListAsync())
                                .ValidateFor().NullOrEmpty();
    }
    public Address GetCustomerAddress(Customer customer)
    {
        return customer.ValidateFor().Null().Address;
    }
  • Various validations based on the type:
    object myObject;
    myObject.ValidateFor().Null();
    myObject.ValidateFor().Null(nameof(myObject));

    string myString;
    myString.ValidateFor().NullOrEmpty();
    myString.ValidateFor().NullOrWhiteSpace();
    myString.ValidateFor().NullOrEmpty(nameof(myString));
    myString.ValidateFor().NullOrWhiteSpace(nameof(myString));

    int myInt;
    myInt.ValidateFor().Default();
    myInt.ValidateFor().Default(nameof(myInt));

    IEnumerable<T> myList;
    myList.ValidateFor().NullOrEmpty();
    myList.ValidateFor().NullOrEmpty(nameof(myList));

    Customer myCustomer = repo.GetByID(1);
    myCustomer.ValidateFor().NotFound(1);
    myCustomer.ValidateFor().NotFound(1, nameof(myCustomer));

Supported Validation Rules

  • T.IValidate<T>.Null<T>(string parameterName = null)

    • For T as class or nullable ValueType
    • Throws if input is null
  • T.IValidate<T>.NullOrEmpty<T>(string parameterName = null)

    • For T as string
    • Throws if input is null or empty string
  • T.IValidate<T>.NullOrWhiteSpace<T>(string parameterName = null)

    • For T as string
    • Throws if input is null, empty string or whitespace only
  • T.IValidate<T>.Default<T>(string parameterName = null)

    • Throws if input is default value for type T
  • IEnumerable<T>.IValidate<IEnumerable<T>>.NullOrEmpty<T>(string parameterName = null)

    • Throws if input is null or empty collection
  • T.IValidate<T>.NotFound<T, TKey>(TKey key, string parameterName = null)

    • For T as class, TKey as not nullable struct
    • Throws if input (the queried object by key value) is null.
    • Throws NotFoundException, custom exception defined in this package.

Extending with your own validation rules

To extend IValidate with your own rules, you can do the following:

namespace PozitronDev.Validations
{
    public static class MyExtension
    {
        public static int MinQuantity(this IValidate<int> validateClause)
        {
            if (validateClause.Input < 1)
            {
                throw new ArgumentException($"Required quantity should not be less than 1.");
            }

            return validateClause.Input;
        }
    }
}
// Usage
public void Buy(int quantity, decimal price)
{
    quantity.ValidateFor().MinQuantity();

    var total = price * quantity;
    
    // Some processing
}
// Inline usage
public void Buy(int quantity, decimal price)
{
    var total = price * quantity.ValidateFor().MinQuantity();
    
    // Some processing
}

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

pozitrondev.validations's People

Contributors

fiseni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bubdm

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.