Created
December 9, 2013 23:25
-
-
Save feanz/7882989 to your computer and use it in GitHub Desktop.
Recursive Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateObjectAttribute: ValidationAttribute { | |
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { | |
var results = new List<ValidationResult>(); | |
var context = new ValidationContext(value, null, null); | |
Validator.TryValidateObject(value, context, results, true); | |
if (results.Count != 0) { | |
var compositeResults = new CompositeValidationResult(String.Format("Validation for {0} failed!", validationContext.DisplayName)); | |
results.ForEach(compositeResults.AddResult); | |
return compositeResults; | |
} | |
return ValidationResult.Success; | |
} | |
} | |
public class CompositeValidationResult: ValidationResult { | |
private readonly List<ValidationResult> _results = new List<ValidationResult>(); | |
public IEnumerable<ValidationResult> Results { | |
get { | |
return _results; | |
} | |
} | |
public CompositeValidationResult(string errorMessage) : base(errorMessage) {} | |
public CompositeValidationResult(string errorMessage, IEnumerable<string> memberNames) : base(errorMessage, memberNames) {} | |
protected CompositeValidationResult(ValidationResult validationResult) : base(validationResult) {} | |
public void AddResult(ValidationResult validationResult) { | |
_results.Add(validationResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment