Skip to content
Valit is dead simple validation for .NET Core. No more if-statements all around your code. Write nice and clean fluent validators instead!
C# Other
  1. C# 98.6%
  2. Other 1.4%
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode autofix files with eclint Dec 4, 2017
src/Valit 2.0.0 Oct 7, 2018
tests/Valit.Tests Merge branch 'develop' into feature/166 Oct 7, 2018
tools remove spaces in gitignore Dec 4, 2017
.editorconfig add default format conventions Dec 4, 2017
.gitattributes add gitattributes Dec 4, 2017
.gitignore remove spaces in gitignore Dec 4, 2017
CONTRIBUTING.md document dotnet watch test Dec 4, 2017
LICENSE Initial commit Sep 11, 2017
PULL_REQUEST_TEMPLATE.md added PR temaplate Sep 25, 2017
README.md Update README.md Oct 7, 2018
Valit.sln autofix files with eclint Dec 4, 2017
appveyor.yml Oldstyle flag Oct 18, 2017
build.cake autofix files with eclint Dec 4, 2017
build.ps1 autofix files with eclint Dec 4, 2017
build.sh added cake scripts for automatic builds Sep 13, 2017
cake.config added cake scripts for automatic builds Sep 13, 2017
codecov.yml Update codecov.yml Oct 26, 2017

README.md

Valit

Valit is dead simple validation for .NET Core. No more if-statements all around your code. Write nice and clean fluent validators instead!

master develop
AppVeyor Build status Build status
Codecov codecov codecov

NuGet

Installation

Valit is available on NuGet.

Package manager

Install-Package Valit -Version 2.0.0

.NET CLI

dotnet add package Valit --version 2.0.0

Getting started

In order to create a validator you need to go through few steps. It's worth mentioning that not all of them are mandatory. The steps are:

  • creating new instance of validator using Create() static method.
  • choosing validation strategy using WithStrategy() method (not required).
  • selecting property using Ensure() method and defining rules for it.
  • Extending rules with custom errors (such as messages or error codes), tags and conditions. (not required).
  • applying created rules to an object using For() method.

Having the validator created, simply invoke Validate() method which will produce the result with all the data.

Let's try it out with very practical example. Imagine that your task is to validate model sent from registration page of your app. The example object might look as follows:

    public class RegisterModel
    {
        public string Email { get; set; }        
        public string Password { get; set; }
        public ushort Age { get; set ;}
    }

These are the validation criteria:

  • Email is required and needs to be a proper email address
  • Password is required and needs to be at least 10 characters long
  • Age must be greater than 16

This is how you can handle such scenario using Valit:

        void ValidateModel(RegisterModel model)
        {
            var result = ValitRules<RegisterModel>
                .Create()
                .Ensure(m => m.Email, _=>_
                    .Required()
                    .Email())
                .Ensure(m => m.Password, _=>_ 
                    .Required()
                    .MinLength(10))
                .Ensure(m => m.Age, _=>_
                    .IsGreaterThan(16))
                .For(model)
                .Validate();

            if(result.Succeeded)
            {
                // do something on success
            }
            else 
            {
                // do something on failure
            }
        }

Pretty cool, right? Of course, the above example was fairly simple but trust us. From now on, even complicated validation criterias won't scare you anymore ;)

Documentation

If you're looking for documentation, you can find it here.

Contributing

Want to help us develop Valit? Awesome! Here you can find contributor guide ;)

You can’t perform that action at this time.