0

I was struggling with an issue that if you use data annotations for model validation in ASP.NET Core and you run patchDoc.ApplyTo(newData); and then if (!TryValidateModel(newData)) you got model validation errors for operations not included in the patch document.

If a property was null before and it has a [Required] attribute it will give a model state validation error although I didn't include that property in the patch document.

1 Answer 1

1

My starting solution is to add an extension method for ModelStateDictionary that looks like this

public static void ApplyPatchDocument<T>(this ModelStateDictionary modelState, JsonPatchDocument<T> patchDoc) where T : class
    {
        if (modelState == null)
        {
            throw new ArgumentNullException(nameof(modelState));
        }

        if (patchDoc == null)
        {
            throw new ArgumentNullException(nameof(patchDoc));
        }

        var modelStateKeys = modelState.Keys.ToList();
        for (var i = modelStateKeys.Count - 1; i >= 0; i--)
        {
            var modelStateKey = modelStateKeys[i];
            var modelStateEntry = modelState[modelStateKey];
            if (modelStateEntry.Errors.Count > 0
                && !patchDoc.Operations
                    .Any(op => op.path
                        .TrimStart('/')
                        .Replace('/', '.')
                        .IndexOf(modelStateKey, StringComparison.OrdinalIgnoreCase) > -1))
            {
                modelState.Remove(modelStateKey);
            }
        }
    }

There are issues with this method, for example when you want to change an array property this won't work as it is but it's a good start. Hopefully it helps someone! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.