Merge pull request #1065 from abpframework/maliming/DtoCustomValidate

fix #1058 Perform non-null validation on propertyName(context.Key.Name).
pull/1068/head
Halil İbrahim Kalkan 7 years ago committed by GitHub
commit 1b5d08818b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -39,6 +39,11 @@ namespace Volo.Abp.AspNetCore.Mvc
return;
}
if (context.Key.Name.IsNullOrWhiteSpace())
{
return;
}
if (_localizationOptions.Value.Value.DataAnnotationLocalizerProvider == null)
{
return;

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
@ -16,6 +17,14 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
return Task.FromResult(model.Value1);
}
[HttpGet]
[Route("object-result-action-with-custom_validate")]
public Task<string> ObjectResultActionWithCustomValidate(CustomValidateModel model)
{
ModelState.IsValid.ShouldBeTrue(); //AbpValidationFilter throws exception otherwise
return Task.FromResult(model.Value1);
}
[HttpGet]
[Route("action-result-action")]
public IActionResult ActionResultAction(ValidationTest1Model model)
@ -29,5 +38,19 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
[MinLength(2)]
public string Value1 { get; set; }
}
public class CustomValidateModel : IValidatableObject
{
public string Value1 { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Value1 != "hello")
{
yield return new ValidationResult("Value1 should be hello");
}
}
}
}
}

@ -47,5 +47,16 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
var result = await GetResponseAsStringAsync("/api/validation-test/action-result-action"); //Missed the value1
result.ShouldBe("ModelState.IsValid: false");
}
[Fact]
public async Task Should_Return_Custom_Validate_Errors()
{
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>(
"/api/validation-test/object-result-action-with-custom_validate?value1=abc", HttpStatusCode.BadRequest); //value1 should be hello.
result.Error.ValidationErrors.Length.ShouldBeGreaterThan(0);
result.Error.ValidationErrors.ShouldContain(x => x.Message == "Value1 should be hello");
}
}
}

Loading…
Cancel
Save