-
-
Notifications
You must be signed in to change notification settings - Fork 158
PATCH with ValidateModelState enabled and [Required] attribute #472
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
Comments
@milosloub I don't have a solution for this off the top of my head. But, to be clear, the desired behavior is that for The nice part is that we might be able to implement this as a non-breaking change with C# 8's default interface methods. |
@jaredcnance yes, that's exactly that case - validating only PATCHed attributes. After few tries I discovered that JsonPatchDocument also doesn't solve this issue. Maybe I missed something general with [Required] and there must be some custom validation. So now I don't know, I'm back at the beginning Ach sooo, this is the case for default interface methods (I was trying to find real world usage 😄) |
It looks like it should work but I’ll have to dig in more. It may be better to have a custom validation implementation instead of parsing into JsonPatchDocument which seems to be unnecessarily coupled to the JSON PATCH specification. That said, I think there’s a good deal we can learn from their API design. In the future I would prefer to migrate to something similar instead of passing the metadata through the context. |
Ah, I found my bad approach of testing this. It is required, that you have full object that proceed through POST and it's validation. In my case I only created new instance of empty model, than validation failed. If I use model with correctly filled properties, then solution above works as expected. So instead of
But still I think, that custom validation should be more effective than this "heavy" object approach. What do you think? |
Agreed. I don't want to re-invent the wheel, so it'd be interesting to see how |
I found another problem maybe related to this. public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public Pet Pet { get; set; }
}
public class Pet
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
} Now if try to post a new |
Hi, I am also experiencing issues with this scenario. Most of my resources contain basic validations that are checked in the BaseJsonApiController Post and Patch methods when the option to ValidateModelState is true. As above, if I patch a subset of fields and there are other fields that are flagged as required, I am getting a validation error stating as such, even though I am not modifying those fields. I also came across a scenario where Patch is failing because I have validations that are dependent on multiple fields in the resource. If I patch only one of those fields, the validations will fail because dependent data is not present, even though the eventual update would be valid. E.g.
I did play around with overriding the PatchAsync method in a custom controller where I retrieved the entity from the DB and patched in the values not in the targeted fields list to then be able to validate a 'completed entity'. This worked ok for basic scenarios, but i suspect there might be issues or constraints with this method, particularly with relationships / included resources. I guess the only other option would be to turn off ValidateModelState and delay and manage the validations in my DbContext prior to SaveChanges where all entities should be populated? Regards, |
…tionships (#781) Fixes #472 This feature allows the Required validator to be disabled. This will allow partial patching in the case that the attribute is excluded from a patch. Validation is applied when the attribute is present in post or patch. Required validation also disabled for a relationships attributes (in attempt to fix [#472 (comment)](#472 (comment))). Co-authored-by: Bart Koelman <[email protected]>
…tionships (#781) Fixes #472 This feature allows the Required validator to be disabled. This will allow partial patching in the case that the attribute is excluded from a patch. Validation is applied when the attribute is present in post or patch. Required validation also disabled for a relationships attributes (in attempt to fix [#472 (comment)](json-api-dotnet/JsonApiDotNetCore#472 (comment))). Co-authored-by: Bart Koelman <[email protected]>
If I provide ValidateModelState =true, then every PATCH failed on validation
on properties with [Required] attribute.
For example I have three properties with [Required], but sending patch to one of them only. Then
two of them failed on validation.
I found that AspNetCore uses JsonPatchDocument for this use case, but maybe I missed something.
Do you know how to deal with it?
The text was updated successfully, but these errors were encountered: