Giter VIP home page Giter VIP logo

a-patel / litexguard Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 56 KB

This library provides guard clauses that could be used to guard against not expected values. It provides conditions for objects, strings, numerics, datatime and enumerables. It is possible to configure which exception should be thrown.

License: MIT License

guard arguments clause asp-net-core aspnetcore aspnet-core csharp litex

litexguard's Introduction

LiteXGuard

LiteX.Guard is a simple guard clause argument validation library, that helps you with validation of your arguments.

A simple package with guard clause helper methods. This library provides guard clauses that could be used to guard against not expected values. It provides conditions for objects, strings, numerics, datatime and enumerables. It is possible to configure which exception should be thrown.

Argument validation made simple and unified. It ensures arguments conforms to simple validation rules and provides an uniformed exception throwing strategy if arguments are invalid.

It's developed for .NET Standard 2.0 and available via NuGet.

Install package

Install via Nuget.

PM> Install-Package LiteX.Guard

Supported Guard Clauses

Null/Empty

  • Guard.NotNull (throws if input is null)
  • Guard.NotNullOrEmpty (throws if string input is null or empty)

Range

  • Guard.InRange (throws if specified value is greater than or equal to a minimum value and less than)
  • Guard.LessThan (throws if specified value is less than a maximum value)
  • Guard.LessThanOrEqualTo (throws if specified value is less than or equal to a maximum value)
  • Guard.GreaterThan (throws if specified value is greater than a minimum value)
  • Guard.GreaterThanOrEqualTo (throws if specified value is greater than or equal to a minimum value)
  • Guard.NotOutOfLength (throws if specified value is out of the range)
  • Guard.IsPositive (throws if specified value is not positive)
  • Guard.NotNegative (throws if specified value is negative)
  • Guard.NotZero (throws if specified value is zero)
  • Guard.PagingArgsValid (throws if specified value is out of the page range)

Misc

  • Guard.IsEqual (throws if ...)
  • Guard.Against (throws if ...)
  • Guard.Cast (throws if ...)
  • Guard.IsTrue (throws if ...)
  • Guard.IsFalse (throws if ...)
  • Guard.GetParamName (throws if ...)

Type

  • Guard.IsFunction (throws if ...)
  • Guard.IsEnumType (throws if ...)
  • Guard.InheritsFrom (throws if ...)
  • Guard.Implements (throws if ...)
  • Guard.IsSubclassOf (throws if ...)
  • Guard.IsTypeOf (throws if ...)
  • Guard.HasDefaultConstructor (throws if ...)

Usage

public class CustomerController : Controller
{
    [HttpPost]
    [Route("add-customer")]
    public IActionResult AddCustomer(Customer customer)
    {
        // class (custom type) guard
        Guard.NotNull(customer, nameof(customer));

        // property guard
        Guard.NotNullOrEmpty(customer.Username, nameof(customer.Username));

        // explicit message
        Guard.NotNullOrEmpty(customer.FirstName, nameof(customer.FirstName), "Firstname is required");

        // int range guard
        Guard.GreaterThanOrEqualTo(10, 50, nameof(customer.Age));

        // bool guard
        Guard.IsFalse(customer.IsActive, nameof(customer.IsActive));

        return Ok();
    }

    [HttpGet]
    [Route("find-customer")]
    public IActionResult FindCustomer(string username)
    {
        Guard.NotNullOrEmpty(username, nameof(username));


        return Ok();
    }

    [HttpGet]
    [Route("get-customer-by-age")]
    public IActionResult GetCustomerByAge(int age)
    {
        // int range guard
        Guard.GreaterThanOrEqualTo(10, 50, nameof(age));

        return Ok();
    }

    [HttpGet]
    [Route("get-active-customers")]
    public IActionResult GetActiveCustomers(bool isActive)
    {
        // bool guard
        Guard.IsFalse(isActive, nameof(isActive));


        return Ok();
    }

    [HttpPost]
    [Route("add-customer-T")]
    public IActionResult AddCustomer<T>(T customer) where T : class
    {
        // generic type guard
        Guard.NotNull<T>(customer, nameof(customer));

        return Ok();
    }

    [HttpPost]
    [Route("add-customers")]
    public IActionResult AddCustomers(List<Customer> customers)
    {
        // list guard
        Guard.NotNull(customers, nameof(customers));


        return Ok();
    }
}

Support ☎️

Reach out to me at one of the following places!

Authors 👦

Connect with me
Linkedin GitHub Facebook Twitter Instagram Tumblr Website
linkedin github facebook twitter instagram tumblr website

Donations 💵

Buy Me A Coffee

License 🔒

This project is licensed under the MIT License - see the LICENSE file for details

litexguard's People

Contributors

a-patel avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

marciogoularte

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.