2016-12-12 1 views
4

나는 값을 보이는 개체 중 하나입니다 임의의 키를 수있는 개체를 확인하려고 내에서 같은 : { "href": "some string" } JSON 스키마 : 실행 하는가를 사용 oneOf, allOf additionalProperties

또는 배열을 포함하는 객체의 위의 일치 .

{ 
    "$schema": "http://json-schema.org/schema#", 
    "id": "https://turnstyle.io/schema/links.json", 
    "type": "object", 
    "additionalProperties": { 
     "oneOf": [ 
      { 
       "type": "object", 
       "required": "href" 
      }, 
      { 
       "type": "array", 
       "items": { 
        "type": "object", 
        "required": "href" 
       } 
      } 
     ] 
    } 
} 



Passing example: 
{ 
    "customer": { "href": "/customer/1" }, 
    "products": [ 
     { "href": "/product/1" }, 
     { "href": "/product/2" } 
    ] 
} 

Failing example: 
{ 
    "customer": { "wrongKey": "/customer/1" }, 
    "products": "aString" 
} 

이 가능하고, 올바른 구문은 무엇인가 그렇다면 다음은

내가 현재 가지고 작동하지 않습니다 무엇인가?

내 생각에 oneOf|anyOf|allOfadditionalProperties에있는 통과 스키마가 additionalProperties에 해당하는 모든 키에 적용되어야하기 때문에이 방법은 작동하지 않습니다.

+0

"내 가정이 oneOf에서 통과 스키마 (들) 때문에이 작동하지 것입니다 | 실행 하는가가 | additionalProperties의 allOf가 모든 키를 additionalProperties에 해당에 적용해야합니다." 아니요, oneOf 내부의 다른 스키마에 따라 다른 키가 유효 할 수 있습니다. 또한 anyOf는이 경우 더 효율적입니다. – esp

답변

4

"필수"는 v4에서 필수 속성의 배열이어야합니다.

또는 "필수": v3의 속성의 일부로 true (또는 false).

이 시도 :

{ 
    "$schema": "http://json-schema.org/schema#", 
    "id": "https://turnstyle.io/schema/links.json", 
    "type": "object", 
    "additionalProperties": { 
     "oneOf": [ 
      { 
       "type": "object", 
       "properties": { 
        "href": {"type": "string"} 
       }, 
       "required": ["href"] 
      }, 
      { 
       "type": "array", 
       "items": { 
        "type": "object", 
        "properties": { 
         "href": {"type": "string"} 
        }, 
        "required": ["href"] 
       } 
      } 
     ] 
    } 
} 
+0

이 스키마에 "필수"키워드가 없습니다. 빈 객체를 값 또는 빈 객체의 배열로 허용합니다. –

+0

@AsyaKamsky 당신 말이 맞습니다. 그것이 실제로 받아 들여지는 입력인지 아닌지는 명확하지 않지만 보이지는 않습니다. 따라서 스키마를 적절하게 업데이트했습니다. – Pedro