/// <inheritdoc />
 public void GetValidators(ClientValidatorProviderContext context)
 {
     foreach (var validatorProvider in ValidatorProviders)
     {
         validatorProvider.GetValidators(context);
     }
 }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            IStringLocalizer stringLocalizer = null;
            if (_options.Value.DataAnnotationLocalizerProvider != null && _stringLocalizerFactory != null)
            {
                // This will pass first non-null type (either containerType or modelType) to delegate.
                // Pass the root model type(container type) if it is non null, else pass the model type.
                stringLocalizer = _options.Value.DataAnnotationLocalizerProvider(
                    context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType,
                    _stringLocalizerFactory);
            }

            var hasRequiredAttribute = false;

            foreach (var attribute in context.ValidatorMetadata.OfType<ValidationAttribute>())
            {
                hasRequiredAttribute |= attribute is RequiredAttribute;

                var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer);
                if (adapter != null)
                {
                    context.Validators.Add(adapter);
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Validators.Add(new RequiredAttributeAdapter(new RequiredAttribute(), stringLocalizer));
            }
        }
 /// <inheritdoc />
 public void GetValidators(ClientValidatorProviderContext context)
 {
     // Perf: Avoid allocations
     for (var i = 0; i < ValidatorProviders.Count; i++)
     {
         ValidatorProviders[i].GetValidators(context);
     }
 }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            var type = context.ModelMetadata.ModelType;
            var typeToValidate = Nullable.GetUnderlyingType(type) ?? type;

            // Check only the numeric types for which we set type='text'.
            if (typeToValidate == typeof(float) ||
                typeToValidate == typeof(double) ||
                typeToValidate == typeof(decimal))
            {
                context.Validators.Add(new NumericClientModelValidator());
            }
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Perf: Avoid allocations
            for (var i = 0; i < ValidatorProviders.Count; i++)
            {
                ValidatorProviders[i].GetValidators(context);
            }
        }
        public void GetValidators_DoesNotGetsNumericValidator_ForUnsupportedTypes(Type modelType)
        {
            // Arrange
            var provider = new NumericClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(modelType);

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Validators);
        }
        public void GetValidators_GetsNumericValidator_ForNumericType(Type modelType)
        {
            // Arrange
            var provider = new NumericClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(modelType);

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            var validator = Assert.Single(providerContext.Validators);
            Assert.IsType<NumericClientModelValidator>(validator);
        }
        public void GetValidators_DoesNotAddRequiredAttribute_ForIsRequiredFalse()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider();

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ReferenceTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Validators);
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var typeToValidate = context.ModelMetadata.UnderlyingOrModelType;

            // Check only the numeric types for which we set type='text'.
            if (typeToValidate == typeof(float) ||
                typeToValidate == typeof(double) ||
                typeToValidate == typeof(decimal))
            {
                context.Validators.Add(new NumericClientModelValidator());
            }
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Perf: Avoid allocations
            for (var i = 0; i < context.ValidatorMetadata.Count; i++)
            {
                var validator = context.ValidatorMetadata[i] as IClientModelValidator;
                if (validator != null)
                {
                    context.Validators.Add(validator);
                }
            }
        }
        public void GetValidators_AddsRequiredAttribute_ForIsRequiredTrue()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider();

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ValueTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            var validator = Assert.Single(providerContext.Validators);
            Assert.IsType<RequiredAttributeAdapter>(validator);
        }
        public void GetValidators_DoesNotAddExtraRequiredAttribute_IfAttributeIsSpecifiedExplicitly()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider();

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.WithAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            var validator = Assert.Single(providerContext.Validators);
            var adapter = Assert.IsType<RequiredAttributeAdapter>(validator);
            Assert.Equal("Custom Required Message", adapter.Attribute.ErrorMessage);
        }
        public void GetValidators_DoesNotAddRequiredAttribute_ForIsRequiredFalse()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ReferenceTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Validators);
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            var hasRequiredAttribute = false;

            foreach (var attribute in context.ValidatorMetadata.OfType <ValidationAttribute>())
            {
                hasRequiredAttribute |= attribute is RequiredAttribute;

                DataAnnotationsClientModelValidationFactory factory;
                if (_attributeFactories.TryGetValue(attribute.GetType(), out factory))
                {
                    context.Validators.Add(factory(attribute));
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Validators.Add(new RequiredAttributeAdapter(new RequiredAttribute()));
            }
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            var hasRequiredAttribute = false;

            foreach (var attribute in context.ValidatorMetadata.OfType<ValidationAttribute>())
            {
                hasRequiredAttribute |= attribute is RequiredAttribute; 

                DataAnnotationsClientModelValidationFactory factory;
                if (_attributeFactories.TryGetValue(attribute.GetType(), out factory))
                {
                    context.Validators.Add(factory(attribute));
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Validators.Add(new RequiredAttributeAdapter(new RequiredAttribute()));
            }
        }
Ejemplo n.º 16
0
        public void GetValidators_FromModelMetadataType_SingleValidator()
        {
            // Arrange
            var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(ProductViewModel),
                nameof(ProductViewModel.Id));
            var context = new ClientValidatorProviderContext(metadata);

            // Act
            validatorProvider.GetValidators(context);

            // Assert
            var validators = context.Validators;

            Assert.Equal(2, validators.Count);
            Assert.Single(validators, v => v is RangeAttributeAdapter);
            Assert.Single(validators, v => v is RequiredAttributeAdapter);
        }
Ejemplo n.º 17
0
        public void GetValidators_AddsRequiredAttribute_ForIsRequiredTrue()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager <MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.ValueTypeWithoutAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            var validator = Assert.Single(providerContext.Validators);

            Assert.IsType <RequiredAttributeAdapter>(validator);
        }
Ejemplo n.º 18
0
        public void GetValidators_DoesNotAddExtraRequiredAttribute_IfAttributeIsSpecifiedExplicitly()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new TestOptionsManager <MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);

            var metadata = _metadataProvider.GetMetadataForProperty(
                typeof(DummyRequiredAttributeHelperClass),
                nameof(DummyRequiredAttributeHelperClass.WithAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            var validator = Assert.Single(providerContext.Validators);
            var adapter   = Assert.IsType <RequiredAttributeAdapter>(validator);

            Assert.Equal("Custom Required Message", adapter.Attribute.ErrorMessage);
        }
        /// <inheritdoc />
        public void GetValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            IStringLocalizer stringLocalizer = null;

            if (_options.Value.DataAnnotationLocalizerProvider != null && _stringLocalizerFactory != null)
            {
                // This will pass first non-null type (either containerType or modelType) to delegate.
                // Pass the root model type(container type) if it is non null, else pass the model type.
                stringLocalizer = _options.Value.DataAnnotationLocalizerProvider(
                    context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType,
                    _stringLocalizerFactory);
            }

            var hasRequiredAttribute = false;

            foreach (var attribute in context.ValidatorMetadata.OfType <ValidationAttribute>())
            {
                hasRequiredAttribute |= attribute is RequiredAttribute;

                DataAnnotationsClientModelValidationFactory factory;
                if (_attributeFactories.TryGetValue(attribute.GetType(), out factory))
                {
                    context.Validators.Add(factory(attribute, stringLocalizer));
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Validators.Add(new RequiredAttributeAdapter(new RequiredAttribute(), stringLocalizer));
            }
        }
        public void UnknownValidationAttribute_IsNotAddedAsValidator()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider(
                new ValidationAttributeAdapterProvider(),
                new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(),
                stringLocalizerFactory: null);
            var metadata = _metadataProvider.GetMetadataForType(typeof(DummyClassWithDummyValidationAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Validators);
        }
        public void GetValidators_ClientValidatorAttribute_DefaultAdapter()
        {
            // Arrange
            var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(CustomValidationAttributeOnProperty),
                nameof(CustomValidationAttributeOnProperty.Property));
            var context = new ClientValidatorProviderContext(metadata);

            // Act
            validatorProvider.GetValidators(context);

            // Assert
            var validators = context.Validators;

             Assert.IsType<CustomValidationAttribute>(Assert.Single(validators));
        }
        public void UnknownValidationAttribute_IsNotAddedAsValidator()
        {
            // Arrange
            var provider = new DataAnnotationsClientModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(typeof(DummyClassWithDummyValidationAttribute));

            var providerContext = new ClientValidatorProviderContext(metadata);

            // Act
            provider.GetValidators(providerContext);

            // Assert
            Assert.Empty(providerContext.Validators);
        }
        public void GetValidators_FromModelMetadataType_SingleValidator()
        {
            // Arrange
            var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(ProductViewModel),
                nameof(ProductViewModel.Id));
            var context = new ClientValidatorProviderContext(metadata);

            // Act
            validatorProvider.GetValidators(context);

            // Assert
            var validators = context.Validators;

            Assert.Equal(2, validators.Count);
            Assert.Single(validators, v => v is RangeAttributeAdapter);
            Assert.Single(validators, v => v is RequiredAttributeAdapter);
        }
        public void GetValidators_FromModelMetadataType_MergedValidators()
        {
            // Arrange
            var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

            var metadata = metadataProvider.GetMetadataForProperty(
                typeof(ProductViewModel),
                nameof(ProductViewModel.Name));
            var context = new ClientValidatorProviderContext(metadata);

            // Act
            validatorProvider.GetValidators(context);

            // Assert
            var validators = context.Validators;

            Assert.Equal(2, validators.Count);
            Assert.Single(validators, v => v is RegularExpressionAttributeAdapter);
            Assert.Single(validators, v => v is StringLengthAttributeAdapter);
        }