SimpleDataValidation 0.3.0

dotnet add package SimpleDataValidation --version 0.3.0
                    
NuGet\Install-Package SimpleDataValidation -Version 0.3.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="SimpleDataValidation" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleDataValidation" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleDataValidation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SimpleDataValidation --version 0.3.0
                    
#r "nuget: SimpleDataValidation, 0.3.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.
#:package SimpleDataValidation@0.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SimpleDataValidation&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleDataValidation&version=0.3.0
                    
Install as a Cake Tool

What is SimpleDataValidation?

SimpleDataValidation is an open-source .NET library for validating data in a simple and intuitive way.

It's written in C#/NET8 with VS2026. The code is covered by 63 unit tests to ensure the non-regression of incoming evolutions.

A quick example

The usage is very simple, you just need to call one of validation static method of the V class, passing the name of the data and its value. Then if the validation failed, the function returns an error message or an empty string if validation finished successfully.

Syntax:
string errMsg = V.Function(DataName, Value);

If the validation fails  -> The error message is returned in the errMsg variable. 
If the validation passes -> An empty string is returned.

Check easily if an error occurs:

Syntax:
string errMsg = V.Function(DataName, Value);

// if an error occurs, there is a message
if(V.Error(errMsg)) DoSomethingError();

// no error, no message
if(V.Ok(errMsg)) DoSomethingOk(); 

The following example shows some usual cases:

using SimpleDataValidation;

//--Validate that the string value contains a text, it is not null, not empty and without any whitespace.
errMsg = V.Text("Name", "John");

if(V.Error(errMsg))
	Console.WriteLine($"-> errorMessage: {errMsg}");   <- Not diplayed

if(V.Ok(errMsg))
	Console.WriteLine($"The Name value is correct!");  <- Displayed

//--Validate that the value has 8 chars length minimum
errMsg = V.MinLength("Name", "John", 8);

if(V.Error(errMsg))
	Console.WriteLine($"-> errorMessage: {errMsg}");   <- Displayed


> Output:
The Name value is correct!
-> errorMessage: Name must be at least 8 characters long

Another way to validate data

You can also do several checks in a more fluent way, by chaining the validation methods to validate a data value many times.

// Validate a string value to check if it's not null and has a minimum length of 8 characters
errMsg = V.Define("Name", "John").AddNotNull().AddMinLength(8).Validate();

errMsg = V.Define("Name", "john").AddNotNull().AddStartsWith("jo").Validate();

Start by calling the validation static method Define of the V class, passing the name of the data and its value. Then, you can chain the validation methods to validate the data value many times. Finally, call the Validate method to perform the validation and return the error message if any.

All validation rules will be executed. Error messages will be concatened.

Features

There are many available features to validate data, by type, by length, by format and more.

Available validation methods

// List of available direct validation methods:
Null                       Check that the object value is null.
NotNull                    Check that the object value is not null.
NotNullOrEmpty             Check that the string value is not null or empty.
NotNullOrWhiteSpace        Check that the string value is not null or whitespace.
Text                       Check that the string contains a text, same as NotNullOrWhiteSpace.

Letters                    Check that the string contains only letter.
Digits                     Check that the string contains only digit.
LettersDigitsUnderscore    Check that the string contains only letter, digit or underscore.

StartsWith                 Check that the string starts with the provided string, case sensitive. 
StartsWithCaseInsensitive  Check that the string starts with the provided string, case insensitive.
EndsWith                   Check that the string ends with the provided string, case sensitive. 
EndsWithCaseInsensitive    Check that the string ends with the provided string, case insensitive.
Contains                   Check that the string contains the provided string, case sensitive. 
ContainsCaseInsensitive    Check that the string contains the provided string, case insensitive. 
NotContains                Check that the string does not contain the provided string, case sensitive.
NotContainsCaseInsensitive Check that the string does not contain the provided string, case insensitive.

FirstCharIn                Check that the first char of the string is one of provided chars.
FirstCharNotIn             Check that the first char of the string is not one of provided chars.

MinLength                  Check that the string value has a minimum length.
MaxLength                  Check that the string value has a maximum length.
BetweenLength              Check that the length of string is between a min and a max value.
ExactLength                Check that the string value has an exact length.

EmailFormat                Check that the email format is valid: a@b.c
Email                      Check that the string value is a valid email address.
EmailStrict                Check that the email format is valid , more strict but more time consuming.
 
Bool                       Check that the object value is a boolean.
Int                        Check that the object value is an integer.
Number                     Check that the object value is a number: integer, double, float or BigInteger.
String                     Check that the object value is a string.

FormatDate                 Check that the string match the provided (one or more) date format.

BankIban                   Check that the string value is a valid bank IBAN number.
BankCardNumber             Check that the string is a valid bank card number.


// List of available chained validation methods:
AddNull
AddNotNull
AddNotNullOrEmpty
AddNotNullOrWhiteSpace
AddText

AddStartsWith
AddStartsWithCaseInsensitive
AddEndsWith
AddEndsWithCaseInsensitive
AddContains                    
AddContainsCaseInsensitive     
AddNotContains                
AddNotContainsCaseInsensitive 

AddFirstCharIn
AddFirstCharNotIn

AddMinLength
AddMaxLength
AddBetweenLength
AddExactLength
AddBankCardNumber

Error Messages

English, French Spanish, German, Italian, Portuguese, Brazilian, Chinese and Hindi languages are supported for error messages, other languages will be added in the future.

You can also set your own error messages for each validation method, by passing the error message as a parameter to the validation method.

string nameValue = null;
errMsg = V.NotNull("Name",nameValue, "The name is mandatory!");
Console.WriteLine($"-> errorMessage: {errMsg}");

// will display the following error message:
-> errorMessage: The name is mandatory!

The default language for error messages is English, but you can change it to another one by calling the SetLanguage method of the V class, passing the language code as a parameter.

// list of available languages:
"en-en"  English   default
"fr-fr"  French
"es-es"  Spanish
"de-de"  German
"it-it"  Italian
"pt-pt"  Portuguese
"pt-br"  Brazilian Portuguese
"zh-cn"  Chinese
"hi-in"  Hindi 

// example: Set the language for error messages to be in French
V.SetLanguage("fr-fr");

You can also set the end of the error message with a dot or not, by calling the SetEndMessageDot method of the V class, passing a boolean value as a parameter.

Below are results of the error message: The name is required.

// by default:
The name is required
// put a . char at the end of each error message
V.SetMessageEndDot();
The name is required.

Or put something else:

// put a ! char at the end of each error message
V.SetMessageEnd("!");
The name is required!

Modify the separator between messages when using a chain validation. By default, put a - between messages.

// now put a | between messages if many errors occurs
V.SetMessageSeparator(" | ");

example:
The name is required | The length of name is too short | ....

Some samples

Check string content

Do some checks on a string value.

// Check that the value starts with the provided string.
errMsg = V.StartsWith("field", "hello", "hgj");  -> ok, errMsg will be empty

// Check that the value first char is one of 'xyz'.
errMsg = V.FirstCharIn("field", "hello", "xyz");  -> error! 

Check date

Check if a date match the provided format:

// one format to match: will be ok
errMsg = V.FormatDate("name", "10/12/2026", "dd/MM/yyyy");
 -> ok
 
// the date can match one format among the both provided
errMsg = V.FormatDate("name", "2026-10-25", new[] { "dd/MM/yyyy", "yyyy-MM-dd" });
 -> ok

// the date can match one format among the both provided, if an error occurs, return the custom message
errMsg = V.FormatDate("name", "10/12/202", new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, "Custom error message");
 -> The date is wrong: errMsg will be: "Custom error message"

// a more complex case: match date and time
errMsg = V.FormatDate("name", "2026-12-20 10:30:00,123", "yyyy-MM-dd HH:mm:ss,fff");
 -> ok

Check bank Iban and card Number

Check if an Iban number is valid: With or without space chars.

// with whitespaces:
string iban = "GB82 WEST 1234 5698 7654 32";
errMsg = V.BankIban("field", iban);  -> ok

iban = "GB82WEST12345698765432";
errMsg = V.BankIban("field", iban);  -> ok

Check if a bank card number is valid:

Should contains only digits and char space. Verify the length and the check sum, apply the Luhn algo.

// the card number bank should be valid over all banks, with whitespaces:
errMsg = V.BankCardNumber("field", "4111 1111 4555 1142");

// the card number bank should be Visa, with whitespaces:
errMsg = V.BankCardNumber("field", "4111 1111 4555 1142", CardBank.Visa);

errMsg = V.BankCardNumber("field", "4111111145551142", CardBank.Visa);

// the card number bank should be Visa or Mastercard
errMsg = V.BankCardNumber("field", "4111 1111 4555 1142", CardBank.Visa | CardBank.Mastercard);

// the card number bank should be Visa or Mastercard or American Express
errMsg = V.BankCardNumber("field", "4111 1111 4555 1142", CardBank.Visa | CardBank.Mastercard | CardBank.AmericanExpress);

Bank validation implemented: Visa, Mastercard, American Express, Dinners Club, Discover, JCB and China UnionPay.

Apply several validation on a data

You can also do several checks in a more fluent way, by chaining the validation methods to validate a data value many times. By default, every validation rule will be applied, error messages will concatenated, separated by default by ' - '. but you can change this by calling the SetMessageSeparator function.

But you can stop the validation after the first error, by using the option: ValidOptions.StopOnFirstError.

errMsg = V.Define("Card Number", "4111 1111 4555 1142").AddNotNull().AddCardNumber(CardBank.Visa).Validate();
 -> ok

// continue on error, so the max length validation will be executed and the error message will be concatenated
errMsg = V.Define("field", "johnjohn").AddMinLength(10).AddMaxLength(5).Validate();
 -> errMsg contains all error messages: "field must be at least 10 characters long - field must be no more than 5 characters long"

// will stop on the first error, so the max length validation will not be executed
errMsg = V.Define("field", "johnjohn").AddMinLength(10).AddMaxLength(4).Validate(ValidOptions.StopOnFirstError);
  -> errMsg contains only the first error: "field must be at least 10 characters long"

Source code, documentation and nuget

SimpleDataValidation library is packaged as a nuget ready to use:

https://www.nuget.org/packages/SimpleDataValidation

Github source repository:

https://github.com/Pierlam/SimpleDataValidation/

Contact

Have a comment to do? A question to ask? Want to request a new feature?

Error messages were generated by AI, maybe some of them are wrong or not clear, please let me know to fix it.

Send an email to:

pierlam-project@outlook.com

Resources

A dotnet well-known data validation library: https://docs.fluentvalidation.net/en/latest/index.html

Iban validation: https://medium.com/@matlabb/iban-structure-and-mod-97-validation-algorithm-719e3d4db5f2

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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
0.3.0 52 11/3/2026
0.2.0 72 10/17/2026
0.1.0 72 10/10/2026