I'm trying to create a JSON schema that validates against multiple different entities that have mostly the same attributes but only differ in a few attributes.
{
"firstname": "Bat",
"lastname": "man",
"email": "[email protected]"
}
{
"firstname": "Super",
"lastname": "man",
"phone": "543-453-4523"
}
{
"firstname": "Wonderwo",
"lastname": "man",
"email": "[email protected]"
}
Basically I want to create a single schema that makes sure the last name is "man" and either have a phone or email attribute.
I was trying to implement this using oneOf, like this:
{
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string",
"pattern": "man"
},
"oneOf": [{
"email": {
"type": "string"
},
"phone": {
"type": "string"
}
}]
}
}
But I don't think this works. Is something like this even possible with JSON schema? And how can I achieve this?
You have several problems:
You need:
{
"type": "object",
"required": ["firstname", "lastname"],
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string",
"enum": ["man"]
},
"email": {
"type": "string"
},
"phone": {
"type": "string"
}
},
"anyOf": [
{ "required": ["email"] },
{ "required": ["phone"] }
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With