Esempio n. 1
0
        private IValidator GetChildValidator(ChildValidatorAdaptor adaptor)
        {
            var validatorContext         = new ValidationContext(null);
            var propertyValidatorContext = new PropertyValidatorContext(validatorContext, null, null);

            return(adaptor.GetValidator(propertyValidatorContext));
        }
 private void AddRulesFromChildValidatorAdaptor <T, TProperty>(
     OpenApiSchema schema,
     SchemaFilterContext context,
     ChildValidatorAdaptor <T, TProperty> adapter
     )
 {
     var propertyValidatorContext = new PropertyValidatorContext(new ValidationContext <T>(default), null, string.Empty);
Esempio n. 3
0
        /// <summary>
        /// Sets the validator associated with the rule. Use with complex properties where an IValidator instance is already declared for the property type.
        /// </summary>
        /// <param name="validator">The validator to set</param>
        public IRuleBuilderOptions <T, TProperty> SetValidator(IValidator <TProperty> validator)
        {
            validator.Guard("Cannot pass a null validator to SetValidator");
            var adaptor = new ChildValidatorAdaptor(validator);

            SetValidator(adaptor);
            return(this);
        }
        public void Given_CorrectValidatorWithSameValidatorType_When_Verifying_Then_ValidationPass()
        {
            // Arrange
            var childValidatorAdaptor = new ChildValidatorAdaptor(new FakeValidator(), typeof(FakeValidator));
            var verifier = new ChildValidatorVerifier <FakeValidator>();

            // Act & Assert
            AssertExtension.NotThrows(() => verifier.Verify(childValidatorAdaptor));
        }
        public void Given_CorrectValidatorWithDifferentChildValidatorType_When_Verifying_Then_ValidationFail()
        {
            // Arrange
            var childValidatorAdaptor = new ChildValidatorAdaptor(new OtherFakeValidator(), typeof(OtherFakeValidator));
            var verifier = new ChildValidatorVerifier <FakeValidator>();

            // Act & Assert
            AssertExtension.Throws <XunitException>(() => verifier.Verify(childValidatorAdaptor), "(ValidatorType property)");
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a new IncludeRule
        /// </summary>
        public IncludeRule(Func <ValidationContext <T>, T, IValidator <T> > func, Func <CascadeMode> cascadeModeThunk, Type typeToValidate, Type validatorType)
            : base(null, x => x, null, cascadeModeThunk, typeToValidate)
        {
            var adaptor = new ChildValidatorAdaptor <T, T>(func, validatorType);

            // Note: ChildValidatorAdaptor implements both IPropertyValidator and IAsyncPropertyValidator
            // So calling AddAsyncValidator will actually register it as supporting both sync and async.
            AddAsyncValidator(adaptor, adaptor);
        }
Esempio n. 7
0
        /// <summary>
        /// Sets the validator associated with the rule. Use with complex properties where an IValidator instance is already declared for the property type.
        /// </summary>
        /// <param name="validator">The validator to set</param>
        /// <param name="ruleSets"></param>
        public IRuleBuilderOptions <T, TProperty> SetValidator(IValidator <TProperty> validator, params string[] ruleSets)
        {
            validator.Guard("Cannot pass a null validator to SetValidator", nameof(validator));
            var adaptor = new ChildValidatorAdaptor <T, TProperty>(validator, validator.GetType())
            {
                RuleSets = ruleSets
            };

            SetValidator(adaptor);
            return(this);
        }
Esempio n. 8
0
        /// <summary>
        /// Uses the Service Provider to inject the default validator for the property type.
        /// </summary>
        /// <param name="ruleBuilder"></param>
        /// <param name="callback"></param>
        /// <param name="ruleSets"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static IRuleBuilderOptions <T, TProperty> InjectValidator <T, TProperty>(this IRuleBuilder <T, TProperty> ruleBuilder, Func <IServiceProvider, ValidationContext <T>, IValidator <TProperty> > callback, params string[] ruleSets)
        {
            var adaptor = new ChildValidatorAdaptor <T, TProperty>((context, _) => {
                var serviceProvider = context.GetServiceProvider();
                var validator       = callback(serviceProvider, context);
                return(validator);
            }, typeof(IValidator <TProperty>));

            adaptor.RuleSets = ruleSets;
            return(ruleBuilder.SetAsyncValidator(adaptor));
        }
Esempio n. 9
0
        public IRuleBuilderOptions <T, TProperty> SetValidator <TValidator>(Func <T, TValidator> validatorProvider, params string[] ruleSets) where TValidator : IValidator <TProperty>
        {
            validatorProvider.Guard("Cannot pass a null validatorProvider to SetValidator", nameof(validatorProvider));
            var adaptor = new ChildValidatorAdaptor <T, TProperty>((context, _) => validatorProvider(context.InstanceToValidate), typeof(TValidator))
            {
                RuleSets = ruleSets
            };

            // ChildValidatorAdaptor supports both sync and async execution.
            Rule.AddAsyncValidator(adaptor, adaptor);
            return(this);
        }
Esempio n. 10
0
        public IRuleBuilderOptions <T, TProperty> SetValidator(IValidator <TProperty> validator, params string[] ruleSets)
        {
            validator.Guard("Cannot pass a null validator to SetValidator", nameof(validator));
            var adaptor = new ChildValidatorAdaptor <T, TProperty>(validator, validator.GetType())
            {
                RuleSets = ruleSets
            };

            // ChildValidatorAdaptor supports both sync and async execution.
            Rule.AddAsyncValidator(adaptor, adaptor);
            return(this);
        }
        /// <summary>
        /// Uses the Service Provider to inject the default validator for the property type.
        /// </summary>
        /// <param name="ruleBuilder"></param>
        /// <param name="callback"></param>
        /// <param name="ruleSets"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static IRuleBuilderOptions <T, TProperty> InjectValidator <T, TProperty>(this IRuleBuilder <T, TProperty> ruleBuilder, Func <IServiceProvider, ValidationContext <T>, IValidator <TProperty> > callback, params string[] ruleSets)
        {
            var adaptor = new ChildValidatorAdaptor(context => {
                var actualContext   = (PropertyValidatorContext)context;
                var serviceProvider = actualContext.ParentContext.GetServiceProvider();
                var contextToUse    = ValidationContext <T> .GetFromNoNGenericContext(actualContext.ParentContext);
                var validator       = callback(serviceProvider, contextToUse);
                return(validator);
            }, typeof(IValidator <TProperty>));

            adaptor.RuleSets = ruleSets;

            return(ruleBuilder.SetValidator(adaptor));
        }
        public IRuleBuilderOptions <T, TChild> InjectValidator <TValidator>(params string[] ruleSets)
            where TValidator : IValidator <TChild>
        {
            var adaptor = new ChildValidatorAdaptor(context =>
            {
                var actualContext = (PropertyValidatorContext)context;
                return(actualContext.ResolveValidator <TChild, TValidator>());
            }, typeof(IValidator <TChild>))
            {
                RuleSets = ruleSets
            };

            return(ruleBuilder.SetValidator(adaptor));
        }
Esempio n. 13
0
        public IEnumerable <RuleDescription> GetNestedRules(string propertyName, PropertyRule rule, ChildValidatorAdaptor childValidator, IRuleBuilder ruleBuilder)
        {
            //HACK: I hate this explicit defintion.
            var          coreDocumentationType = typeof(DocBuilder);
            const string methodIdentifier      = "Document";

            var getRulesMethodDefinition = coreDocumentationType.ExtractMethodInfo(new[] { methodIdentifier })[methodIdentifier];

            // Create the generic method instance of Document()
            getRulesMethodDefinition = getRulesMethodDefinition.MakeGenericMethod(childValidator.ValidatorType.GetTypeInfo().BaseType.GenericTypeArguments[0]);

            //Parameter 1 = Validator instance derived from AbstractValidator<T>, Parameter 2 = boolean (documentNested)
            var parameterArray = new object[]
            {
                childValidator.GetValidator(_fluentValidationHelper.BuildPropertyValidatorContext(rule, propertyName)),
                true
            };

            //Invoke extension method with validator instance
            var documentationInstance = Activator.CreateInstance(coreDocumentationType, ruleBuilder);
            var nestedRules           = getRulesMethodDefinition.Invoke(documentationInstance, parameterArray) as IEnumerable <RuleDescription>;

            if (nestedRules == null)
            {
                yield return(null);
            }

            foreach (var deepDocumentRule in nestedRules)
            {
                yield return(deepDocumentRule);
            }
        }