添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (type, cost).

Here are some sample objects:

{"firstName":"John", "lastName":"Doe", "sport": "football"}
{"vehicle":"car", "price":20000}

The question is what have I done wrongly and how can I fix it. Here is the schema:

"description": "schema validating people and vehicles", "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "required": [ "oneOf" ], "properties": { "oneOf": [ "firstName": {"type": "string"}, "lastName": {"type": "string"}, "sport": {"type": "string"} "vehicle": {"type": "string"}, "price":{"type": "integer"}

When I try to validate it in this parser:

https://json-schema-validator.herokuapp.com/

I get the following error:

"level" : "fatal", "message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n \"level\" : \"error\",\n \"schema\" : {\n \"loadingURI\" : \"#\",\n \"pointer\" : \"/properties/oneOf\"\n },\n \"domain\" : \"syntax\",\n \"message\" : \"JSON value is of type array, not a JSON Schema (expected an object)\",\n \"found\" : \"array\"\n} ]", "info" : "other messages follow (if any)" "level" : "error", "schema" : { "loadingURI" : "#", "pointer" : "/properties/oneOf" "domain" : "syntax", "message" : "JSON value is of type array, not a JSON Schema (expected an object)", "found" : "array" Does oneOf help in validating the request? I can't see any jackson annotation for oneOf during pojo conversion – gursahib.singh.sahni May 3, 2017 at 8:50 @gursahib.singh.sahni, that is a jackson specific problem. It is not easy to translate oneOf semantics to some programming languages such as java. You need some construct like Disjoint sets, Discriminated Unions, Union Types... that other languages have. – jruizaranguren May 3, 2017 at 9:47 @jruizaranguren there any library other than jackson that handles this case ? or else we would have to add custom validators in our codebase. – gursahib.singh.sahni May 3, 2017 at 9:50 @AlirezaMirian, if you do not explicitly specify it, any other type would satisfy the specification, because "oneOf" only applies to types. It is ignored for other types. – jruizaranguren Dec 29, 2017 at 14:52 Well it makes sense from deduction of what properties does, i.e. why would properties ever make a special case for keys named oneOf? Pretty much common sense it works this way now that I think of it. Thanks for the explanation. – davidtgq Oct 19, 2017 at 0:17

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.