Coder Social home page Coder Social logo

validator's Introduction

Validator

NuGet Version NuGet Downloads

Model validation library for .NET applications.

Usage:

Define model classes with properties and methods having validation attributes (e.g.,IntValidate) with constraints (e.g.,MinValue) according to your use case. An introductory example is shown below. For additional usage information refer to QuickStartTests.

public class MyClass
{
    [IntValidate("Id must be greater than 0.", MinValue = 1)]
    public int Id { get; set; }

    [StringValidate("Text must have length between 2 and 100 characters.", MinLength = 2, MaxLength = 100)]
    public string Text { get; set; }

    [ComplexTypeValidate] // This attribute tells ValidationHelper to validate MyValueClass object
    [NotNullValidate("MyValue must be specified.")]
    public MyValueClass MyValue { get; set; }

    [ValidateMethod] // This attribute tells ValidationHelper to execute this method as part of model validation
    internal ValidationError ValidateTextHasOnlyLetters()
    {
        if (string.IsNullOrWhiteSpace(Text))
            return null;
        
        if (Text.All(char.IsLetter))
            return null;
        
        return new ValidationError(nameof(Text), "Text must have only letters.");
    }
}

public class MyValueClass
{
    [NotNullValidate("Value must be specified.")]
    public object Value { get; set; }
}

Create an instance of MyClass.

var myClass =
    new MyClass
    {
        Id = -1, // Invalid
        Text = "SomeText", // Valid
        MyValue = // Valid
            new MyValueClass
            {
                Value = null // Invalid
            }
    };

Then use IValidationHelper, which provides three type of methods to validate the object.

// Instantiate ValidationHelper
var validator = new ValidationHelper();

// Validate using one of the following method
var validationErrors = validator.Validate(myClass);

var isMyClassValid = validator.IsValid(myClass);

validator.CheckIsValid(myClass);

validator's People

Contributors

vyshaksrishyla avatar poehalli avatar reddyashwanth avatar

Watchers

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.