RecordValidator 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RecordValidator --version 1.2.0                
NuGet\Install-Package RecordValidator -Version 1.2.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.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RecordValidator --version 1.2.0                
#r "nuget: RecordValidator, 1.2.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.2.0

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

RecordValidator

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

Define the rule for the properties for the class

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; }

    }

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"
    };
    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"
    };
    
    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);
}

New Changes in 1.2.0

Added single value validations

// We have multiple validation rules like Mandatory such as DateValidation, Email, Length, RegularExpressionValidation
// All the above rules can be configured as below in the list
List<ValidationBase> validationsTypes = new List<ValidationBase>(){ new Mandatory()
{
    ErrorMessage = "Field is Mandatory",
    ValidateForDefault = false
}};

string name = "";
name.ValidateValue(validationsTypes, paramName: nameof(name),throwException:true);//throws exception if validation failed
var result = name.ValidateValue(validationsTypes, paramName: nameof(name), throwException: true);//return list of errors if validation failed

Added complex object validations

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.  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