You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just noticed that validation ignores data annotations on fully defined records but work on classes.
Expected Behavior
DataAnnotations for properties, such as MaxLength, work the same for classes and fully defined records.
Steps To Reproduce
record:
public record Person {
public Person( string firstName, string lastName) { //...}
[MaxLength(50)]
public string FirstName { get; init; }
[MaxLength(50)]
public string LastName { get; init; }
}
api-method:
[HttpPost]
public async Task<ActionResult> Post([FromBody] Person input) {
if (ModelState.IsValid) {
return Ok();
}
return BadRequest(ModelState);
}
When Person is record and you post a LastName with more than 50 characters, the ModelState is valid.
If Person is changed to a class (without any other changes), the validation error is detected.
Exceptions (if any)
No response
.NET Version
6
Anything else?
No response
The text was updated successfully, but these errors were encountered:
Hi, public record Person([property:MaxLength(50)] string FirstName,[property:MaxLength(50)] string LastName); is ignored public record Person([MaxLength(50)] string FirstName,[MaxLength(50)] string LastName); works public record Person([param: MaxLength(50)] string FirstName, [param: MaxLength(50)] string LastName); works
So yeah, it seems that when the attribute targets the parameter rather than property it's included.
This makes some things harder (like checking if a record property has a specific parameter) but I'll see if I can work around that. Thanks for pointing me to the right direction
Is there an existing issue for this?
Describe the bug
Hi,
I just noticed that validation ignores data annotations on fully defined records but work on classes.
Expected Behavior
DataAnnotations for properties, such as MaxLength, work the same for classes and fully defined records.
Steps To Reproduce
record:
api-method:
When
Person
is record and you post a LastName with more than 50 characters, the ModelState is valid.If Person is changed to a class (without any other changes), the validation error is detected.
Exceptions (if any)
No response
.NET Version
6
Anything else?
No response
The text was updated successfully, but these errors were encountered: