GithubHelp home page GithubHelp logo

tests's Introduction

Fluent Validator

A .NET Core library that uses lambda expressions to build validations rules.

Nuget Package

Install-Package FluentValidator.Core

Example Usage

Initializing validations from a constructor

public class Person : Notifiable
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    
    public Person(){}

    public Person(string name, string email, int age)
    {
        this.Name = name;
        this.Email = email;
        this.Age = age;

        new ValidationContract<Person>(this).IsRequired(x => x.Name);
        new ValidationContract<Person>(this).IsRequired(x => x.Email);
        new ValidationContract<Person>(this).IsGreaterThan(x => x.Age, 18);
        new ValidationContract<Person>(this).IsEmail(x => x.Email);
    }
}

Initializing validations from an existing object instance

public class Person : Notifiable
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    
    public Person()
    {

    }
}

class Program 
{
    static void Main(string[] args)
    {
        Person newPerson = new Person();
        newPerson.Name = "Milton";
        newPerson.Email = "[email protected]";
        newPerson.Age = 15;

        new ValidationContract<Person>(newPerson).IsRequired(x => x.Name);
        new ValidationContract<Person>(newPerson).IsRequired(x => x.Email);
        new ValidationContract<Person>(newPerson).IsGreaterThan(x => x.Age, 18);
        new ValidationContract<Person>(newPerson).IsEmail(x => x.Email);            

        Console.WriteLine(newPerson.IsValid());
        
        foreach(var message in newPerson.Notifications)
        {
            Console.WriteLine(message.Message);
        }
    }
}

Availble validators

  • IsRequired(Expression<Func<T, string>> selector, string message = "")
  • HasMinLenght(Expression<Func<T, string>> selector, int min, string message = "")
  • HasMaxLenght(Expression<Func<T, string>> selector, int max, string message = "")
  • IsFixedLenght(Expression<Func<T, string>> selector, int length, string message = "")
  • IsEmail(Expression<Func<T, string>> selector, string message = "")
  • IsUrl(Expression<Func<T, string>> selector, string message = "")
  • IsGreaterThan(Expression<Func<T, int>> selector, int number, string message = "")
  • IsGreaterOrEqualsThan(Expression<Func<T, int>> selector, int number, string message = "")
  • IsGreaterThan(Expression<Func<T, decimal>> selector, decimal number, string message = "")
  • IsGreaterOrEqualsThan(Expression<Func<T, decimal>> selector, decimal number, string message = "")
  • IsGreaterThan(Expression<Func<T, double>> selector, double number, string message = "")
  • IsGreaterOrEqualsThan(Expression<Func<T, double>> selector, double number, string message = "")
  • IsGreaterThan(Expression<Func<T, DateTime>> selector, DateTime date, string message = "")
  • IsGreaterOrEqualsThan(Expression<Func<T, DateTime>> selector, DateTime date, string message = "")
  • IsLowerThan(Expression<Func<T, int>> selector, int number, string message = "")
  • IsLowerOrEqualsThan(Expression<Func<T, int>> selector, int number, string message = "")
  • IsLowerThan(Expression<Func<T, decimal>> selector, decimal number, string message = "")
  • IsLowerOrEqualsThan(Expression<Func<T, decimal>> selector, decimal number, string message = "")
  • IsLowerThan(Expression<Func<T, double>> selector, double number, string message = "")
  • IsLowerOrEqualsThan(Expression<Func<T, double>> selector, double number, string message = "")
  • IsLowerThan(Expression<Func<T, DateTime>> selector, DateTime date, string message = "")
  • IsLowerOrEqualsThan(Expression<Func<T, DateTime>> selector, DateTime date, string message = "")
  • IsBetween(Expression<Func<T, int>> selector, int a, int b, string message = "")
  • IsBetween(Expression<Func<T, decimal>> selector, decimal a, decimal b, string message = "")
  • IsBetween(Expression<Func<T, double>> selector, double a, double b, string message = "")
  • IsBetween(Expression<Func<T, DateTime>> selector, DateTime a, DateTime b, string message = "")
  • Contains(Expression<Func<T, string>> selector, string text, string message = "")
  • IsNull(object obj, string message)
  • IsNotNull(object obj, string message)
  • AreEquals(Expression<Func<T, string>> selector, string text, string message = "")
  • AreEquals(Expression<Func<T, int>> selector, int val, string message = "")
  • AreEquals(Expression<Func<T, decimal>> selector, decimal val, string message = "")
  • AreEquals(Expression<Func<T, double>> selector, double val, string message = "")
  • AreEquals(Expression<Func<T, bool>> selector, bool val, string message = "")
  • AreEquals(Expression<Func<T, DateTime>> selector, DateTime val, string message = "")
  • AreNotEquals(Expression<Func<T, string>> selector, string text, string message = "")
  • AreNotEquals(Expression<Func<T, int>> selector, int val, string message = "")
  • AreNotEquals(Expression<Func<T, decimal>> selector, decimal val, string message = "")
  • AreNotEquals(Expression<Func<T, double>> selector, double val, string message = "")
  • AreNotEquals(Expression<Func<T, bool>> selector, bool val, string message = "")
  • AreNotEquals(Expression<Func<T, DateTime>> selector, DateTime val, string message = "")
  • IsTrue(Expression<Func<T, bool>> selector, string message = "")
  • IsFalse(Expression<Func<T, bool>> selector, string message = "")
  • IsTrue(bool value, string field, string message = "")
  • IsFalse(bool value, string field, string message = "")

tests's People

Contributors

mcamaraanp avatar

Stargazers

 avatar

Watchers

Milton Câmara avatar James Cloos 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.