2017-01-25 2 views
0

스키마는 json 개체 다음에 유효성을 검사해야하며 추가 속성을 허용하지 않아야합니다.다른 속성이 조건부로 존재하도록 json 스키마를 작성하는 방법은 무엇입니까?

{ 
    "id": "11111111111111111111111111111111", 
    "status": "employed", 
    "company_name": "Google" 
} 

{ 
    "id": "00000000000000000000000000000000", 
    "status": "unemployed" 
} 

스키마 초안 V4 :

{ 
    "type": "object", 
    "additionalProperties":false, 
    "required": [ 
    "status", 
    "id" 
    ], 
    "properties": { 
    "id": { 
     "type": "string" 
    }, 
    "status": { 
     "type": "string", 
     "enum":["unemployed", "employed"] 
    }, 
    "company_name": { 
     "type": "string" 
    } 
    }, 
    "dependencies": { 
    "company_name": { 
     "properties": { 
     "status": { 
      "enum": [ 
      "employed" 
      ] 
     } 
     } 
    } 
    } 
} 

이 스키마를 잘 보이지만 그것은 JSON 개체를 다음하지 무효화 않습니다.

{ 
    "id": "00000000000000000000000000000001", 
    "status": "employed" 
} 

"상태"가 "적용"되었지만 필수 속성 "회사 이름"이 제공되지 않으므로 유효하지 않습니다.

+0

참조 http://stackoverflow.com/questions/41837518/removing-the-duplication-in-a-json-schema-that-uses-oneof-v4-or-v5/41838321?noredirect=1#comment70862053_41838321 – esp

+0

@ esp저기서는 분명하지 않습니다 .. 정교하게 부탁해주십시오. 내 관찰에 따라 : 나는 마지막 하나를 사용하고있다. (가장 좋은 것) :) 내가 빠진 것이 있는가? – Nilesh

+0

@esp 나는 대답을 추가했다. ** 의존성을 사용하여 ** 이렇게하는 방법에 대한 또 다른 답변을 추가하십시오. ** – Nilesh

답변

1

anyOf을 사용하면 가능하지만 dependencies을 사용하는 방법은 확실하지 않습니다.

{ 
    "type": "object", 
    "additionalProperties": false, 
    "required": [ 
    "status", 
    "id" 
    ], 
    "properties": { 
    "id": { 
     "type": "string" 
    }, 
    "status": { 
     "type": "string", 
     "enum": [ 
     "unemployed", 
     "employed" 
     ] 
    }, 
    "company_name": { 
     "type": "string" 
    } 
    }, 
    "anyOf": [ 
    { 
     "properties": { 
     "status": { 
      "enum": [ 
      "employed" 
      ] 
     } 
     }, 
     "required": [ 
     "company_name" 
     ] 
    }, 
    { 
     "properties": { 
     "status": { 
      "enum": [ 
      "unemployed" 
      ] 
     } 
     }, 
     "not": { 
     "required": [ 
      "company_name" 
     ] 
     } 
    } 
    ] 
} 

덕분에 @esp.