I have a customer class which has both PhoneNumber and Email properties. Using DataAnnotations I can decorate the properties with DataType validation attributes, but I cannot see what that is getting me.
For example:
[DataType(DataType.PhoneNumber)]
public string PhoneNumber {get; set;}
I have a unit test that assigned "1515999A" to this property. When I step through the validation runner the value is considered valid for a phone number. I would have thought this should be invalid.
I google'd around some but couldn't find a decent explanation of what the various enumerated DataTypes actually catch. Is there a worthwhile reference somewhere?
Edit:
Here are the guts of what I'm using for a validation runner...
public virtual XLValidationIssues ValidateAttributes<TEntity>(TEntity entity)
{
var validationIssues = new XLValidationIssues();
// Get list of properties from validationModel
var props = entity.GetType().GetProperties();
// Perform validation on each property
foreach (var prop in props)
ValidateProperty(validationIssues, entity, prop);
// Return the list
return validationIssues;
}
protected virtual void ValidateProperty<TEntity>(XLValidationIssues validationIssues, TEntity entity, PropertyInfo property)
{
// Get list of validator attributes
var validators = property.GetCustomAttributes(typeof(ValidationAttribute), true);
foreach (ValidationAttribute validator in validators)
ValidateValidator(validationIssues, entity, property, validator);
}
protected virtual void ValidateValidator<TEntity>(XLValidationIssues validationIssues, TEntity entity, PropertyInfo property, ValidationAttribute validator)
{
var value = property.GetValue(entity, null);
if (!validator.IsValid(value))
validationIssues.Add(new XLValidationIssue(property.Name, value, validator.FormatErrorMessage(property.Name, value)));
}