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.