4

I have a JSON PATCH endpoint in my API, but I don't want to allow all properties to be edited and eventually need to do specific actions when a property has changed.

I am having some trouble finding the best solution to achieve this.

Running NET Core 3.1.

Example of the object I have:

{
    "id": "string",
    "name": "string",
    "type": "string",
    "fields": [
        {
            "name": "string",
            "label": "string",
            "type": "Unknown",
            "description": "string",
            "required": true,
            "defaultValue": "string",
            "allowedValues": [
                "string"
            ]
        }
    ]
}

Example:

  • Do not allow the id to be changed.
  • When a field is changed to required, then defaultValue should be set as well.
  • If type as specific value, then allowedValues cannot be empty and if defaultValue is set as well, then this defaultValue should be part of alllowedValues.
  • etc.

So first is defining the rules for what is allowed to do and what not.

For that part I can manage most of it I think with defining Rules with FluentValidation. But the hard part is to figure out, which fields actually have changed. Mainly in the fields array. Any suggestions how to get that?

Second I want to do certain actions if a property has changed, but only if the property has changed.

Any suggestions how to best achieve this?

Edit: Ideally a C# alternative to this package: json-patch-rules.

Menno
  • 142
  • 8
  • I think you will need to query the original object from the db or whereever it is stored and create a diff for both versions. – Ackdari Sep 04 '20 at 12:57
  • Yes so I get the original object from the db. Any suggestion for creating the diff? – Menno Sep 04 '20 at 13:23
  • Take a look at [this](https://github.com/wbish/jsondiffpatch.net) and/or [this](https://github.com/replaysMike/AnyDiff). The later is probably more usefull for you, since I don't know if you can configure patch rules for JsonDiffPatch – Ackdari Sep 04 '20 at 13:37
  • Yeah had a look at JsonDiffPatch before, but didn't really fit my needs. AnyDiff might work for my needs. I will give it a try, thanks! – Menno Sep 04 '20 at 14:06
  • 1
    For future questions: It might be usefull to mention in the question if you already tried certain populare packages, but didn't fit for what you were looking for. Also if you find a solution that is worth sharing, please remember that you can answer your own question. – Ackdari Sep 04 '20 at 17:37

0 Answers0