Exemple #1
0
        /// <summary>
        /// The validate business rules.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="validationResults">The validation results.</param>
        protected override void ValidateBusinessRules(ModelBase instance, List <IBusinessRuleValidationResult> validationResults)
        {
            if (_validatorDescriptionAttribute.ValidationType == ValidationType.BusinessRule)
            {
                var validationContext = new global::FluentValidation.ValidationContext <ModelBase>(instance);
                var validationResult  = _validator.Validate(validationContext);
                if (!validationResult.IsValid)
                {
                    var dinstinctValidationResults = validationResult.Errors.Select(validationFailure => new BusinessRuleValidationResult(
                                                                                        _validatorDescriptionAttribute.ValidationResultType, validationFailure.ErrorMessage)
                    {
                        Tag = _validatorDescriptionAttribute.Tag
                    }).Distinct(new BusinessRuleValidationResultEqualityComparer()).Cast <IBusinessRuleValidationResult>();

                    validationResults.AddRange(dinstinctValidationResults);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// The validate fields.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="validationResults">The validation results.</param>
        protected override void ValidateFields(ModelBase instance, List <IFieldValidationResult> validationResults)
        {
            if (_validatorDescriptionAttribute.ValidationType == ValidationType.Field)
            {
                var validationContext = new global::FluentValidation.ValidationContext <ModelBase>(instance);
                var validationResult  = _validator.Validate(validationContext);
                if (!validationResult.IsValid)
                {
                    var distinctValidationResults = validationResult.Errors.Select(fieldValidationResult => new FieldValidationResult(
                                                                                       fieldValidationResult.PropertyName, _validatorDescriptionAttribute.ValidationResultType,
                                                                                       fieldValidationResult.ErrorMessage)
                    {
                        Tag = _validatorDescriptionAttribute.Tag
                    }).Distinct(new FieldValidationResultEqualityComparer()).Cast <IFieldValidationResult>();

                    validationResults.AddRange(distinctValidationResults);
                }
            }
        }