Exemple #1
0
        /// <summary>
        /// Returns the ValidationRule objects from an <see cref="AbstractValidator{T}"/> that are relevant to the given <see cref="Validatable{T}"/> objects.
        /// </summary>
        /// <typeparam name="T">The Type of the class that the <see cref="Validatable{T}"/>object properties pertains to, and therefore is the target Type for the <see cref="AbstractValidator{T}"/>.</typeparam>
        /// <param name="validator">The Fluent <see cref="AbstractValidator{T}"/> instance.</param>
        /// <param name="validatableGroup">The <see cref="Validatable{T}"/> objects for which to retrieve ValidationRules for.</param>
        /// <returns>A List of IValidationRule relevant to the <see cref="Validatable{T}"/> objects.</returns>
        public static List <IValidationRule> GetRulesFor <T>(this AbstractValidator <T> validator, Validatables validatableGroup)
        {
            var descriptor = validator.CreateDescriptor();

            var rules = new List <IValidationRule>();

            foreach (var obj in validatableGroup.Objects)
            {
                rules.AddRange(descriptor.GetRulesForMember(obj.ClassPropertyName));
            }

            return(rules);
        }
Exemple #2
0
        public static string GetJavascript <T>(AbstractValidator <T> validator)
        {
            var jsBuilder = new StringBuilder();

            jsBuilder.AppendLine(validator.GetType().Name + ":function (obj) { \r\n var errors = {}; \r\n Object.keys(obj).forEach(function(element) {\r\n errors[element] = []; \r\n });");
            var props = typeof(T).GetProperties();

            foreach (var property in props)
            {
                var propertyName = property.Name;
                foreach (var element in validator.CreateDescriptor().GetValidatorsForMember(propertyName))
                {
                    var parameters = new Dictionary <string, object>();
                    var errorKey   = element.GetType().Name;
                    switch (element)
                    {
                    case IComparisonValidator cv:
                        parameters.Add(nameof(cv.ValueToCompare), cv.ValueToCompare);
                        break;

                    case IBetweenValidator bv:
                        parameters.Add(nameof(bv.From), bv.From);
                        parameters.Add(nameof(bv.To), bv.To);
                        break;

                    case ILengthValidator lv:
                        parameters.Add(nameof(lv.Min), lv.Min);
                        parameters.Add(nameof(lv.Max), lv.Max);
                        break;
                    }

                    foreach (var converterType in TypeLookup[element.GetType()])
                    {
                        if (Activator.CreateInstance(converterType, args: element) is IJsConverterValidator converter)
                        {
                            jsBuilder.AppendLine(converter.GetJs(propertyName, errorKey, parameters));
                        }
                    }
                }
            }
            jsBuilder.AppendLine("return errors;\r\n},");
            return(jsBuilder.ToString());
        }
Exemple #3
0
        public IEnumerable <RuleDescriptor> Document <T>(AbstractValidator <T> validator)
        {
            if (validator == null)
            {
                throw new ArgumentNullException(nameof(validator));
            }

            var descriptor = validator.CreateDescriptor();

            var memberValidators = descriptor.GetMembersWithValidators();

            foreach (var member in memberValidators)
            {
                var rules = descriptor.GetRulesForMember(member.Key);

                foreach (var validationRule in rules)
                {
                    var rule         = (PropertyRule)validationRule;
                    var propertyName = rule.GetDisplayName();

                    yield return(_ruleBuilder.BuildRuleDescription(rule.Validators, propertyName, rule.CascadeMode, rule));
                }
            }
        }
Exemple #4
0
 public IValidatorDescriptor CreateDescriptor()
 {
     return(_validator.CreateDescriptor());
 }
Exemple #5
0
        /// <summary>
        /// Returns the ValidationRule objects from an <see cref="AbstractValidator{T}"/> that are relevant to the given <see cref="Validatable{T}"/>object.
        /// </summary>
        /// <typeparam name="T">The Type of the class that the <see cref="Validatable{T}"/>object property pertains to, and therefore is the target Type for the <see cref="AbstractValidator{T}"/>.</typeparam>
        /// <param name="validator">The Fluent <see cref="AbstractValidator{T}"/> instance.</param>
        /// <param name="validatableObj">The <see cref="Validatable{T}"/>object for which to retrieve ValidationRules for.</param>
        /// <returns>A List of IValidationRule relevant to the <see cref="Validatable{T}"/>object.</returns>
        public static List <IValidationRule> GetRulesFor <T>(this AbstractValidator <T> validator, IValidity validatableObj)
        {
            var descriptor = validator.CreateDescriptor();

            return(descriptor.GetRulesForMember(validatableObj.ClassPropertyName).ToList());
        }