IValidator <TValidated> GetValidatorPrivate <TValidated>(ValidationManifest manifest)
 {
     return(new Validator <TValidated>(manifest,
                                       resolver.GetRequiredService <IGetsRuleExecutor>(),
                                       resolver.GetRequiredService <IGetsAllExecutableRulesWithDependencies>(),
                                       resolver.GetRequiredService <IGetsResolvedValidationOptions>(),
                                       resolver.GetRequiredService <IGetsRuleExecutionContext>()));
 }
        public void GetValidatorShouldReturnACorrectlyTypedGenericValidator([Frozen, AutofixtureServices] IServiceProvider resolver,
                                                                            ValidatorFromManifestFactory sut,
                                                                            [ManifestModel] ValidationManifest manifest)
        {
            manifest.ValidatedType = typeof(string);

            Assert.That(() => sut.GetValidator(manifest), Is.InstanceOf <Validator <string> >());
        }
        /// <summary>
        /// Gets a validator from a validation manifest.
        /// </summary>
        /// <param name="manifest">The validation manifest.</param>
        /// <returns>A validator.</returns>
        public IValidator GetValidator(ValidationManifest manifest)
        {
            if (manifest is null)
            {
                throw new ArgumentNullException(nameof(manifest));
            }

            var method = GetType().GetTypeInfo().GetDeclaredMethod(nameof(GetValidatorPrivate)).MakeGenericMethod(manifest.ValidatedType);

            return((IValidator)method.Invoke(this, new [] { manifest }));
        }
Esempio n. 4
0
        public void GetManifestShouldReturnManifestCreatedFromBuilderAndBuilderCustomisation([Frozen] IGetsValidatorBuilder builderFactory,
                                                                                             ManifestFromBuilderProvider sut,
                                                                                             IBuildsValidator <string> builder,
                                                                                             IValidatorBuilder <string> validatorBuilder,
                                                                                             [ManifestModel] ValidationManifest manifest)
        {
            Mock.Get(builderFactory)
            .Setup(x => x.GetValidatorBuilder <string>(It.IsAny <ValidatorBuilderContext>()))
            .Returns(validatorBuilder);
            Mock.Get(validatorBuilder)
            .Setup(x => x.GetManifest())
            .Returns(manifest);

            var result = sut.GetManifest(builder);

            Assert.That(result, Is.SameAs(manifest));
            Mock.Get(builder).Verify(x => x.ConfigureValidator(validatorBuilder), Times.Once);
        }
        /// <summary>
        /// Gets a validator instance for a specified generic type, using a specified validation manifest.
        /// The manifest must describe a validator which is able to validate the object type <typeparamref name="TValidated"/>.
        /// </summary>
        /// <typeparam name="TValidated">The type of object to validate.</typeparam>
        /// <param name="factory">The validator factory.</param>
        /// <param name="manifest">The validation manifest.</param>
        /// <returns>A strongly-typed validator implementation.</returns>
        /// <exception cref="ArgumentNullException">If either <paramref name="factory"/> or <paramref name="manifest"/> are <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">If <paramref name="manifest"/> does not describe a validator for <typeparamref name="TValidated"/>.</exception>
        public static IValidator <TValidated> GetValidator <TValidated>(this IGetsValidator factory, Manifest.ValidationManifest manifest)
        {
            if (factory is null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (!IsCorrectValidatedType <TValidated>(manifest.ValidatedType))
            {
                throw new ArgumentException(String.Format(GetExceptionMessage("ValidatedTypeMustBeSelectedValidatedType"), typeof(TValidated).Name), nameof(manifest));
            }

            return((IValidator <TValidated>)factory.GetValidator(manifest));
        }