LightVx 1.1.0

LightVx is a light, easy and extensible validation framework for .Net which includes a Fluent API.

There is a newer version of this package available.
See the version list below for details.
Install-Package LightVx -Version 1.1.0
dotnet add package LightVx --version 1.1.0
<PackageReference Include="LightVx" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightVx --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LightVx, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install LightVx as a Cake Addin
#addin nuget:?package=LightVx&version=1.1.0

// Install LightVx as a Cake Tool
#tool nuget:?package=LightVx&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Light Vx

LightVx is a light, easy and extensible validation framework for .Net which includes a Fluent API.
It's intended to help validating user input in apps, or service requests in Web Services or Web API's, or anywhere you need to validate data.

Author

Tim Wheeler - http://blog.timwheeler.io/
Contact - http://timwheeler.io/#contact

Download

Source on GitHub
Nuget Package

Built-in Validators

  • Aggregate - Combines other validators
  • AlphaNumeric - Alphabetical or Numbers
  • AlphaText - a to Z and spaces
  • Decimal - a decimal value
  • Email - email address
  • Url - Uri/Url
  • Empty - will match an empty string
  • HexColor - a valid hex color
  • IsNull - matches null
  • Length - matches string length. Supply min and max value.
  • MaxLength - matches a max string length
  • MinLength - matches a min string length
  • Max - validates a number is not greater than x. If input is an Array or ICollection then it will validate against number of items.
  • Min - validates a number is not less than x. If input is an Array or ICollection then it will validate against number of items.
  • NotEmpty - must not be empty string
  • Numeric - numbers only
  • PhoneAndLength - combine phone number validator and length validator
  • PhoneNumber - attempts to validate a phone number with optional braces
  • SqlSafeText - Detects use of characters used in SQL Injection Attacks
  • XssSafeText - Detects use of characters used in XSS Attacks
  • SafeText - Combines both the XSS and SQL validators.
  • Url - validates against a valid url
  • MinDate - Date is equal to or greater than
  • MaxDate - Date is equal to or less than

Location Specific Validators

US - United States

Note: To use the US Validators in the Fluent API add use namespace: LightVx.Validators.US

  • USStateValidator - Checks values against a known list of US state codes (2 characters uppercase)

Fluent API

Using the Validator.Eval method you can call a number of validators.
Example

            var input = "123ABC";
            Validator.Eval(input, "Customer ID")
                .Required()
                .IsAlphaNumeric()
                .HasLength(6, 6)
                .Success((() =>
                {
                    Console.WriteLine("Validation succeeded");
                }))
                .Fail((errors, validators) =>
                {
                    Console.WriteLine(string.Join(",", errors));
                    // Validation failed, put your failure logic here
                });

For more examples, see below.

Available Methods

  • Required() - must not be null or empty string
  • HasLength(int min, int? max)
  • IsAlphaNumeric()
  • IsAlphaText()
  • IsDecimal()
  • IsEmailAddress()
  • IsNumeric()
  • IsPhoneNumber()
  • IsSafeText()
  • IsSafeForSql()
  • IsSafeForXss()
  • IsUrl()
  • Min(int value)
  • Min(double value)
  • Min(decimal value)
  • Max(int/double/decimal/date value)
  • IsEmpty()
  • IsNotEmpty()
  • IsNull()
  • IsNotNull()
  • HasMinLength(int minLength)
  • HasMaxLength(int maxLength)
  • IsAfter(DateTime date)
  • IsBefore(DateTime date)
  • IsAfter(DateTime date)
  • IsBetween(DateTime startDate, DateTime endDate)

US Validation Extensions

  • IsUSState() - Checks values against a known list of US state codes (2 characters uppercase)

Fluent API Examples

Example using Result

            var input = 100; //user input to be validated
            var result = Validator.Eval(input, "Quantity Requested")
                .Required()
                .Min(50)
                .Max(100);
            if (result.IsValid == false)
            {
                Console.WriteLine(string.Join(";", result.ErrorMessages));
                //... add failure logic here
            }

Inline Example

            var input = "https://github.com/TjWheeler/LightVx"; //user input to be validated
            Validator.Eval(input, "Source Url")
                .Required()
                .IsUrl()
                .Success((() =>
                {
                    Console.WriteLine("Validation succeeded");
                }))
                .Fail((errors, validators) =>
                {
                    Console.WriteLine(string.Join(",", errors));
                    // Validation failed, put your failure logic here
                });

Examples using Validation Helper

            //WebApi - Validate that the text is matches Alphabet only
            var input = "ABC";
            string errorMessage;
            if (Validator.IsNotValid<AlphaTextValidator>(input, "First Name" , out errorMessage))
            {
                return BadRequest($"The input is invalid: {errorMessage}");
            }

Examples using Validators directly

Note: Although you can call the validators directly, using the Validation helper is more convenient.

            //Validate numberic
            string input = "123ABC";
            IValidator validator = new NumericValidator();
            validator.Validate(input, "My Field Name");
            if (!validator.IsValid)
            {
                Console.WriteLine("Input: " + input + " return error (" + validator.ErrorMessage + ")");
            }

Extending and creating your own validators

In the following example, we are inheriting from an AggregateValidator, this allows us to combine validators.

Creating a Post Code Validator by combining other validators.

//Step 1: Add the custom validator
public class PostCodeValidator : AggregatedValidator
    {
        public PostCodeValidator()
        {
            AddValidator(new LengthValidator(4, 4));
            AddValidator(new NumericValidator());
        }
    }

//Step 2: Add an extension method
public static class PostCodeValidatorExtension
    {
        public static ValidatorFluent IsPostCode(this ValidatorFluent fluentApi)
        {
            fluentApi.AddValidator(new PostCodeValidator());
            return fluentApi;
        }
    }

//Step 3: Call it to validate input

    public void ExampleCustomValidator()
    {
        string input = "...";
        var isValid = Validator.Eval(input, "MyFieldName")
            .Required()
            .IsPostCode()
            .Fail(((errors, validators) =>
            {
                Console.WriteLine("Example failure: " + string.Join(";", errors));
            })).IsValid;
    }

Creating your own validator

Create a class and inherit ValidatorBase. The only method you need to implement is Validate. There are some base methods that will make it easy to validate using Regular Expressions. Here's an example of one of the built in validators.

    /// <summary>
    ///     Validate Email Addresses
    /// </summary>
    public class EmailValidator : ValidatorBase
    {
        protected override string Expression => @"^([\&\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

        protected override void Validate()
        {
            if (_Input == null || (string) _Input == string.Empty)
            {
                Succeed();
                return;
            }

            if (SingleMatch((string) _Input))
                Succeed();
            else
                Fail("is not a valid email address.");
        }
    }

When using Regular expressions you can also use the HasMatch and MatchCount base methods.

Light Vx

LightVx is a light, easy and extensible validation framework for .Net which includes a Fluent API.
It's intended to help validating user input in apps, or service requests in Web Services or Web API's, or anywhere you need to validate data.

Author

Tim Wheeler - http://blog.timwheeler.io/
Contact - http://timwheeler.io/#contact

Download

Source on GitHub
Nuget Package

Built-in Validators

  • Aggregate - Combines other validators
  • AlphaNumeric - Alphabetical or Numbers
  • AlphaText - a to Z and spaces
  • Decimal - a decimal value
  • Email - email address
  • Url - Uri/Url
  • Empty - will match an empty string
  • HexColor - a valid hex color
  • IsNull - matches null
  • Length - matches string length. Supply min and max value.
  • MaxLength - matches a max string length
  • MinLength - matches a min string length
  • Max - validates a number is not greater than x. If input is an Array or ICollection then it will validate against number of items.
  • Min - validates a number is not less than x. If input is an Array or ICollection then it will validate against number of items.
  • NotEmpty - must not be empty string
  • Numeric - numbers only
  • PhoneAndLength - combine phone number validator and length validator
  • PhoneNumber - attempts to validate a phone number with optional braces
  • SqlSafeText - Detects use of characters used in SQL Injection Attacks
  • XssSafeText - Detects use of characters used in XSS Attacks
  • SafeText - Combines both the XSS and SQL validators.
  • Url - validates against a valid url
  • MinDate - Date is equal to or greater than
  • MaxDate - Date is equal to or less than

Location Specific Validators

US - United States

Note: To use the US Validators in the Fluent API add use namespace: LightVx.Validators.US

  • USStateValidator - Checks values against a known list of US state codes (2 characters uppercase)

Fluent API

Using the Validator.Eval method you can call a number of validators.
Example

            var input = "123ABC";
            Validator.Eval(input, "Customer ID")
                .Required()
                .IsAlphaNumeric()
                .HasLength(6, 6)
                .Success((() =>
                {
                    Console.WriteLine("Validation succeeded");
                }))
                .Fail((errors, validators) =>
                {
                    Console.WriteLine(string.Join(",", errors));
                    // Validation failed, put your failure logic here
                });

For more examples, see below.

Available Methods

  • Required() - must not be null or empty string
  • HasLength(int min, int? max)
  • IsAlphaNumeric()
  • IsAlphaText()
  • IsDecimal()
  • IsEmailAddress()
  • IsNumeric()
  • IsPhoneNumber()
  • IsSafeText()
  • IsSafeForSql()
  • IsSafeForXss()
  • IsUrl()
  • Min(int value)
  • Min(double value)
  • Min(decimal value)
  • Max(int/double/decimal/date value)
  • IsEmpty()
  • IsNotEmpty()
  • IsNull()
  • IsNotNull()
  • HasMinLength(int minLength)
  • HasMaxLength(int maxLength)
  • IsAfter(DateTime date)
  • IsBefore(DateTime date)
  • IsAfter(DateTime date)
  • IsBetween(DateTime startDate, DateTime endDate)

US Validation Extensions

  • IsUSState() - Checks values against a known list of US state codes (2 characters uppercase)

Fluent API Examples

Example using Result

            var input = 100; //user input to be validated
            var result = Validator.Eval(input, "Quantity Requested")
                .Required()
                .Min(50)
                .Max(100);
            if (result.IsValid == false)
            {
                Console.WriteLine(string.Join(";", result.ErrorMessages));
                //... add failure logic here
            }

Inline Example

            var input = "https://github.com/TjWheeler/LightVx"; //user input to be validated
            Validator.Eval(input, "Source Url")
                .Required()
                .IsUrl()
                .Success((() =>
                {
                    Console.WriteLine("Validation succeeded");
                }))
                .Fail((errors, validators) =>
                {
                    Console.WriteLine(string.Join(",", errors));
                    // Validation failed, put your failure logic here
                });

Examples using Validation Helper

            //WebApi - Validate that the text is matches Alphabet only
            var input = "ABC";
            string errorMessage;
            if (Validator.IsNotValid<AlphaTextValidator>(input, "First Name" , out errorMessage))
            {
                return BadRequest($"The input is invalid: {errorMessage}");
            }

Examples using Validators directly

Note: Although you can call the validators directly, using the Validation helper is more convenient.

            //Validate numberic
            string input = "123ABC";
            IValidator validator = new NumericValidator();
            validator.Validate(input, "My Field Name");
            if (!validator.IsValid)
            {
                Console.WriteLine("Input: " + input + " return error (" + validator.ErrorMessage + ")");
            }

Extending and creating your own validators

In the following example, we are inheriting from an AggregateValidator, this allows us to combine validators.

Creating a Post Code Validator by combining other validators.

//Step 1: Add the custom validator
public class PostCodeValidator : AggregatedValidator
    {
        public PostCodeValidator()
        {
            AddValidator(new LengthValidator(4, 4));
            AddValidator(new NumericValidator());
        }
    }

//Step 2: Add an extension method
public static class PostCodeValidatorExtension
    {
        public static ValidatorFluent IsPostCode(this ValidatorFluent fluentApi)
        {
            fluentApi.AddValidator(new PostCodeValidator());
            return fluentApi;
        }
    }

//Step 3: Call it to validate input

    public void ExampleCustomValidator()
    {
        string input = "...";
        var isValid = Validator.Eval(input, "MyFieldName")
            .Required()
            .IsPostCode()
            .Fail(((errors, validators) =>
            {
                Console.WriteLine("Example failure: " + string.Join(";", errors));
            })).IsValid;
    }

Creating your own validator

Create a class and inherit ValidatorBase. The only method you need to implement is Validate. There are some base methods that will make it easy to validate using Regular Expressions. Here's an example of one of the built in validators.

    /// <summary>
    ///     Validate Email Addresses
    /// </summary>
    public class EmailValidator : ValidatorBase
    {
        protected override string Expression => @"^([\&\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

        protected override void Validate()
        {
            if (_Input == null || (string) _Input == string.Empty)
            {
                Succeed();
                return;
            }

            if (SingleMatch((string) _Input))
                Succeed();
            else
                Fail("is not a valid email address.");
        }
    }

When using Regular expressions you can also use the HasMatch and MatchCount base methods.

Release Notes

https://github.com/TjWheeler/LightVx/blob/master/ReleaseNotes.md

  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.3 122 11/16/2020
2.0.2 353 5/28/2020
2.0.1 167 5/22/2020
2.0.0 166 4/21/2020
1.2.0 1,055 5/4/2018
1.1.1 555 4/27/2018
1.1.0 538 3/8/2018
1.0.3 506 2/20/2018
1.0.2 640 2/12/2018
1.0.1 566 2/11/2018
1.0.0 539 2/11/2018
Show less