Getting started

This part will guide you through very basic concepts of Valit, including instalation proccess and creating simple validator for domain object.


Valit is available on NuGet.

Package manager

Install-Package Valit -Version 2.0.0


dotnet add package Valit --version 2.0.0

Creating basic validator

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 you’re 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 valid 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>
        .Ensure(m => m.Email, _=>_
        .Ensure(m => m.Password, _=>_
        .Ensure(m => m.Age, _=>_

        // do something on success
        // 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 ;)

Need help?

If you need some help, feel free to look at more examples on GitHub. Also don’t hesitate to open new issues if something could be done better!