public void Validate_ComplexReferenceType_Invalid_MultipleErrorsOnProperty_EmptyPrefix()
        {
            // Arrange
            var validatorProvider = CreateValidatorProvider();
            var modelState        = new ModelStateDictionary();
            var validationState   = new ValidationStateDictionary();

            var validator = CreateValidator();

            var model = (object)new Address()
            {
                Street = "Microsoft Way"
            };

            modelState.SetModelValue("Street", "Microsoft Way", "Microsoft Way");
            validationState.Add(model, new ValidationStateEntry()
            {
                Key = string.Empty
            });

            // Act
            validator.Validate(validatorProvider, modelState, validationState, string.Empty, model);

            // Assert
            Assert.False(modelState.IsValid);
            AssertKeysEqual(modelState, "Street");

            var entry = modelState["Street"];

            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);

            Assert.Equal(2, entry.Errors.Count);
            var errorMessages = entry.Errors.Select(e => e.ErrorMessage);

            Assert.Contains(ValidationAttributeUtil.GetStringLengthErrorMessage(null, 5, "Street"), errorMessages);
            Assert.Contains(ValidationAttributeUtil.GetRegExErrorMessage("hehehe", "Street"), errorMessages);
        }
        public void Validate_ComplexReferenceType_Valid()
        {
            // Arrange
            var validatorProvider = CreateValidatorProvider();
            var modelState        = new ModelStateDictionary();
            var validationState   = new ValidationStateDictionary();

            var validator = CreateValidator();

            var model = (object)new ReferenceType()
            {
                Reference = "ref", Value = 256
            };

            modelState.SetModelValue("parameter.Reference", "ref", "ref");
            modelState.SetModelValue("parameter.Value", "256", "256");
            validationState.Add(model, new ValidationStateEntry()
            {
                Key = "parameter"
            });

            // Act
            validator.Validate(validatorProvider, modelState, validationState, "parameter", model);

            // Assert
            Assert.True(modelState.IsValid);
            AssertKeysEqual(modelState, "parameter.Reference", "parameter.Value");

            var entry = modelState["parameter.Reference"];

            Assert.Equal(ModelValidationState.Valid, entry.ValidationState);
            Assert.Empty(entry.Errors);

            entry = modelState["parameter.Value"];
            Assert.Equal(ModelValidationState.Valid, entry.ValidationState);
            Assert.Empty(entry.Errors);
        }
Beispiel #3
0
        public void Validate_ComplexReferenceType_Invalid()
        {
            // Arrange
            var validatorProvider = CreateValidatorProvider();
            var actionContext     = new ActionContext();
            var modelState        = actionContext.ModelState;
            var validationState   = new ValidationStateDictionary();

            var validator = CreateValidator();

            var model = (object)new Person();

            validationState.Add(model, new ValidationStateEntry()
            {
                Key = string.Empty
            });

            // Act
            validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model);

            // Assert
            Assert.False(modelState.IsValid);
            AssertKeysEqual(modelState, "Name", "Profession");

            var entry = modelState["Name"];

            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            var error = Assert.Single(entry.Errors);

            Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("Name"), error.ErrorMessage);

            entry = modelState["Profession"];
            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            error = Assert.Single(entry.Errors);
            Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("Profession"), error.ErrorMessage);
        }
Beispiel #4
0
        public void Validate_ForExcludedComplexType_PropertiesMarkedAsSkipped()
        {
            // Arrange
            var validatorProvider = CreateValidatorProvider();
            var actionContext     = new ActionContext();
            var modelState        = actionContext.ModelState;
            var validationState   = new ValidationStateDictionary();

            var validator = CreateValidator(typeof(User));

            var model = new User()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            // Note that user.ConfirmPassword has no entry in modelstate - we should not
            // create one just to mark it as skipped.
            modelState.SetModelValue("user.Password", "password-val", "password-val");
            validationState.Add(model, new ValidationStateEntry()
            {
                Key = "user",
            });

            // Act
            validator.Validate(actionContext, validatorProvider, validationState, "user", model);

            // Assert
            Assert.Equal(ModelValidationState.Valid, modelState.ValidationState);
            AssertKeysEqual(modelState, "user.Password");

            var entry = modelState["user.Password"];

            Assert.Equal(ModelValidationState.Skipped, entry.ValidationState);
            Assert.Empty(entry.Errors);
        }
        public void Validate_CollectionType_DictionaryOfComplexType_Invalid()
        {
            // Arrange
            var validatorProvider = CreateValidatorProvider();
            var modelState        = new ModelStateDictionary();
            var validationState   = new ValidationStateDictionary();

            var validator = CreateValidator();

            var model = (object)new Dictionary <string, Person> {
                { "Joe", new Person() }, { "Mark", new Person() }
            };

            modelState.SetModelValue("[0].Key", "Joe", "Joe");
            modelState.SetModelValue("[1].Key", "Mark", "Mark");
            validationState.Add(model, new ValidationStateEntry()
            {
                Key = string.Empty
            });

            // Act
            validator.Validate(validatorProvider, modelState, validationState, string.Empty, model);

            // Assert
            Assert.False(modelState.IsValid);
            AssertKeysEqual(
                modelState,
                "[0].Key",
                "[0].Value.Name",
                "[0].Value.Profession",
                "[1].Key",
                "[1].Value.Name",
                "[1].Value.Profession");

            var entry = modelState["[0].Key"];

            Assert.Equal(ModelValidationState.Valid, entry.ValidationState);
            Assert.Empty(entry.Errors);

            entry = modelState["[1].Key"];
            Assert.Equal(ModelValidationState.Valid, entry.ValidationState);
            Assert.Empty(entry.Errors);

            entry = modelState["[0].Value.Name"];
            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            var error = Assert.Single(entry.Errors);

            Assert.Equal(error.ErrorMessage, ValidationAttributeUtil.GetRequiredErrorMessage("Name"));

            entry = modelState["[0].Value.Profession"];
            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            error = Assert.Single(entry.Errors);
            Assert.Equal(error.ErrorMessage, ValidationAttributeUtil.GetRequiredErrorMessage("Profession"));

            entry = modelState["[1].Value.Name"];
            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            error = Assert.Single(entry.Errors);
            Assert.Equal(error.ErrorMessage, ValidationAttributeUtil.GetRequiredErrorMessage("Name"));

            entry = modelState["[1].Value.Profession"];
            Assert.Equal(ModelValidationState.Invalid, entry.ValidationState);
            error = Assert.Single(entry.Errors);
            Assert.Equal(error.ErrorMessage, ValidationAttributeUtil.GetRequiredErrorMessage("Profession"));
        }