Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataAnnotation ignored on Record for validation #39631

Closed
1 task done
spaasis opened this issue Jan 19, 2022 · 2 comments
Closed
1 task done

DataAnnotation ignored on Record for validation #39631

spaasis opened this issue Jan 19, 2022 · 2 comments
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@spaasis
Copy link

spaasis commented Jan 19, 2022

Is there an existing issue for this?

  • I have searched the existing issues

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:

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

@javiercn javiercn added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jan 19, 2022
@pranavkm
Copy link
Contributor

Are you specifically using a primary constructor (e.g. public record Person(string FirstName, string LastName)? MVC expects these validation attributes to be declared on the constructor parameter: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#constructor-binding-and-record-types

@spaasis
Copy link
Author

spaasis commented Jan 20, 2022

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

@spaasis spaasis closed this as completed Jan 20, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

No branches or pull requests

3 participants