2016-11-03 51 views
0

模式是用于与头属性的消息,然后的msg1msg2任一特性:JSON架构oneOf不与参考工作

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 

    "definitions": { 
     "header": { 
      "type": "object", 
      "properties": { 
       "token":   { "type": "string" }, 
       "id":   { "type": "number" } 
      }, 
      "required": ["token", "id"] 
     }, 
     "msg1": { 
      "type": "object", 
      "properties": { 
       "content1":   { "type": "string" } 
      }, 
      "required": ["content1"] 
     }, 
     "msg2": { 
      "type": "object", 
      "properties": { 
       "content2":   { "type": "string" } 
      }, 
      "required": ["content2"] 
     } 
    }, 

    "type": "object", 
    "$ref": "#/definitions/header", 
    "oneOf": [ 
     {"$ref": "#/definitions/msg1" }, 
     {"$ref": "#/definitions/msg2" } 
    ] 
} 

所以这应该通过:

{ 
    "token": "abc123", 
    "id": 333, 
    "content1": "s" 
} 

的问题是,下列通行证:

{ 
    "token": "abc123", 
    "id": 333 
} 

如何解决?

(当然,也有很多的msg# S和他们有不同的结构),比$ref其他

+0

你正在使用什么验证器?按照规范,如果对象具有$ ref,则应忽略其他关键字(并且您在顶层具有$ ref)。所以可能是oneOf被忽略。但不同的验证器实现它的方式不同。 – esp

+0

@esp,没错。 – Velkan

回答