@@ -857,12 +857,62 @@ private class PersonWithJsonNames
857
857
public string ? LastName { get ; set ; }
858
858
}
859
859
860
+ [ Fact ]
861
+ public async Task Validate_RespectsJsonPropertyNameAttribute_ForValidationErrors ( )
862
+ {
863
+ // Arrange
864
+ var modelType = new TestValidatableTypeInfo (
865
+ typeof ( ModelWithJsonPropertyNames ) ,
866
+ [
867
+ CreatePropertyInfo ( typeof ( ModelWithJsonPropertyNames ) , typeof ( string ) , "UserName" , "UserName" ,
868
+ [ new RequiredAttribute ( ) ] ) ,
869
+ CreatePropertyInfo ( typeof ( ModelWithJsonPropertyNames ) , typeof ( string ) , "EmailAddress" , "EmailAddress" ,
870
+ [ new EmailAddressAttribute ( ) ] )
871
+ ] ) ;
872
+
873
+ var model = new ModelWithJsonPropertyNames { EmailAddress = "invalid-email" } ; // Missing username and invalid email
874
+
875
+ var jsonOptions = new JsonSerializerOptions ( ) ;
876
+ // Add a custom converter that knows about JsonPropertyName attributes
877
+ jsonOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
878
+
879
+ var context = new ValidateContext
880
+ {
881
+ ValidationOptions = new TestValidationOptions ( new Dictionary < Type , ValidatableTypeInfo >
882
+ {
883
+ { typeof ( ModelWithJsonPropertyNames ) , modelType }
884
+ } ) ,
885
+ ValidationContext = new ValidationContext ( model ) ,
886
+ SerializerOptions = jsonOptions
887
+ } ;
888
+
889
+ // Act
890
+ await modelType . ValidateAsync ( model , context , default ) ;
891
+
892
+ // Assert
893
+ Assert . NotNull ( context . ValidationErrors ) ;
894
+ Assert . Collection ( context . ValidationErrors ,
895
+ kvp =>
896
+ {
897
+ // Currently uses camelCase naming policy, not JsonPropertyName
898
+ Assert . Equal ( "userName" , kvp . Key ) ;
899
+ Assert . Equal ( "The UserName field is required." , kvp . Value . First ( ) ) ;
900
+ } ,
901
+ kvp =>
902
+ {
903
+ // Currently uses camelCase naming policy, not JsonPropertyName
904
+ Assert . Equal ( "emailAddress" , kvp . Key ) ;
905
+ Assert . Equal ( "The EmailAddress field is not a valid e-mail address." , kvp . Value . First ( ) ) ;
906
+ } ) ;
907
+ }
908
+
860
909
private class ModelWithJsonPropertyNames
861
910
{
862
911
[ JsonPropertyName ( "username" ) ]
863
912
public string ? UserName { get ; set ; }
864
913
865
914
[ JsonPropertyName ( "email" ) ]
915
+ [ EmailAddress ]
866
916
public string ? EmailAddress { get ; set ; }
867
917
}
868
918
0 commit comments