validate 2 possible types of data in jsonchema











up vote
1
down vote

favorite












I have spent all day trying to get this to work, will post a list of references and things I have tried after the question.



So here is my jsonschema:



{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}
}


Consider the exact piece of jsonschema but with the field1 element as follows:



"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}



  • The first example validates only capital letters in its field1

  • The second wants an integer between 5 and 10.


How can you make it validate either of these, so both are accepted -





  • either only capital letters


  • or an integer between 5 and 10?


Oh - the field1 in the data section above is not that important, it is a desired default value.



I have tried all kinds of ideas -
with oneOf - here, here, here



param - here



additionalProperties - here



required - here



The intuitive thing was to use the oneOf on pattern, but oneOf, as is mentioned in many questions, does not do anything inside the properties section, only outside it. So I tried to have the exact same properties inside a oneOf with just the one difference as described above. That did not work either, and contains a lot of repetition which must somehow be avoidable.



Does anyone know how to solve this? Am out of ideas..










share|improve this question






















  • Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
    – Relequestual
    Nov 8 at 13:56










  • It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
    – cardamom
    Nov 8 at 14:02















up vote
1
down vote

favorite












I have spent all day trying to get this to work, will post a list of references and things I have tried after the question.



So here is my jsonschema:



{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}
}


Consider the exact piece of jsonschema but with the field1 element as follows:



"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}



  • The first example validates only capital letters in its field1

  • The second wants an integer between 5 and 10.


How can you make it validate either of these, so both are accepted -





  • either only capital letters


  • or an integer between 5 and 10?


Oh - the field1 in the data section above is not that important, it is a desired default value.



I have tried all kinds of ideas -
with oneOf - here, here, here



param - here



additionalProperties - here



required - here



The intuitive thing was to use the oneOf on pattern, but oneOf, as is mentioned in many questions, does not do anything inside the properties section, only outside it. So I tried to have the exact same properties inside a oneOf with just the one difference as described above. That did not work either, and contains a lot of repetition which must somehow be avoidable.



Does anyone know how to solve this? Am out of ideas..










share|improve this question






















  • Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
    – Relequestual
    Nov 8 at 13:56










  • It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
    – cardamom
    Nov 8 at 14:02













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have spent all day trying to get this to work, will post a list of references and things I have tried after the question.



So here is my jsonschema:



{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}
}


Consider the exact piece of jsonschema but with the field1 element as follows:



"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}



  • The first example validates only capital letters in its field1

  • The second wants an integer between 5 and 10.


How can you make it validate either of these, so both are accepted -





  • either only capital letters


  • or an integer between 5 and 10?


Oh - the field1 in the data section above is not that important, it is a desired default value.



I have tried all kinds of ideas -
with oneOf - here, here, here



param - here



additionalProperties - here



required - here



The intuitive thing was to use the oneOf on pattern, but oneOf, as is mentioned in many questions, does not do anything inside the properties section, only outside it. So I tried to have the exact same properties inside a oneOf with just the one difference as described above. That did not work either, and contains a lot of repetition which must somehow be avoidable.



Does anyone know how to solve this? Am out of ideas..










share|improve this question













I have spent all day trying to get this to work, will post a list of references and things I have tried after the question.



So here is my jsonschema:



{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}
}


Consider the exact piece of jsonschema but with the field1 element as follows:



"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}



  • The first example validates only capital letters in its field1

  • The second wants an integer between 5 and 10.


How can you make it validate either of these, so both are accepted -





  • either only capital letters


  • or an integer between 5 and 10?


Oh - the field1 in the data section above is not that important, it is a desired default value.



I have tried all kinds of ideas -
with oneOf - here, here, here



param - here



additionalProperties - here



required - here



The intuitive thing was to use the oneOf on pattern, but oneOf, as is mentioned in many questions, does not do anything inside the properties section, only outside it. So I tried to have the exact same properties inside a oneOf with just the one difference as described above. That did not work either, and contains a lot of repetition which must somehow be avoidable.



Does anyone know how to solve this? Am out of ideas..







json validation jsonschema






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 17:07









cardamom

1,73411136




1,73411136












  • Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
    – Relequestual
    Nov 8 at 13:56










  • It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
    – cardamom
    Nov 8 at 14:02


















  • Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
    – Relequestual
    Nov 8 at 13:56










  • It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
    – cardamom
    Nov 8 at 14:02
















Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
– Relequestual
Nov 8 at 13:56




Out of interest, is this an assignment of some kind, or work? Seen a few very similar questions over the past few days?
– Relequestual
Nov 8 at 13:56












It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
– cardamom
Nov 8 at 14:02




It's work. I think everyone is trying to use jsonschema for forms even if it wasn't made for that. I have a question with a bounty at the moment to try to save us from alpaca so we can stay more within Python.
– cardamom
Nov 8 at 14:02












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










You were one the right track with oneOf, except what you actually want is anyOf. Almost every time you think you want oneOf, you really want anyOf. Remember that the values of properties are schemas just like any other. You can use the boolean keywords there just like anywhere else.



{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}


Edit 1



When you hear that oneOf can't be used inside properties, this is the kind of thing they are talking about.



{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}


Edit 2



Because it came up in the comments, here's a better explanation of why oneOf is almost never the right choice. To be clear, oneOf will always work in place of anyOf. If anyOf didn't exist, JSON Schema wouldn't loose any expressive power.



However, anyOf is a more precise tool. Using oneOf when anyOf will do is like using a sledge hammer to drive a nail when you have a simple claw hammer in your toolbox.



anyOf is the boolean OR operation. oneOf is the boolean "exclusive OR" (XOR) operation. "XOR" has so little usefulness, that modern languages don't even have support for it. OR is usually represented with the operator ||. XOR has no analog.



anyOf means any of the items can be true. oneOf means one and only one of the items can be true. When you use oneOf, the validator needs to test all of the schemas to ensure that one schema validates as true and the rest validate as false. When you use anyOf, the validator can stop as soon as it finds a schema that validates as true. This is called "short circuiting" and all modern programming languages do this when evaluating OR operations. When the schemas are mutually exclusive (which they almost always are), continuing to validate schemas after one is found is pure waste and therefore should be avoided.



I think oneOf is overused because from a natural language perspective, it sounds right.






share|improve this answer























  • While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
    – Relequestual
    Nov 8 at 9:32










  • @JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
    – cardamom
    Nov 8 at 9:44










  • Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
    – cardamom
    Nov 8 at 11:11










  • @cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
    – Relequestual
    Nov 8 at 11:52






  • 1




    There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
    – Relequestual
    Nov 8 at 11:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194387%2fvalidate-2-possible-types-of-data-in-jsonchema%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You were one the right track with oneOf, except what you actually want is anyOf. Almost every time you think you want oneOf, you really want anyOf. Remember that the values of properties are schemas just like any other. You can use the boolean keywords there just like anywhere else.



{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}


Edit 1



When you hear that oneOf can't be used inside properties, this is the kind of thing they are talking about.



{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}


Edit 2



Because it came up in the comments, here's a better explanation of why oneOf is almost never the right choice. To be clear, oneOf will always work in place of anyOf. If anyOf didn't exist, JSON Schema wouldn't loose any expressive power.



However, anyOf is a more precise tool. Using oneOf when anyOf will do is like using a sledge hammer to drive a nail when you have a simple claw hammer in your toolbox.



anyOf is the boolean OR operation. oneOf is the boolean "exclusive OR" (XOR) operation. "XOR" has so little usefulness, that modern languages don't even have support for it. OR is usually represented with the operator ||. XOR has no analog.



anyOf means any of the items can be true. oneOf means one and only one of the items can be true. When you use oneOf, the validator needs to test all of the schemas to ensure that one schema validates as true and the rest validate as false. When you use anyOf, the validator can stop as soon as it finds a schema that validates as true. This is called "short circuiting" and all modern programming languages do this when evaluating OR operations. When the schemas are mutually exclusive (which they almost always are), continuing to validate schemas after one is found is pure waste and therefore should be avoided.



