2013-12-21 1 views
3

를 작동하지 않는 경우, 파일 API.json은 다음과 같다 :JSON 스키마 검증 IP 주소가 하나의 DICT에 IP 주소를 확인하지 못했습니다

from jsonschema import Draft3Validator, ValidationError, FormatChecker 
import json 

if __name__ == '__main__': 
    graphics1 = {'type': 'spice', 'listen': '0.0.0.0'} 
    graphics2 = {'type': 'vnc', 'listen': '0.0.0.0'} 
    graphics3 = {'type': 'abc', 'listen': '0.0.0.0'} 
    graphics4 = {'type': 'vnc', 'listen': '777.485.999'} 
    graphics5 = {'type': 'vnc', 'listen': 'fe00::0'} 
    graphics6 = {'type': 'vnc', 'listen': 'def'} 
    graphics7 = {'type': 'vnc', 'listen': 'fe00::0abcdefdefs'} 
    s = json.load(open('API.json')) 
    validator = Draft3Validator(s, format_checker=FormatChecker()) 
    for x in range(1, 8): 
     try: 
      graphics = locals().get('graphics'+str(x)) 
      validator.validate(graphics) 
     except ValidationError: 
      print('; '.join(e.message for e in validator.iter_errors(graphics))) 

그리고 다음과 같이

{ 
"$schema": "http://json-schema.org/draft-03/schema#", 
"title": "test", 
"type": "object", 
"properties": { 
    "type": {"enum": ["spice", "vnc"]}, 
    "listen": { 
     "type": "string", 
     "oneOf": [ 
      {"format": "ipv4"}, 
      {"format": "ipv6"} 
     ] 
    } 
}, 
"additionalProperties": false 
} 

코드는 인쇄는 다음과 같습니다

분명히
'abc' is not one of [u'spice', u'vnc'] 

, '777.485.999', 'DEF'및 'fe00 :: 0abcdefdefs'을 IP 주소로되지 않지만, 테스트 스크립트는하지 않습니다 그들에 대해 경고하십시오. 'ip-address'에 대해 말하는 하나의 문서 (http://tools.ietf.org/html/draft-zyp-json-schema-03)가 있지만 'ipv4'는 아니지만 작동하지 않습니다.

[편집] : Draft3Validator에 FormatChecker()가 추가되었지만 여전히 작동하지 않습니다. 하지만 내가 시도한 것처럼, Draft4Validator는 괜찮습니다. 문서에서 Draft3Valdator가 어디서든 format/ip-address를 지원하지 않는다고 생각하지는 않습니다. 작동해야합니다. docs

형식 검증 밖으로

+3

우를 참조하십시오! – ThiefMaster

+0

정확히 어떤 도구를 사용하고 있습니까? format의 유효성 검사는 선택 사항이지만 대부분의 도구는 특정 형식에 대해 확장/사용자 정의 유효성 검사를 제공 할 수 있도록합니다. – cloudfeet

답변

3

Draft3Validator는 "format/ip-address"가 아니라 "oneOf", "allOf", "anyOf"및 "not"를 지원하기 때문에 아닙니다. 그래서 API.json가 있어야한다 : 당신은()`해킹 및 다른 변수이 끔찍한`지역 주민 대신 목록을 사용하지 않는 이유

{ 
"$schema": "http://json-schema.org/draft-03/schema#", 
"title": "test", 
"type": "object", 
"properties": { 
    "type": {"enum": ["spice", "vnc"]}, 
    "listen": { 
     "type": "string", 
     "format": "ip-address" 
    } 
}, 
"additionalProperties": false 
} 

http://json-schema.org/draft-03/schema#http://json-schema.org/draft-04/schema#

2

검사는 선택 사항입니다, 당신은 그것을 가능하게하는 형식 검사가 필요합니다.

+0

감사합니다, 줄리안. 답이 도움이됩니다. – apporc