{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
"required": ["firstName", "lastName", "age"]
}
(edited after @gregdennis's answer)
Given the above schema, a valid data would be
{
"firstName": "John",
"lastName": "Doe",
"age": 21
}
But I want to make it "optional", as in I want to allow empty object
// should pass
{}
But not half schemas
// Shouldn't pass
{
"firstName": "John"
}
What you have is already optional, so the empty object should pass. In order to make those properties required you need to put them in a required
keyword.
"required": [ "first name", "last name", "age" ]
But just adding this keyword removes the ability to validate the empty object.
To fix that, wrap this in a oneOf
asking with another schema that only accepts the empty object.
{
"oneOf": [
{ "const": {} },
{
// your schema from above along with required
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.