를 작동하지 않는 경우, 파일 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
형식 검증 밖으로
우를 참조하십시오! – ThiefMaster
정확히 어떤 도구를 사용하고 있습니까? format의 유효성 검사는 선택 사항이지만 대부분의 도구는 특정 형식에 대해 확장/사용자 정의 유효성 검사를 제공 할 수 있도록합니다. – cloudfeet