Skip to content

Instantly share code, notes, and snippets.

@Char0394
Created June 1, 2020 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Char0394/8f08e0498a0f0012b35297ff736d09d4 to your computer and use it in GitHub Desktop.
Save Char0394/8f08e0498a0f0012b35297ff736d09d4 to your computer and use it in GitHub Desktop.
public class ValidatablePair<T> : IValidatable<ValidatablePair<T>>
{
public List<IValidationRule<ValidatablePair<T>>> Validations { get; } = new List<IValidationRule<ValidatablePair<T>>>();
public bool IsValid { get; set; } = true;
public List<string> Errors { get; set; } = new List<string>();
public ValidatableObject<T> Item1 { get; set; } = new ValidatableObject<T>();
public ValidatableObject<T> Item2 { get; set; } = new ValidatableObject<T>();
public event PropertyChangedEventHandler PropertyChanged;
public bool Validate()
{
var item1IsValid = Item1.Validate();
var item2IsValid = Item2.Validate();
if (item1IsValid && item2IsValid)
{
Errors.Clear();
IEnumerable<string> errors = Validations.Where(v => !v.Check(this))
.Select(v => v.ValidationMessage);
Errors = errors.ToList();
Item2.Errors = Errors;
Item2.IsValid = !Errors.Any();
}
IsValid = !Item1.Errors.Any() && !Item2.Errors.Any();
return this.IsValid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment