Exemplo n.º 1
0
        public void Validator_IfValidateAllPropertiesIsNotSet_DoesNotAutoExpand()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                LonelyPerson,
                typeof(Person));

            var validationContext = testValidationContext.ModelValidationContext;
            var validator         = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var modelExplorer = testValidationContext.ModelValidationContext.ModelExplorer;

            // No ChildNode added
            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);

            // Since Person is not IValidatable and we do not look at its properties, the state is empty.
            Assert.Empty(validationContext.ModelState.Keys);
        }
Exemplo n.º 2
0
        public void ExpectedValidationErrorsRaised(object model, Type type, Dictionary <string, string> expectedErrors)
        {
            // Arrange
            var context = GetModelValidationContext(model, type);

            var validator = new DefaultObjectValidator(context.ExcludeFiltersProvider, context.ModelMetadataProvider);

            // Act
            validator.Validate(context.ModelValidationContext);

            // Assert
            var actualErrors = new Dictionary <string, string>();

            foreach (var keyStatePair in context.ModelValidationContext.ModelState)
            {
                foreach (var error in keyStatePair.Value.Errors)
                {
                    actualErrors.Add(keyStatePair.Key, error.ErrorMessage);
                }
            }

            Assert.Equal(expectedErrors.Count, actualErrors.Count);
            foreach (var keyErrorPair in expectedErrors)
            {
                Assert.Contains(keyErrorPair.Key, actualErrors.Keys);
                Assert.Equal(keyErrorPair.Value, actualErrors[keyErrorPair.Key]);
            }
        }
Exemplo n.º 3
0
        public void ForExcludedNonModelBoundType_Properties_NotMarkedAsSkiped()
        {
            // Arrange
            var user = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                "user",
                new List <Type> {
                typeof(User)
            });
            var validationContext = testValidationContext.ModelValidationContext;
            var validator         = new DefaultObjectValidator(
                testValidationContext.ExcludeFiltersProvider,
                testValidationContext.ModelMetadataProvider);

            // Act
            validator.Validate(validationContext);

            // Assert
            Assert.False(validationContext.ModelState.ContainsKey("user.Password"));
            Assert.False(validationContext.ModelState.ContainsKey("user.ConfirmPassword"));
            var modelState = validationContext.ModelState["user"];

            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);
        }
Exemplo n.º 4
0
        public void ForExcludedNonModelBoundTypes_NoEntryInModelState()
        {
            // Arrange
            var user = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List <Type> {
                typeof(User)
            });
            var validationContext = testValidationContext.ModelValidationContext;
            var validator         = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Empty(validationContext.ModelState);
        }
Exemplo n.º 5
0
        public void DictionaryType_ValidationSuccessful()
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();

            modelStateDictionary.Add("items[0].Key", new ModelState());
            modelStateDictionary.Add("items[0].Value", new ModelState());
            modelStateDictionary.Add("items[1].Key", new ModelState());
            modelStateDictionary.Add("items[1].Value", new ModelState());

            var model = new Dictionary <string, string>()
            {
                { "FooKey", "FooValue" },
                { "BarKey", "BarValue" }
            };

            var testValidationContext = GetModelValidationContext(
                model,
                typeof(Dictionary <string, string>),
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List <IExcludeTypeValidationFilter>();

            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());
            testValidationContext.ExcludeFilters = excludeTypeFilters;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);

            var topLevelValidationNode =
                new ModelValidationNode(
                    "items",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(4, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["items[0].Key"];

            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[0].Value"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[1].Key"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[1].Value"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
        }
Exemplo n.º 6
0
        public void DictionaryType_ValidationSuccessful()
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();

            modelStateDictionary.Add("items[0].Key", new ModelState());
            modelStateDictionary.Add("items[0].Value", new ModelState());
            modelStateDictionary.Add("items[1].Key", new ModelState());
            modelStateDictionary.Add("items[1].Value", new ModelState());

            var model = new Dictionary <string, string>()
            {
                { "FooKey", "FooValue" },
                { "BarKey", "BarValue" }
            };

            var testValidationContext = GetModelValidationContext(
                model,
                typeof(Dictionary <string, string>),
                "items",
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List <IExcludeTypeValidationFilter>();

            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());

            var mockValidationExcludeFiltersProvider = new Mock <IValidationExcludeFiltersProvider>();

            mockValidationExcludeFiltersProvider
            .SetupGet(o => o.ExcludeFilters)
            .Returns(excludeTypeFilters);
            testValidationContext.ExcludeFiltersProvider = mockValidationExcludeFiltersProvider.Object;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFiltersProvider,
                testValidationContext.ModelMetadataProvider);

            // Act
            validator.Validate(validationContext);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            var modelState = validationContext.ModelState["items"];

            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);
            modelState = validationContext.ModelState["items[0].Key"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
            modelState = validationContext.ModelState["items[0].Value"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
            modelState = validationContext.ModelState["items[1].Key"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
            modelState = validationContext.ModelState["items[1].Value"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 7
0
        public void EnumerableType_ValidationSuccessful(object model, Type type)
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();

            modelStateDictionary.Add("items[0]", new ModelState());
            modelStateDictionary.Add("items[1]", new ModelState());
            modelStateDictionary.Add("items[2]", new ModelState());

            var testValidationContext = GetModelValidationContext(
                model,
                type,
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List <IExcludeTypeValidationFilter>();

            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());
            testValidationContext.ExcludeFilters = excludeTypeFilters;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "items",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(3, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["items[0]"];

            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);

            modelState = validationContext.ModelState["items[1]"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);

            modelState = validationContext.ModelState["items[2]"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);
        }
Exemplo n.º 8
0
        public void Validation_ShortCircuit_WhenMaxErrorCountIsSet()
        {
            // Arrange
            var user = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List <Type> {
                typeof(string)
            });

            var validationContext = testValidationContext.ModelValidationContext;

            validationContext.ModelState.MaxAllowedErrors = 2;
            validationContext.ModelState.AddModelError("key1", "error1");
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.Equal(new[] { "key1", "", "user.ConfirmPassword" },
                         validationContext.ModelState.Keys.ToArray());
            var modelState = validationContext.ModelState["user.ConfirmPassword"];

            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);

            var error = Assert.Single(validationContext.ModelState[""].Errors);

            Assert.IsType <TooManyModelErrorsException>(error.Exception);
        }
Exemplo n.º 9
0
        public void Validate_IfSuppressIsSet_MarkedAsSkipped()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                new Person2()
            {
                Address = new Address {
                    Street = "GreaterThan5Characters"
                }
            },
                typeof(Person2));
            var validationContext = testValidationContext.ModelValidationContext;

            // Create an entry like a model binder would.
            validationContext.ModelState.Add("person.Address", new ModelState());

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var modelExplorer          = testValidationContext.ModelValidationContext.ModelExplorer;
            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model);

            var propertyExplorer = modelExplorer.GetExplorerForProperty("Address");
            var childNode        = new ModelValidationNode(
                "person.Address",
                propertyExplorer.Metadata,
                propertyExplorer.Model)
            {
                SuppressValidation = true
            };

            topLevelValidationNode.ChildNodes.Add(childNode);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(1, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["person.Address"];

            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 10
0
        public void ForExcludedModelBoundTypes_Properties_MarkedAsSkipped()
        {
            // Arrange
            var user = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List <Type> {
                typeof(User)
            });
            var validationContext = testValidationContext.ModelValidationContext;

            // Set the value on model state as a model binder would.
            var valueProviderResult = new ValueProviderResult(rawValue: "password");

            validationContext.ModelState.SetModelValue("user.Password", valueProviderResult);
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            var entry = Assert.Single(validationContext.ModelState);

            Assert.Equal("user.Password", entry.Key);
            Assert.Empty(entry.Value.Errors);
            Assert.Equal(entry.Value.ValidationState, ModelValidationState.Skipped);
            Assert.Same(valueProviderResult, entry.Value.Value);
        }
