RecordValidator 1.5.0

dotnet add package RecordValidator --version 1.5.0                
NuGet\Install-Package RecordValidator -Version 1.5.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="1.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RecordValidator --version 1.5.0                
#r "nuget: RecordValidator, 1.5.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=1.5.0

// Install RecordValidator as a Cake Tool
#tool nuget:?package=RecordValidator&version=1.5.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 ValidateValueAndThrowException 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.ValidateValueAndThrowException();
}
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.ValidateValueAndReturnListOfExceptions();
    //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 Mandatory(), throwException: true, paramName: "name");

// Returns RecordValidatorException object with error details if mandatory validation fails
var name = "";
var error = name.ValidateValue(new Mandatory(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 validations= new List<ValidationBase>()
{
   new Mandatory(){},
   new Length(min:3,max:20),
};

var errors = name.ValidateValue(validations, 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.ValidateValueAndThrowException()



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 1.5.0

  • Optimized code

Features

  • Support for various validation rules and will be added more in coming versions.
  • Easy to add in the project and can be easily added in existing projects.
  • Custom error message can be set if it is not given then default will be taken.

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. 
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
1.5.0 121 7/6/2024
1.4.0 110 6/15/2024
1.3.0 93 6/9/2024
1.2.0 89 5/20/2024
1.1.0 130 5/5/2024
1.0.2 72 5/2/2024
1.0.1 107 4/27/2024
1.0.0 105 4/21/2024