Example #1
0
 public virtual bool Validate(Rule rule, ValidationRule ruleProperty, TObject item, RuleResults rulesResults)
 {
     throw new Exception(CoreRessources.EX0002);
 }
Example #2
0
        /// <summary>
        ///   Valide une règle d'intégrité
        /// </summary>
        /// <typeparam name="TObject"> Type de l'objet associé à la validation </typeparam>
        /// <param name="ruleProperty"> Objet de type RuleToItem </param>
        /// <param name="objectToValidate"> Objet associé à la validation </param>
        /// <param name="originalObject"> Instance d'une classe de validation qui implémente l'interface IValidation </param>
        /// <param name="globalRuleResults"> Liste de résultats de validation. On peut ajouter la liste de RuleResult retournée par une autre validation à cette liste afin que les messages soient retournés. </param>
        /// <returns> Objet de type RuleResults encapsulant les validations qui n'ont pas été réussies </returns>
        private static void CheckValidation <TObject>(ValidationRule ruleProperty, TObject objectToValidate, IValidation <TObject> originalObject, RuleResults globalRuleResults)
        {
            try
            {
                var  listRuleResults = new RuleResults();
                bool result          = false;

                switch (ruleProperty.Rule.Type)
                {
                case Rule.RuleType.CustomRules:
                    result = originalObject.Validate(ruleProperty.Rule, ruleProperty, objectToValidate, globalRuleResults);
                    break;

                case Rule.RuleType.ObjectRequired:
                    result = CommonRules.ObjectRequired(objectToValidate, ruleProperty.PropertyName);
                    break;

                case Rule.RuleType.StringRequired:
                    result = CommonRules.StringRequired(objectToValidate, ruleProperty.PropertyName);
                    break;

                case Rule.RuleType.StringMaxLength:
                    result = CommonRules.StringMaxLength(objectToValidate, ruleProperty.PropertyName,
                                                         (int)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateFormat:
                    result = CommonRules.DateFormat(objectToValidate, ruleProperty.PropertyName,
                                                    (string)ruleProperty.Rule.Parameter,
                                                    ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.IntegerMaxValue:
                    result = CommonRules.IntegerMaxValue(objectToValidate, ruleProperty.PropertyName,
                                                         (int)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.IntergerMinValue:
                    result = CommonRules.IntegerMinValue(objectToValidate, ruleProperty.PropertyName,
                                                         (int)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.StringMinLength:
                    result = CommonRules.StringMinLength(objectToValidate, ruleProperty.PropertyName,
                                                         (int)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.PostalCode:
                    result = CommonRules.CodePostal(objectToValidate, ruleProperty.PropertyName,
                                                    ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.PhoneNumber:
                    result = CommonRules.PhoneNumber(objectToValidate, ruleProperty.PropertyName,
                                                     ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.EmailAddress:
                    result = CommonRules.EmailAddress(objectToValidate, ruleProperty.PropertyName,
                                                      ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.RegularExpression:
                    result = CommonRules.RegularExpression(objectToValidate, ruleProperty.PropertyName,
                                                           (string)ruleProperty.Rule.Parameter,
                                                           ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateGreaterThan:
                    result = CommonRules.DateGreaterThan(objectToValidate, ruleProperty.PropertyName,
                                                         (DateTime)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateGreaterOrEqualThan:
                    result = CommonRules.DateGreaterOrEqualThan(objectToValidate, ruleProperty.PropertyName,
                                                                (DateTime)ruleProperty.Rule.Parameter,
                                                                ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateLessThan:
                    result = CommonRules.DateLessThan(objectToValidate, ruleProperty.PropertyName,
                                                      (DateTime)ruleProperty.Rule.Parameter,
                                                      ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateLessOrEqualThan:
                    result = CommonRules.DateLessOrEqualThan(objectToValidate, ruleProperty.PropertyName,
                                                             (DateTime)ruleProperty.Rule.Parameter,
                                                             ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DateRequired:
                    result = CommonRules.DateRequired(objectToValidate, ruleProperty.PropertyName,
                                                      ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.Luhn:
                    result = CommonRules.Luhn(objectToValidate, ruleProperty.PropertyName,
                                              ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.ListNotEmpty:
                    result = CommonRules.ListNotEmpty(objectToValidate, ruleProperty.PropertyName,
                                                      ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.ObjectContainsOneValidProperty:
                    result = CommonRules.ObjectContainsOneValidProperty(objectToValidate,
                                                                        (List <string>)ruleProperty.Rule.Parameter,
                                                                        ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.StringExactLength:
                    result = CommonRules.StringExactLength(objectToValidate, ruleProperty.PropertyName,
                                                           (int)ruleProperty.Rule.Parameter,
                                                           ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DecimalMaxValue:
                    result = CommonRules.DecimalMaxValue(objectToValidate, ruleProperty.PropertyName,
                                                         (decimal)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DecimalMinValue:
                    result = CommonRules.DecimalMinValue(objectToValidate, ruleProperty.PropertyName,
                                                         (decimal)ruleProperty.Rule.Parameter,
                                                         ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DoubleMaxValue:
                    result = CommonRules.DoubleMaxValue(objectToValidate, ruleProperty.PropertyName,
                                                        (double)ruleProperty.Rule.Parameter,
                                                        ruleProperty.Rule.AllowNull);
                    break;

                case Rule.RuleType.DoubleMinValue:
                    result = CommonRules.DoubleMinValue(objectToValidate, ruleProperty.PropertyName,
                                                        (double)ruleProperty.Rule.Parameter,
                                                        ruleProperty.Rule.AllowNull);
                    break;
                }

                if (result == false)
                {
                    //Effectue un remplacement d'argument dans la description de la règle par le nom convivial de la propriété. Recherche le tag {0}
                    Rule rule = ruleProperty.Rule.CloneObject();

                    rule.Description = string.Format(rule.Description, ruleProperty.FriendlyName, ruleProperty.FriendlyName1, ruleProperty.FriendlyName2);

                    listRuleResults.Add(new RuleResult(rule,
                                                       typeof(TObject).ToString(),
                                                       ruleProperty.PropertyName,
                                                       typeof(TObject).Name,
                                                       ruleProperty.BindingObjectName ?? typeof(TObject).Name,
                                                       ruleProperty.BindingPropertyName ?? ruleProperty.PropertyName));
                }
                globalRuleResults.Add(listRuleResults);
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(NotImplementedException))
                {
                    throw new ValidationException(ruleProperty.Rule.Type, ruleProperty.PropertyName, typeof(TObject), ex);
                }

                throw;
            }
        }