public ValidationResult Validate(TEntity entity) { var result = new ValidationResult(); foreach (var validation in _validations) { var res = validation.IsValid(entity); if (!res.Sucess) { result.AddErrors(res.Errors); } } return(result); }
public ValidationResult ValidateAddress(Address address) { var result = new ValidationResult(); if (string.IsNullOrEmpty(address.Line1)) { result.AddError("Address line 1 is empty."); } if (string.IsNullOrEmpty(address.City)) { result.AddError("The city is empty."); } var postalCodeValidation = _postalCodeValidator.ValidatePostalCode(address); if (postalCodeValidation.HasErrors) { result.AddErrors(postalCodeValidation.Errors); } return(result); }
/// <summary> /// Evaluates the rule for the given request. The <b>AndRule</b> accumulates the /// results of all of its child rules. It evaluates all of the child rules /// regardless of whether or not any particular rule passes or fails. /// </summary> /// <param name="request">The <b>MvcRequest</b> to evaluate the rule for.</param> /// <returns>A <b>ValidationResult</b> indicating the result of evaluating the /// rule and any validation errors that occurred.</returns> public override ValidationResult Evaluate( MvcRequest request) { ValidationResult result = new ValidationResult(); foreach (IValidationRule rule in children) { // evaluate the child rule ValidationResult childResult = rule.Evaluate(request); // if the child rule failed, set this rule to failure and add the // error(s) from the child if (!childResult.Passed) { result.Passed = false; result.AddErrors(childResult.Errors); if (rule.StopOnFail || childResult.StopProcessing) { result.StopProcessing = true; break; } } } return result; }
/// <summary> /// Evaluates the rule for the given request. The <b>OrRule</b> short circuits /// on the first successful child rule, with a passed status. /// </summary> /// <param name="request">The <b>MvcRequest</b> to evaluate the rule for.</param> /// <returns>A <b>ValidationResult</b> indicating the result of evaluating the /// rule and any validation errors that occurred.</returns> public override ValidationResult Evaluate( MvcRequest request) { ValidationResult result = new ValidationResult { Passed = false }; // TODO: if cnt is 0 should we return true or false?? int cnt = children.Count; for (int k = 0; k < cnt && !result.Passed; k++) { IValidationRule rule = children[k]; // evaluate the child rule ValidationResult childResult = rule.Evaluate(request); // clear out any previous validation errors result.ClearErrors(); // if the child rule passed, set this rule to passed, // if not, add the errors from the child to the OrRule's result if (childResult.Passed) { result.Passed = true; } else { result.AddErrors(childResult.Errors); if (rule.StopOnFail || childResult.StopProcessing) { result.StopProcessing = true; break; } } } // if the OrRule did not pass and the is a specific error identified // for the OrRule, replace any errors from the children with the one // for the OrRule if (!result.Passed && (ErrorId != 0)) { result.ClearErrors(); result.AddError(Field, ErrorId); } return result; }