RecordValidator 2.0.0

dotnet add package RecordValidator --version 2.0.0                
NuGet\Install-Package RecordValidator -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RecordValidator" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RecordValidator --version 2.0.0                
#r "nuget: RecordValidator, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RecordValidator as a Cake Addin
#addin nuget:?package=RecordValidator&version=2.0.0

// Install RecordValidator as a Cake Tool
#tool nuget:?package=RecordValidator&version=2.0.0                

RecordValidator

RecordValidator is a .NET library for validating data against predefined rules.

public class Student{

        [Mandatory(validateForDefaultValue:true)]
        public  int  Id { get; set; }
        [Mandatory]
        public  string  Name { get; set; }
        [Mandatory(errorMessage:"Email is mandatory")]
        [Email]
        public  string  Email { get; set; }
        [Date(minDate:"2000-01-01")]
        public  DateTime  Doj { get; set; }
        [Mandatory]
        [RegularExpression(pattern: "^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[6789]\\d{9}$")]
        public  string  Mobile { get; set; }
        public  bool  IActive { get; set; }
        [Length(min:10, max:100)]
        public  string  Address { get; set; }
        [RegularExpression(pattern: "^[1-9][0-9]{5}$")]
        public  string  Pincode { get; set; }
        [DataRange(min: 0, max: 100)]
        public int Age { get; set; }

    }

Now call the Validate with throwException parameter value as true function

try
{
    var std=new Student()
    {
        Name="",
        Address="TH",
        Doj=new DateTime(1999,01,01),
        Email="Vishal",
        IActive=true,
        Id=0,
        Mobile="3949132",
        Pincode="0002345",
        Age=10
    };
    std.Validate(throwException:true);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Or you can use ValidateValueAndReturnListOfExceptions() to get the exceptions in case if you don't want to break your flow

try
{
    var std=new Student()
    {
        Name="",
        Address="TH",
        Doj=new DateTime(1999,01,01),
        Email="Vishal",
        IActive=true,
        Id=0,
        Mobile="3949132",
        Pincode="0002345",
        Age=10

    };
    
    var result=std.Validate(throwException:false);
    //you can now use the list to log the errors 
    foreach(var exception in result)
    {
        Console.WriteLine($"Error - {exception.ErrorMessage} against {exception.Param}");
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Single value validations can also be done

//Throws exception if mandatory validation fails
var name = "";
name.ValidateValue(new ValidationBuilder().ValidateMandatory(), throwException: true, paramName: "name");

// Returns RecordValidatorException object with error details if mandatory validation fails
var name = "";
var error = name.ValidateValue(new ValidationBuilder().ValidateMandatory(errorMessage:"Please provide name "), paramName: "name");

// passing multiple validation rules in the single line to validate for all the given rules and resturns the list of RecordValidatorException object with error details 
var name = "";
var validationBuilder= new ValidationBuilder().ValidateMandatory().ValidateLength(min:3,max:20);
var errors = name.ValidateValue(validationBuilder, paramName: "name");



public class Company
{
   public List<EmployeeData> Employees { get; set; }
}

public class EmployeeData
{
    [Mandatory]
    [Length(max: 100)]
    public string Name { get; set; }
    [Mandatory]
    [Length(min:4,max: 6)]
    public string Gender { get; set; }

}

var company=new Company();
company.Employees=new List<EmployeeData>()
{
     new EmployeeData()
    {
        Name="Vishal",
        Gender="Male"
    },
    new EmployeeData()
    {
        Name=string.Empty,
        Gender=string.Empty
    }

};


//it will validate Employee data also against the given Record Validator attribute.
company.Validate(throwException:true)



List of validations supported in the latest version

  • Mandatory
  • Range(DataRange)
  • Date
  • Email
  • Length
  • Regular Expression(RegularExpression)
  • more will be added in the coming versions

New Changes in 2.0.0

  • Custom validation attribute
    • Now you can create custom validation attribute.
  • Performance improvement


//Inherit RecordValidatorBase class 
[AttributeUsage(AttributeTargets.Property)]
public class CustomValidationAttribute : RecordValidatorBase
{
    public CustomValidationAttribute(string errorMessage = "") : base(errorMessage)
    {
    }

    //Override the Validate method and put your validation logic inside this method
    public override RecordValidatorException Validate(object value, string param)
    {

        var errroMessage = ErrorMessage;
        if (string.IsNullOrEmpty(errroMessage))
        {
            errroMessage = $"Invalid data ";
            // in case validation fails then always return RecordValidatorException class object
            return new RecordValidatorException() { ErrorMessage = errroMessage, Param = param };
        }
           
        return null;
    }

    
}

Features

  • Support for various validation rules and will be added more in coming versions.
  • Easy to use
  • Custom error message can be set, if it is not given then default will be taken.
  • Custom validation attribute support

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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 Downloads Last updated
2.0.0 89 1/5/2025
1.5.0 135 7/6/2024
1.4.0 120 6/15/2024
1.3.0 103 6/9/2024
1.2.0 99 5/20/2024
1.1.0 142 5/5/2024
1.0.2 82 5/2/2024
1.0.1 116 4/27/2024
1.0.0 116 4/21/2024