Skip to content

TryValidateModel is not working for nested attributes #12

Closed
@ThumNet

Description

@ThumNet

Hi Shibayan,

When using TryValidateModel method DataAnnotations attributes (for example [Required]) on nested objects/properties are not validated.

public class SampleModel
{
    [Required]
    public string Name { get; set; }

    public string[] Array { get; set; }

    public NestedModel Nested { get; set; }
}

public class NestedModel
{
    [Required]
    public string Name { get; set; }
}

When posting the SampleModel and omitting the Name property, TryValidateModel returns true and not false as expected.

Tried with:

{
    "name": "test",
    "nested":{       
    }
}

and also with:

{
    "name": "test"
}

This is what I expected:
image

Is this something you are aware of or a bug?

Ps I found a fix for the behavior described above, and if you are interested I can provide a PR.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions