Skip to content

Instantly share code, notes, and snippets.

@kdowswell
Created April 30, 2016 16:28
Show Gist options
  • Save kdowswell/ed7f9adeb1f03b849e899ad9a9a14539 to your computer and use it in GitHub Desktop.
Save kdowswell/ed7f9adeb1f03b849e899ad9a9a14539 to your computer and use it in GitHub Desktop.
public abstract class MessageValidator<IRequest>
{
private readonly DataAnnotationsValidator _dataAnnotationsValidator;
protected ValidationResult _result;
protected MessageValidator()
{
_dataAnnotationsValidator = new DataAnnotationsValidator();
_result = new ValidationResult();
}
public bool ValidateAnnotations(IRequest message)
{
List<System.ComponentModel.DataAnnotations.ValidationResult> dataAnnotationValidationResults;
var success = _dataAnnotationsValidator.TryValidate(message, out dataAnnotationValidationResults);
AddDataAnnotationValidationResultsToValidationResultObject(dataAnnotationValidationResults);
return success;
}
private void AddDataAnnotationValidationResultsToValidationResultObject(
List<System.ComponentModel.DataAnnotations.ValidationResult> dataAnnotationValidationResults)
{
foreach (var item in dataAnnotationValidationResults)
{
_result.AddError(new ValidationFailure(item.MemberNames.FirstOrDefault(), item.ErrorMessage));
}
}
public abstract ValidationResult Validate(IRequest message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment