private TestModelValidationContext GetModelValidationContext( object model, Type type, List <Type> excludedTypes, ModelStateDictionary modelStateDictionary) { var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var excludedValidationTypesPredicate = new List <IExcludeTypeValidationFilter>(); if (excludedTypes != null) { var mockExcludeTypeFilter = new Mock <IExcludeTypeValidationFilter>(); mockExcludeTypeFilter .Setup(o => o.IsTypeExcluded(It.IsAny <Type>())) .Returns <Type>(excludedType => excludedTypes.Any(t => t.IsAssignableFrom(excludedType))); excludedValidationTypesPredicate.Add(mockExcludeTypeFilter.Object); } var modelExplorer = modelMetadataProvider.GetModelExplorerForType(type, model); return(new TestModelValidationContext { ModelValidationContext = new ModelValidationContext( null, TestModelValidatorProvider.CreateDefaultProvider(), modelStateDictionary, modelExplorer), ModelMetadataProvider = modelMetadataProvider, ExcludeFilters = excludedValidationTypesPredicate }); }
public void ConstructorSetsInstanceAndType() { // Arrange var validatorProvider = new TestModelValidatorProvider(); // Act var descriptor = new ModelValidatorProviderDescriptor(validatorProvider); // Assert Assert.Same(validatorProvider, descriptor.Instance); Assert.Equal(validatorProvider.GetType(), descriptor.OptionType); }
public void GetValidators_ForIValidatableObject() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForType(typeof(ValidatableObject)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; var validator = Assert.Single(validators); Assert.IsType <ValidatableObjectAdapter>(validator); }
public void GetValidators_ModelValidatorAttributeOnClass() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForType(typeof(ModelValidatorAttributeOnClass)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; var validator = Assert.IsType <CustomModelValidatorAttribute>(Assert.Single(validators)); Assert.Equal("Class", validator.Tag); }
public void GetValidators_FromModelMetadataType_SingleValidator() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(ProductViewModel), nameof(ProductViewModel.Id)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; Assert.IsType <RangeAttributeAdapter>(Assert.Single(validators)); }
public void GetValidators_DataAnnotationsAttribute_DefaultAdapter() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(CustomValidationAttributeOnProperty), nameof(CustomValidationAttributeOnProperty.Property)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; Assert.IsType <DataAnnotationsModelValidator>(Assert.Single(validators)); }
public void GetValidators_ClientModelValidatorAttributeOnPropertyAndClass() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(ModelValidatorAttributeOnPropertyAndClass), nameof(ModelValidatorAttributeOnPropertyAndClass.Property)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; Assert.Equal(2, validators.Count); Assert.Single(validators, v => Assert.IsType <CustomModelValidatorAttribute>(v).Tag == "Class"); Assert.Single(validators, v => Assert.IsType <CustomModelValidatorAttribute>(v).Tag == "Property"); }
public void GetValidators_FromModelMetadataType_MergedValidators() { // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var validatorProvider = TestModelValidatorProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(ProductViewModel), nameof(ProductViewModel.Name)); var context = new ModelValidatorProviderContext(metadata); // Act validatorProvider.GetValidators(context); // Assert var validators = context.Validators; Assert.Equal(2, validators.Count); Assert.Single(validators, v => ((DataAnnotationsModelValidator)v).Attribute is RegularExpressionAttribute); Assert.Single(validators, v => ((DataAnnotationsModelValidator)v).Attribute is StringLengthAttribute); }
private static IModelValidatorProvider CreateValidatorProvider() { return(TestModelValidatorProvider.CreateDefaultProvider()); }