Created
April 24, 2013 05:20
-
-
Save jimklo/5449805 to your computer and use it in GitHub Desktop.
jsonschema bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jsonschema import * | |
import pprint | |
working_good_schema = { | |
"type": "array", | |
"items": { | |
"patternProperties": { | |
"a|b|c": { "type": "string" } | |
}, | |
"additionalProperties": False | |
}, | |
"minItems":1 | |
} | |
nonworking_good_schema = { | |
"type": "array", | |
"items": [], | |
"additionalItems": { | |
"patternProperties": { | |
"a|b|c": { "type": "string" } | |
}, | |
"additionalProperties": False | |
}, | |
"minItems": 1 | |
} | |
good_data = [{ | |
"a": "foo", | |
"b": "foo", | |
"c": "foo" | |
}] | |
bad_data = [{ | |
"a": "bar", | |
"b": "bar", | |
"c": "bar", | |
"d": "bar" | |
}] | |
def valid(msg, schema, inst): | |
print "\n\n\n\n##### {0} #####".format(msg) | |
validator = Draft4Validator(schema, format_checker=FormatChecker()) | |
print json.dumps(inst) | |
lastidx = 0 | |
for idx, err in enumerate(validator.iter_errors(inst), 1): | |
lastidx = idx | |
if idx == 1: | |
print "\tSCHEMA ERRORS:" | |
print "\t{0}. {1}\n".format(idx, pprint.pformat(err)) | |
if lastidx == 0: | |
print "\tVALIDATED SUCCESS" | |
valid("schema uses 'items' : Good Data", working_good_schema, good_data) | |
valid("schema uses 'items' : Bad Data = Should Fail", working_good_schema, bad_data) | |
valid("schema uses 'additionalItems' : Good Data", nonworking_good_schema, good_data) | |
# import pdb; pdb.set_trace() | |
valid("schema uses 'additionalItems' : Bad Data = Should Fail", nonworking_good_schema, bad_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment