DEV Community

Cover image for C# Why to Validate Data?
Amina Elsheikh
Amina Elsheikh

Posted on

C# Why to Validate Data?


Having accurate, high quality source data means that we will generate data that is clean, helpful, consistent, and accurate aiming to avoid data loss and errors during the using of the data, migrating and merging data.


Data Validation is the process of making sure that the data source is accurate before collecting, using, importing, analyzing, or preparing data.

Importance of validation

  • Reducing Data Loss.
  • Producing accurate Data.
  • Avoiding Errors.
  • Making decision based on wrong data.

Some Types of Validations

  • Data Type Validation Validating emails, numeric, IPs, URL an so on.
  • Null-ability Validation Checking data if null or not selected.
  • Range Validation Validate whether input data falls within a predefined range.
  • Consistency Validation Confirms the data has been entered in a logically consistent way. For example, date is not in the future or password is follow the predefined rules.
  • Uniqueness Validation Ensures that an item is not entered multiple times into a database.
  • Length Validation Ensures that the appropriate number of characters are entered into the field.


Although data Validation is a critical step in any data workflow, but also it may slow down the developing time and my be boring for some of us because of having to redo it in every application.

We can save our developing time and make it less boring by using one the data validation libraries a available online to be installed and used.

In the fallowing example I will use DEFC.Util.DataValidation library as an example of those libraries. this library is available on nuget.

Install-Package DEFC.Util.DataValidation -Version 1.0.0
Enter fullscreen mode Exit fullscreen mode


Console application with full example available at the GitHub repository.

using DEFC.Util.DataValidation;
using System.Text.RegularExpressions;
Enter fullscreen mode Exit fullscreen mode

 public static void Validator()
            //Sample of data type validator
            bool IsValidAlphanumeric= DataType.IsAlphanumeric("Foo1234");
            bool IsValidGUID = DataType.IsGUID("am I a GUID");
            bool IsValidIPv4 = DataType.IsIPv4("");
            bool IsValidURL = DataType.IsURL("");
            //Sample of math validator
            bool IsValidNegative = Math.IsNegative(-1);
            bool IsValidEven = Math.IsEven(9);
            //Sample of comparison validator
            bool IsBetween = Comparison.IsBetween(4,2,10);
            bool IsLessThanOrEqual = Comparison.IsLessThanOrEqual(12,3);
            //Sample of SQL Injection validator
            bool HasSQLInjection = SQLInjection.IsExists("' or 1=1");
            //Sample of Regular Expression validator
            bool IsValidExpression = RegularExpression.IsMatch("Foo1234",new Regex("[a-zA-Z0-9]*$"));
            //Sample of Password validator
            bool Isvalid = Password.ValidatRules(new PasswordRules() 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)