If I have an object like this:
{
"name": "John",
"details": "{"age": 30, "email": "[email protected]"}"
}
Is it possible to describe nested JSON in "details"
with a schema inside of an outer schema?
I imagine something like this:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"details": {
"type": "string",
"contentMediaType": "application/json",
"schema": {
"type": "object",
"properties": {
"age": {
"type": "number"
},
"email": {
"type": "string"
}
}
}
}
}
}
2
Answers
No, the
details
is only validated as astring
. It’s not possible to apply further schema constraints on the string value in this scenario.Yes, if you are using the latest draft, 2020-12. See section 8. A Vocabulary for the Contents of String-Encoded Data. You already have the
contentMediaType
; this draft addscontentSchema
.Note, however, that you will find very limited support in tooling for validating the decoded JSON against this schema. The spec states "[these keywords] do not function as validation assertions; a malformed string-encoded document MUST NOT cause the containing instance to be considered invalid." So, yes, you can describe the contents of the JSON string, but that description will not be validated unless your application takes further steps to perform that validation.