I think oneOf is overused because from a natural language perspective, it sounds right.






share|improve this answer























  • While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
    – Relequestual
    Nov 8 at 9:32










  • @JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
    – cardamom
    Nov 8 at 9:44










  • Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
    – cardamom
    Nov 8 at 11:11










  • @cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
    – Relequestual
    Nov 8 at 11:52






  • 1




    There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
    – Relequestual
    Nov 8 at 11:57















up vote
3
down vote



accepted










You were one the right track with oneOf, except what you actually want is anyOf. Almost every time you think you want oneOf, you really want anyOf. Remember that the values of properties are schemas just like any other. You can use the boolean keywords there just like anywhere else.



{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}


Edit 1



When you hear that oneOf can't be used inside properties, this is the kind of thing they are talking about.



{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}


Edit 2



Because it came up in the comments, here's a better explanation of why oneOf is almost never the right choice. To be clear, oneOf will always work in place of anyOf. If anyOf didn't exist, JSON Schema wouldn't loose any expressive power.



However, anyOf is a more precise tool. Using oneOf when anyOf will do is like using a sledge hammer to drive a nail when you have a simple claw hammer in your toolbox.



anyOf is the boolean OR operation. oneOf is the boolean "exclusive OR" (XOR) operation. "XOR" has so little usefulness, that modern languages don't even have support for it. OR is usually represented with the operator ||. XOR has no analog.



anyOf means any of the items can be true. oneOf means one and only one of the items can be true. When you use oneOf, the validator needs to test all of the schemas to ensure that one schema validates as true and the rest validate as false. When you use anyOf, the validator can stop as soon as it finds a schema that validates as true. This is called "short circuiting" and all modern programming languages do this when evaluating OR operations. When the schemas are mutually exclusive (which they almost always are), continuing to validate schemas after one is found is pure waste and therefore should be avoided.



I think oneOf is overused because from a natural language perspective, it sounds right.






share|improve this answer























  • While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
    – Relequestual
    Nov 8 at 9:32










  • @JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
    – cardamom
    Nov 8 at 9:44










  • Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
    – cardamom
    Nov 8 at 11:11










  • @cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
    – Relequestual
    Nov 8 at 11:52






  • 1




    There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
    – Relequestual
    Nov 8 at 11:57













up vote
3
down vote



accepted







up vote
3
down vote



accepted






You were one the right track with oneOf, except what you actually want is anyOf. Almost every time you think you want oneOf, you really want anyOf. Remember that the values of properties are schemas just like any other. You can use the boolean keywords there just like anywhere else.



{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}


Edit 1



When you hear that oneOf can't be used inside properties, this is the kind of thing they are talking about.



{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}


Edit 2



Because it came up in the comments, here's a better explanation of why oneOf is almost never the right choice. To be clear, oneOf will always work in place of anyOf. If anyOf didn't exist, JSON Schema wouldn't loose any expressive power.



However, anyOf is a more precise tool. Using oneOf when anyOf will do is like using a sledge hammer to drive a nail when you have a simple claw hammer in your toolbox.



anyOf is the boolean OR operation. oneOf is the boolean "exclusive OR" (XOR) operation. "XOR" has so little usefulness, that modern languages don't even have support for it. OR is usually represented with the operator ||. XOR has no analog.



anyOf means any of the items can be true. oneOf means one and only one of the items can be true. When you use oneOf, the validator needs to test all of the schemas to ensure that one schema validates as true and the rest validate as false. When you use anyOf, the validator can stop as soon as it finds a schema that validates as true. This is called "short circuiting" and all modern programming languages do this when evaluating OR operations. When the schemas are mutually exclusive (which they almost always are), continuing to validate schemas after one is found is pure waste and therefore should be avoided.



I think oneOf is overused because from a natural language perspective, it sounds right.






share|improve this answer














