Giter VIP home page Giter VIP logo

sandra.simplevalidator's Introduction

Sandra.SimpleValidator

Sandra.SimpleValidator is designed to be... a simple validator, nothing more. It has no ability to inject repositories and query the database to validate stuff. It has no ability to generate JavaScript validation.

It's purely used to validate a Model/ViewModel or DTO, or something that requires super simple validation such as Required, Minimum Length, etc.

To use simply create a validator:

public class UserValidator : ValidateThis<User>
{
    public UserValidator()
    {
        For(x => x.UserName)
            .Ensure(new Required());

        For(x => x.Email)
            .Ensure(new Required())
            .Ensure(new Email());

        For(x => x.Locale)
            .Ensure(new Required());
    }
}

Then you can inject the ValidationService into your Controller or NancyModule

public class HomeModule : NancyModule
{
    public HomeModule(ValidationService validate)
    {
        Get["/"] = _ =>
        {
            var user = this.Bind<User>();
            var validationResult = validate.This(user);

            if (validationResult.IsInvalid)
            {
                //Handle errors with
                //validationResult.Messages

                return "Validation has fails :(";
            }

            return "Validation was successful :)";
        };
    }
}

And that's it. The ValidationService method is virtual so you can mock and test it, and unit testing your Validators is easy too, simply create an instance of the validator and call validate on the model you want to test.

[Fact]
public void Given_Valid_Model_Should_Return_IsValid_As_True()
{
    var validator = new TestClassValidator();
    var model = new TestClass
    {
        Name = "Hello World",
        Thing = "Thing!"
    };

    var result = validator.Validate(model);

    Assert.True(result.IsValid);
}

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.