Exemplo n.º 11
0
        public void EnumerableType_ValidationSuccessful(object model, Type type)
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();

            modelStateDictionary.Add("items[0]", new ModelState());
            modelStateDictionary.Add("items[1]", new ModelState());
            modelStateDictionary.Add("items[2]", new ModelState());

            var testValidationContext = GetModelValidationContext(
                model,
                type,
                "items",
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List <IExcludeTypeValidationFilter>();

            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());

            var mockValidationExcludeFiltersProvider = new Mock <IValidationExcludeFiltersProvider>();

            mockValidationExcludeFiltersProvider
            .SetupGet(o => o.ExcludeFilters)
            .Returns(excludeTypeFilters);
            testValidationContext.ExcludeFiltersProvider = mockValidationExcludeFiltersProvider.Object;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFiltersProvider,
                testValidationContext.ModelMetadataProvider);

            // Act
            validator.Validate(validationContext);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            var modelState = validationContext.ModelState["items"];

            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);
        }
Exemplo n.º 12
0
        public void Validator_IfValidateAllPropertiesSet_WithChildNodes_DoesNotAutoExpand()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                LonelyPerson,
                typeof(Person));

            var validationContext = testValidationContext.ModelValidationContext;
            var validator         = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var modelExplorer = testValidationContext.ModelValidationContext.ModelExplorer;

            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            var propertyExplorer = modelExplorer.GetExplorerForProperty("Profession");
            var childNode        = new ModelValidationNode(
                "person.Profession",
                propertyExplorer.Metadata,
                propertyExplorer.Model);

            topLevelValidationNode.ChildNodes.Add(childNode);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            var modelState = validationContext.ModelState;

            Assert.False(modelState.IsValid);

            // Since the model is invalid at property level there is no entry in the model state for top level node.
            Assert.Single(modelState.Keys, k => k == "person.Profession");
            Assert.Equal(1, modelState.Count);
        }
Exemplo n.º 13
0
        public void ForExcludedModelBoundTypes_Properties_MarkedAsSkipped()
        {
            // Arrange
            var user = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                "user",
                new List <Type> {
                typeof(User)
            });
            var validationContext = testValidationContext.ModelValidationContext;

            // Set the value on model state as a model binder would.
            validationContext.ModelState.SetModelValue(
                "user.Password",
                Mock.Of <ValueProviderResult>());
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFiltersProvider,
                testValidationContext.ModelMetadataProvider);

            // Act
            validator.Validate(validationContext);

            // Assert
            var modelState = validationContext.ModelState["user.Password"];

            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);

            modelState = validationContext.ModelState["user.ConfirmPassword"];
            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 14
0
        public void NonRequestBoundModel_MarkedAsSkipped()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                new TestServiceProvider(),
                typeof(TestServiceProvider),
                "serviceProvider");

            var validationContext = testValidationContext.ModelValidationContext;
            var validator         = new DefaultObjectValidator(
                testValidationContext.ExcludeFiltersProvider,
                testValidationContext.ModelMetadataProvider);

            // Act
            validator.Validate(validationContext);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            var modelState = validationContext.ModelState["serviceProvider.TestService"];

            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 15
0
        public void ForExcludedNonModelBoundTypes_NoEntryInModelState()
        {
            // Arrange
            var user = new User()
            {
                Password = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List<Type> { typeof(User) });
            var validationContext = testValidationContext.ModelValidationContext;
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Empty(validationContext.ModelState);
        }
Exemplo n.º 16
0
        public void ForExcludedModelBoundTypes_Properties_MarkedAsSkipped()
        {
            // Arrange
            var user = new User()
            {
                Password = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List<Type> { typeof(User) });
            var validationContext = testValidationContext.ModelValidationContext;

            // Set the value on model state as a model binder would.
            validationContext.ModelState.SetModelValue(
                "user.Password",
                Mock.Of<ValueProviderResult>());
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            var entry = Assert.Single(validationContext.ModelState);
            Assert.Equal("user.Password", entry.Key);
            Assert.Empty(entry.Value.Errors);
            Assert.Equal(entry.Value.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 17
0
        public void Validate_IfSuppressIsSet_MarkedAsSkipped()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                new Person2()
                {
                    Address = new Address { Street = "GreaterThan5Characters" }
                },
                typeof(Person2));
            var validationContext = testValidationContext.ModelValidationContext;

            // Create an entry like a model binder would.
            validationContext.ModelState.Add("person.Address", new ModelState());

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters, 
                testValidationContext.ModelMetadataProvider);
            var modelExplorer = testValidationContext.ModelValidationContext.ModelExplorer;
            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model);

            var propertyExplorer = modelExplorer.GetExplorerForProperty("Address");
            var childNode = new ModelValidationNode(
                "person.Address",
                propertyExplorer.Metadata,
                propertyExplorer.Model)
            {
                SuppressValidation = true
            };

            topLevelValidationNode.ChildNodes.Add(childNode);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(1, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["person.Address"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);
        }
Exemplo n.º 18
0
        public void Validator_IfValidateAllPropertiesIsNotSet_DoesNotAutoExpand()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                LonelyPerson,
                typeof(Person));

            var validationContext = testValidationContext.ModelValidationContext;
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var modelExplorer = testValidationContext.ModelValidationContext.ModelExplorer;

            // No ChildNode added
            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);

            // Since Person is not IValidatable and we do not look at its properties, the state is empty.
            Assert.Empty(validationContext.ModelState.Keys);
        }
Exemplo n.º 19
0
        public void Validation_ShortCircuit_WhenMaxErrorCountIsSet()
        {
            // Arrange
            var user = new User()
            {
                Password = "******",
                ConfirmPassword = "******"
            };

            var testValidationContext = GetModelValidationContext(
                user,
                typeof(User),
                new List<Type> { typeof(string) });

            var validationContext = testValidationContext.ModelValidationContext;
            validationContext.ModelState.MaxAllowedErrors = 2;
            validationContext.ModelState.AddModelError("key1", "error1");
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "user",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.Equal(new[] { "key1", "", "user.ConfirmPassword" },
                validationContext.ModelState.Keys.ToArray());
            var modelState = validationContext.ModelState["user.ConfirmPassword"];
            Assert.Empty(modelState.Errors);
            Assert.Equal(modelState.ValidationState, ModelValidationState.Skipped);

            var error = Assert.Single(validationContext.ModelState[""].Errors);
            Assert.IsType<TooManyModelErrorsException>(error.Exception);
        }
Exemplo n.º 20
0
        public void Validator_IfValidateAllPropertiesSet_WithChildNodes_DoesNotAutoExpand()
        {
            // Arrange
            var testValidationContext = GetModelValidationContext(
                LonelyPerson,
                typeof(Person));

            var validationContext = testValidationContext.ModelValidationContext;
            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var modelExplorer = testValidationContext.ModelValidationContext.ModelExplorer;

            var topLevelValidationNode = new ModelValidationNode(
                "person",
                modelExplorer.Metadata,
                modelExplorer.Model)
            {
                ValidateAllProperties = true
            };

            var propertyExplorer = modelExplorer.GetExplorerForProperty("Profession");
            var childNode = new ModelValidationNode(
                "person.Profession",
                propertyExplorer.Metadata,
                propertyExplorer.Model);

            topLevelValidationNode.ChildNodes.Add(childNode);

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            var modelState = validationContext.ModelState;
            Assert.False(modelState.IsValid);

            // Since the model is invalid at property level there is no entry in the model state for top level node.
            Assert.Single(modelState.Keys, k => k == "person.Profession");
            Assert.Equal(1, modelState.Count);
        }
Exemplo n.º 21
0
        public void EnumerableType_ValidationSuccessful(object model, Type type)
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();
            modelStateDictionary.Add("items[0]", new ModelState());
            modelStateDictionary.Add("items[1]", new ModelState());
            modelStateDictionary.Add("items[2]", new ModelState());

            var testValidationContext = GetModelValidationContext(
                model,
                type,
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List<IExcludeTypeValidationFilter>();
            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());
            testValidationContext.ExcludeFilters = excludeTypeFilters;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(
                    "items",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(3, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["items[0]"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);

            modelState = validationContext.ModelState["items[1]"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);

            modelState = validationContext.ModelState["items[2]"];
            Assert.Equal(modelState.ValidationState, ModelValidationState.Valid);
        }
Exemplo n.º 22
0
        public void ExpectedValidationErrorsRaised(object model, Type type, Dictionary<string, string> expectedErrors)
        {
            // Arrange
            var context = GetModelValidationContext(model, type);

            var validator = new DefaultObjectValidator(context.ExcludeFilters, context.ModelMetadataProvider);
            var topLevelValidationNode =
                new ModelValidationNode(string.Empty, context.ModelValidationContext.ModelExplorer.Metadata, model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(context.ModelValidationContext, topLevelValidationNode);

            // Assert
            var actualErrors = new Dictionary<string, string>();
            foreach (var keyStatePair in context.ModelValidationContext.ModelState)
            {
                foreach (var error in keyStatePair.Value.Errors)
                {
                    actualErrors.Add(keyStatePair.Key, error.ErrorMessage);
                }
            }

            Assert.Equal(expectedErrors.Count, actualErrors.Count);
            foreach (var keyErrorPair in expectedErrors)
            {
                Assert.Contains(keyErrorPair.Key, actualErrors.Keys);
                Assert.Equal(keyErrorPair.Value, actualErrors[keyErrorPair.Key]);
            }
        }
Exemplo n.º 23
0
        public void DictionaryType_ValidationSuccessful()
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();
            modelStateDictionary.Add("items[0].Key", new ModelState());
            modelStateDictionary.Add("items[0].Value", new ModelState());
            modelStateDictionary.Add("items[1].Key", new ModelState());
            modelStateDictionary.Add("items[1].Value", new ModelState());

            var model = new Dictionary<string, string>()
            {
                { "FooKey", "FooValue" },
                { "BarKey", "BarValue" }
            };

            var testValidationContext = GetModelValidationContext(
                model,
                typeof(Dictionary<string, string>),
                excludedTypes: null,
                modelStateDictionary: modelStateDictionary);

            var excludeTypeFilters = new List<IExcludeTypeValidationFilter>();
            excludeTypeFilters.Add(new SimpleTypesExcludeFilter());
            testValidationContext.ExcludeFilters = excludeTypeFilters;

            var validationContext = testValidationContext.ModelValidationContext;

            var validator = new DefaultObjectValidator(
                testValidationContext.ExcludeFilters,
                testValidationContext.ModelMetadataProvider);

            var topLevelValidationNode =
                new ModelValidationNode(
                    "items",
                    testValidationContext.ModelValidationContext.ModelExplorer.Metadata,
                    testValidationContext.ModelValidationContext.ModelExplorer.Model)
                {
                    ValidateAllProperties = true
                };

            // Act
            validator.Validate(validationContext, topLevelValidationNode);

            // Assert
            Assert.True(validationContext.ModelState.IsValid);
            Assert.Equal(4, validationContext.ModelState.Count);
            var modelState = validationContext.ModelState["items[0].Key"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[0].Value"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[1].Key"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
            modelState = validationContext.ModelState["items[1].Value"];
            Assert.Equal(ModelValidationState.Skipped, modelState.ValidationState);
        }