-
Notifications
You must be signed in to change notification settings - Fork 0
Using the Library: Defining the Validation Rules
Consider the below Employee class.
public class Employee
{
public String FirstName { get; set; }
public String MiddleName { get; set; }
public String LastName { get; set; }
public String Email { get; set; }
public Int16 YearsExperience { get; set; }
public Single Rating { get; set; }
public DateTime DateEmployed { get; set; }
}
We define the validation rules via custom attributes to each of the class properties we wish to automate the validations with. Below is how a fully-defined class shall look like after rules have been defined.
[StringValidator("First Name", 5, 50, IsRequired = true)]
public String FirstName { get; set; }
[StringValidator("Middle Name", 5, 20)]
public String MiddleName { get; set; }
For this class, we define validation rules using the StringValidatorAttribute (and other corresponding attributes) of the validations library. To apply size validations to the name fields, we define them as follows:
The above definition indicates that the FirstName field should be between 5 to 50 characters in length, inclusive, and is a required field, while the MiddleName field should be between 5 to 20 characters in length, inclusive, but this time is an optional field as the IsRequired property is not set.
All the validator attributes have the same first constructor argument: the FieldLabel. The FieldLabel is the friendly display name when referring to this property during validation operations. For StringValidatorAttribute, the second and third arguments define the minimum and maximum text lengths. There are other optional settings for the various attributes, like the IsRequired, which defines the property as a required input, among others.
Below is the entire Employee class with appropriate validator attributes defined.
public class Employee
{
[StringValidator("First Name", 5, 50, IsRequired = true)]
public String FirstName { get; set; }
[StringValidator("Middle Name", 5, 20)]
public String MiddleName { get; set; }
[StringValidator("Last Name", 5, 20, IsRequired = true)]
public String LastName { get; set; }
[EmailValidator("Email", 5, 20, IsRequired = true)]
public String Email { get; set; }
[IntValidator("Years of Experience", 0, 30)]
public Int16 YearsExperience { get; set; }
[DoubleValidator("Rating", 0.0, 5.0, IsRequired = true)]
public Single Rating { get; set; }
[DateValidator("Date Employed")]
public DateTime DateEmployed { get; set; }
}
This configures the validation rules to be used for each field in the data entry pages of our application.
Of note is IntValidationAttribute, whose minimum/maximum values are overridden internally as needed, depending on the scope of the Int type decorated. For example, if this attribute is decorating an Int16 property but was defined with a maximum value more than the maximum value of an Int16, internally the validator shall override the defined maximum to instead use Int16.MaxValue.