예제 #1
0
        /// <summary>
        /// Adds a func/method to get called when this property is validated
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="propertyValidations"></param>
        /// <param name="validation"></param>
        /// <param name="setter"></param>
        /// <returns></returns>
        public static IPropertyValidations <TSource, TProperty> Add <TSource, TProperty>(
            this IPropertyValidations <TSource, TProperty> propertyValidations,
            Func <TProperty, TSource, ValidationContext, ValidationResult> validation,
            Action <ValidationAttribute> setter = null)
        {
            Func <object, ValidationContext, ValidationResult> map =
                (prop, context) => validation((TProperty)prop, (TSource)context.ObjectInstance, context);
            var attribute = new ValidatableObjectAttribute(map);

            setter?.Invoke(attribute);

            return(propertyValidations.Add(attribute));
        }
        /// <summary>
        /// Adds the validation Func/method to entire class
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="objectValidations"></param>
        /// <param name="validation"></param>
        /// <param name="setter"></param>
        /// <returns></returns>
        public static IObjectValidations <TSource> Add <TSource>(this IObjectValidations <TSource> objectValidations,
                                                                 Func <TSource, ValidationContext, ValidationResult> validation,
                                                                 Action <ValidationAttribute> setter = null)
        {
            if (validation == null)
            {
                throw new ArgumentNullException(nameof(validation));
            }

            Func <object, ValidationContext, ValidationResult> map =
                (source, context) => validation((TSource)source, context);

            var validationAttribute = new ValidatableObjectAttribute(map);

            return(objectValidations.Add(validationAttribute, setter));
        }
예제 #3
0
        /// <summary>
        /// Adds a func/method to get called when this property is validated
        /// If the other properties pass in pass validation
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="propertyValidations"></param>
        /// <param name="validation"></param>
        /// <param name="setter"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static IPropertyValidations <TSource, TProperty> AddIfValid <TSource, TProperty>(
            this IPropertyValidations <TSource, TProperty> propertyValidations,
            Func <TProperty, TSource, ValidationContext, ValidationResult> validation,
            Action <ValidationAttribute> setter,
            params Expression <Func <TSource, object> >[] properties)
        {
            Func <object, ValidationContext, ValidationResult> map =
                (prop, context) => validation((TProperty)prop, (TSource)context.ObjectInstance, context);
            var attribute = new ValidatableObjectAttribute(map);

            setter?.Invoke(attribute);

            var propsToValidate = properties.ToDictionary(p => p.NameOf(), p =>
            {
                var func = p.Compile();
                Func <object, object> convert = o => func((TSource)o);
                return(convert);
            });

            var validationAttribute = new ValidateIfAttribute(propsToValidate, attribute);

            return(propertyValidations.Add(validationAttribute));
        }