1

I would like to use single JSON schema for both creating and updating (patching) a record in my app. The problem happens with required property, which i don't know how to change for purposes of patching. I should allow partial updates on the record.

I can think of only 2 solutions for this, but none of those looks good to me. I believe there must be a better way.

Solution 1: Modify the schema on the fly for purposes of patching. Basically import the schema, delete the required property or modify it, and use that schema to validate.

Solution 2: On patch request, fetch the existing entry, and apply data that came from the request.

Here's an example json schema:

{
  "$id": "https://myapp.com/schemas/post.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Post",
  "description": "Blog post",
  "properties": {
    "title": {
      "type": "string",
      "description": "Post title"
    },
    "body": {
      "type": "string",
      "description": "Post content"
    }
  },
  "required": ["title", "body"]
}

Initially, when creating a post, i need both properties. Later, i want to allow changing only one entry on it. So client can send to rest API only body, and i should be able to properly validate it against the schema.

Kristijan
  • 323
  • 6
  • 11
  • Can you distinguish between creating a post and updating it later in the JSON data? If so, you could use if-then-else from draft-7. – Clemens Apr 16 '19 at 10:00
  • Possible duplicate of [jsonSchema attribute conditionally required](https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required) – tom redfern Apr 16 '19 at 12:45
  • @tomredfern You have misunderstood the question. The OP isn't asking about conditionally requiring an attribute in the same way as that linked question. – Relequestual Apr 16 '19 at 12:57

2 Answers2

2

JSON Schema has no knowledge of how it is being used, so you cannot intrinsically encode this in JSON Schema.

You could create two schemas, one for whole object validation and one for patch requests. In your validation schema, you'd reference the patch schema, and add required.

However, I wouldn't advise doing this, as you may find you have different validation requirements for creating and patching later down the line that you can't manage this way.

It may be more natural to apply the patch request to the object, then validate using the schema, then save to your database (assuming that's your final step). After all, you may find you have validation conditions which are based on other values (which will mean you'll need additional validation outside of JSON Schema anyway).

Relequestual
  • 11,631
  • 6
  • 47
  • 83
-1

These resources may be useful:

RFC-6902 JavaScript Object Notation (JSON) Patch

https://www.rfc-editor.org/rfc/rfc6902

JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The "application/json-patch+json" media type is used to identify such patch documents.

http://jsonpatch.com/

https://github.com/Starcounter-Jack/JSON-Patch

"Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches."

https://www.npmjs.com/package/fast-json-patch

With JSON-Patch, you can: applyPatch to apply patches applyOperation to apply single operations validate a sequence of patches observe for changes (and generate patches when a change is detected) compare two objects (to obtain the difference).

Community
  • 1
  • 1