You were one the right track with oneOf, except what you actually want is anyOf. Almost every time you think you want oneOf, you really want anyOf. Remember that the values of properties are schemas just like any other. You can use the boolean keywords there just like anywhere else.



{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}

}
}
}


Edit 1



When you hear that oneOf can't be used inside properties, this is the kind of thing they are talking about.



{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}


Edit 2



Because it came up in the comments, here's a better explanation of why oneOf is almost never the right choice. To be clear, oneOf will always work in place of anyOf. If anyOf didn't exist, JSON Schema wouldn't loose any expressive power.



However, anyOf is a more precise tool. Using oneOf when anyOf will do is like using a sledge hammer to drive a nail when you have a simple claw hammer in your toolbox.



anyOf is the boolean OR operation. oneOf is the boolean "exclusive OR" (XOR) operation. "XOR" has so little usefulness, that modern languages don't even have support for it. OR is usually represented with the operator ||. XOR has no analog.



anyOf means any of the items can be true. oneOf means one and only one of the items can be true. When you use oneOf, the validator needs to test all of the schemas to ensure that one schema validates as true and the rest validate as false. When you use anyOf, the validator can stop as soon as it finds a schema that validates as true. This is called "short circuiting" and all modern programming languages do this when evaluating OR operations. When the schemas are mutually exclusive (which they almost always are), continuing to validate schemas after one is found is pure waste and therefore should be avoided.



I think oneOf is overused because from a natural language perspective, it sounds right.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 4:29

























answered Nov 8 at 5:23









Jason Desrosiers

7,18511628




7,18511628












  • While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
    – Relequestual
    Nov 8 at 9:32










  • @JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
    – cardamom
    Nov 8 at 9:44










  • Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
    – cardamom
    Nov 8 at 11:11










  • @cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
    – Relequestual
    Nov 8 at 11:52






  • 1




    There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
    – Relequestual
    Nov 8 at 11:57


















  • While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
    – Relequestual
    Nov 8 at 9:32










  • @JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
    – cardamom
    Nov 8 at 9:44










  • Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
    – cardamom
    Nov 8 at 11:11










  • @cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
    – Relequestual
    Nov 8 at 11:52






  • 1




    There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
    – Relequestual
    Nov 8 at 11:57
















While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
– Relequestual
Nov 8 at 9:32




While this schema will work, I'm not sure why you think oneOf is not the correct solution here. @cardamom what happens if you use this schema but replace the anyOf with oneOf? Does it still behave how you expect? In this instance, it should do, but anyOf and oneOf will behave differently in other situations.
– Relequestual
Nov 8 at 9:32












@JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
– cardamom
Nov 8 at 9:44




@JasonDesrosiers thanks for the answer. @Relequestual am still struggling a bit to get everything to work in Alpaca form validation, but if you both think this should work the problem is probably something else.Asked another question re alpaca. What I don't get here is, anyOf is inside properties and everything tells us not to do that, like this and this. Full of contradictions...
– cardamom
Nov 8 at 9:44












Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
– cardamom
Nov 8 at 11:11




Guys, thanks, it works! It does exactly the correct validation if I run it with the Python library fastjsonschema. It's with alpaca that there is some kind of bug. Need to know who to blame..
– cardamom
Nov 8 at 11:11












@cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
– Relequestual
Nov 8 at 11:52




@cardamom Those links regarding anyOf insdie properties... What you're seeing there is, any key of the properties object is relating to a key in the JSON instance. The value of each key in a properties object, is a JSON Schema. So therefore what you have above is OK. Make sense?
– Relequestual
Nov 8 at 11:52




1




1




There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
– Relequestual
Nov 8 at 11:57




There's nothing in the documentation that suggests alpaca.js supports anyOf or any *Of keywords from JSON Schema. JSON Schema is not designed for creation of forms, so libraries which generate forms tend to kind of do their own thing, as they see fit. There are github issues.
– Relequestual
Nov 8 at 11:57


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194387%2fvalidate-2-possible-types-of-data-in-jsonchema%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini