2017-11-06 9 views
1

다음 json에서 배열 names의 요소 중 하나 이상에 nickName 값이 Ginny 인 것을 어떻게 확인할 수 있습니까?JSON 스키마 : 배열에 주어진 값을 가진 속성이있는 객체가 하나 이상 포함되어 있는지 확인하는 방법은 무엇입니까?

{ 
    "names": [ 
    { 
     "firstName": "Hermione", 
     "lastName": "Granger" 
    }, { 
     "firstName": "Harry", 
     "lastName": "Potter" 
    }, { 
     "firstName": "Ron", 
     "lastName": "Weasley" 
    }, { 
     "firstName": "Ginevra", 
     "lastName": "Weasley", 
     "nickName": "Ginny" 
    } 
    ] 
} 

현재 초안 06 버전 (FAQ here)을 사용하고 있습니다.

이 내 작동하지 않는 스키마입니다 :

{ 
    "$schema": "http://json-schema.org/draft-06/schema#", 
    "title": "Complex Array", 
    "description": "Schema to validate the presence and value of an object within an array.", 

    "type": "object", 
    "properties": { 
    "names": { 
     "type": "array", 
     "minItems": 1, 
     "items": { 
     "type": "object", 
     "properties": { 
      "firstName": { 
      "type": "string" 
      }, 
      "lastName": { 
      "type": "string" 
      }, 
      "nickName": { 
      "type": "string" 
      } 
     }, 
     "anyOf": [ 
      {"required": ["nickName"]} 
     ] 
     } 
    } 
    } 
} 
+0

당신은 [ "경우 ParameterName"] = 전무 딕셔너리를 사용하여 확인 후 사전 형식으로 JSON을 변환해야합니다. –

답변

1

내가 draft-06를 사용하여 알아낼 수 있었다. 이 버전에서는 새 키워드 contains가 추가되었습니다. 이 초안 specification에 따르면

이 키워드의 값을 포함하는 유효한 JSON 스키마해야합니다. 배열 인스턴스는 요소 중 적어도 하나가 지정된 스키마에 대해 유효하면 "contains"에 대해 유효합니다.

근무 스키마!

{ 
    "$schema": "http://json-schema.org/draft-06/schema#", 
    "title": "Complex Array", 

    "type": "object", 
    "properties": { 
    "names": { 
     "type": "array", 
     "minItems": 1, 
     "contains": { 
     "type": "object", 
     "properties": { 
      "firstName": { 
      "type": "string" 
      }, 
      "lastName": { 
      "type": "string" 
      }, 
      "nickName": { 
      "type": "string", 
      "pattern": "^Ginny$" 
      } 
     }, 
     "required": ["nickName"] 
     }, 
     "items": { 
     "type": "object", 
     "properties": { 
      "firstName": { 
      "type": "string" 
      }, 
      "lastName": { 
      "type": "string" 
      }, 
      "nickName": { 
      "type": "string" 
      } 
     } 
     } 
    } 
    } 
}