예제 #1
0
        public void Validate_ShortCircuits_IfModelStateHasReachedMaxNumberOfErrors()
        {
            // Arrange
            var model = new ValidateAllPropertiesModel
            {
                RequiredString = null /* error */,
                RangedInt      = 0 /* error */,
                ValidString    = "cat" /* error */
            };

            var modelMetadata = GetModelMetadata(model);
            var node          = new ModelValidationNode(modelMetadata, "theKey")
            {
                ValidateAllProperties = true
            };
            var context = CreateContext(modelMetadata);

            context.ModelState.MaxAllowedErrors = 3;
            context.ModelState.AddModelError("somekey", "error text");

            // Act
            node.Validate(context);

            // Assert
            Assert.Equal(3, context.ModelState.Count);
            Assert.IsType <TooManyModelErrorsException>(context.ModelState[""].Errors[0].Exception);
            Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("RequiredString"),
                         context.ModelState["theKey.RequiredString"].Errors[0].ErrorMessage);
            Assert.False(context.ModelState.ContainsKey("theKey.RangedInt"));
            Assert.False(context.ModelState.ContainsKey("theKey.ValidString"));
        }
예제 #2
0
        public void ProcessDto_RequiredFieldMissing_RaisesModelError()
        {
            // Arrange
            var model             = new ModelWithRequired();
            var containerMetadata = GetMetadataForObject(model);
            var bindingContext    = CreateContext(containerMetadata);

            // Set no properties though Age (a non-Nullable struct) and City (a class) properties are required.
            var dto            = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.ProcessDto(bindingContext, dto);

            // Assert
            var modelStateDictionary = bindingContext.ModelState;

            Assert.Equal(false, modelStateDictionary.IsValid);
            Assert.Equal(2, modelStateDictionary.Count);

            // Check Age error.
            ModelState modelState;

            Assert.True(modelStateDictionary.TryGetValue("theModel.Age", out modelState));
            Assert.Equal(1, modelState.Errors.Count);

            var modelError = modelState.Errors[0];

            Assert.Null(modelError.Exception);
            Assert.NotNull(modelError.ErrorMessage);
            var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.Age));

            Assert.Equal(expected, modelError.ErrorMessage);

            // Check City error.
            Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState));
            Assert.Equal(1, modelState.Errors.Count);

            modelError = modelState.Errors[0];
            Assert.Null(modelError.Exception);
            Assert.NotNull(modelError.ErrorMessage);
            expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City));
            Assert.Equal(expected, modelError.ErrorMessage);
        }
예제 #3
0
        public void ProcessDto_RequiredFieldNull_RaisesModelError()
        {
            // Arrange
            var model             = new ModelWithRequired();
            var containerMetadata = GetMetadataForObject(model);
            var bindingContext    = CreateContext(containerMetadata);

            var dto            = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
            var testableBinder = new TestableMutableObjectModelBinder();

            // Make Age valid and City invalid.
            var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "Age");

            dto.Results[propertyMetadata] =
                new ComplexModelDtoResult(23, new ModelValidationNode(propertyMetadata, "theModel.Age"));
            propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "City");
            dto.Results[propertyMetadata] =
                new ComplexModelDtoResult(null, new ModelValidationNode(propertyMetadata, "theModel.City"));

            // Act
            testableBinder.ProcessDto(bindingContext, dto);

            // Assert
            var modelStateDictionary = bindingContext.ModelState;

            Assert.Equal(false, modelStateDictionary.IsValid);
            Assert.Equal(1, modelStateDictionary.Count);

            // Check City error.
            ModelState modelState;

            Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState));
            Assert.Equal(1, modelState.Errors.Count);

            var modelError = modelState.Errors[0];

            Assert.Null(modelError.Exception);
            Assert.NotNull(modelError.ErrorMessage);
            var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City));

            Assert.Equal(expected, modelError.ErrorMessage);
        }
        public void GetClientValidationRules_ReturnsValidationParameters()
        {
            // Arrange
            var expected  = ValidationAttributeUtil.GetRequiredErrorMessage("Length");
            var provider  = new DataAnnotationsModelMetadataProvider();
            var metadata  = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
            var attribute = new RequiredAttribute();
            var adapter   = new RequiredAttributeAdapter(attribute);
            var context   = new ClientModelValidationContext(metadata, provider);

            // Act
            var rules = adapter.GetClientValidationRules(context);

            // Assert
            var rule = Assert.Single(rules);

            Assert.Equal("required", rule.ValidationType);
            Assert.Empty(rule.ValidationParameters);
            Assert.Equal(expected, rule.ErrorMessage);
        }
예제 #5
0
        public void MultipleValidationErrorsOnSameMemberReported()
        {
            // Arrange
            var model = new Address()
            {
                Street = "Microsoft Way"
            };
            var validationContext = GetModelValidationContext(model, model.GetType());

            // Act (does not throw)
            new DefaultBodyModelValidator().Validate(validationContext, keyPrefix: string.Empty);

            // Assert
            Assert.Contains("Street", validationContext.ModelState.Keys);
            var streetState = validationContext.ModelState["Street"];

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

            Assert.Contains(ValidationAttributeUtil.GetStringLengthErrorMessage(null, 5, "Street"), errorCollection);
            Assert.Contains(ValidationAttributeUtil.GetRegExErrorMessage("hehehe", "Street"), errorCollection);
        }