Exemple #1
0
        public static PropertyRule HasPropertyRule <T, TProperty>(
            this AbstractValidator <T> validator,
            Expression <Func <T, TProperty> > expression)
        {
            var propertyRule = validator
                               .OfType <PropertyRule>()
                               .SingleOrDefault(rule => rule.Member == expression.GetMember());

            var propertyName = expression.GetMember().Name;

            Assert.IsNotNull(propertyRule, $"Expects a property rule for {propertyName}, but it is not defined");

            return(propertyRule);
        }
Exemple #2
0
        public static PropertyValidatorContext PropertyContext <TModel, TProperty>(
            this AbstractValidator <TModel> validator,
            TModel model,
            Expression <Func <TModel, TProperty> > expression)
        {
            var propertyRule = validator
                               .OfType <PropertyRule>()
                               .SingleOrDefault(rule => rule.Member == expression.GetMember());

            var projector = expression.Compile();
            var value     = projector(model);

            var validationContext = new ValidationContext <TModel>(model, new PropertyChain(),
                                                                   ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory());

            return(new PropertyValidatorContext(validationContext, propertyRule,
                                                expression.GetMember().Name, value));
        }