Created
September 3, 2015 04:07
-
-
Save mmulich/40bf33cdb81801659269 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Rough sketch of using hypothesis to make jsonschema schemas | |
from hypothesis.strategies import ( | |
composite, dictionaries, fixed_dictionaries, sampled_from, text | |
) | |
JSON_SCHEMA_PRIMITIVES = ('array', 'boolean', 'integer', 'number', 'null', | |
'object', 'string') | |
MAX_SCHEMA_DEPTH = 2 | |
@composite | |
def make_props_strategy( | |
draw, | |
key_st=text(min_size=2, max_size=5), | |
value_st=fixed_dictionaries({'type': sampled_from(JSON_SCHEMA_PRIMITIVES)}), | |
depth=0): | |
props = draw(dictionaries(key_st, value_st, max_size=4)) | |
for key, prop in props.items(): | |
if prop['type'] == 'object': | |
if depth > MAX_SCHEMA_DEPTH: | |
del props[key] | |
continue | |
prop['properties'] = draw(make_props_strategy(key_st, value_st, depth+1)) | |
return props | |
@composite | |
def make_jsonschema(draw): | |
node = { | |
'type': 'object', | |
'properties': draw(make_props_strategy()), | |
} | |
return node | |
from hypothesis import find | |
from pprint import pprint | |
pprint(make_jsonschema().filter(lambda x: True in [z['type'] == 'object' for y,z in x['properties'].items()]).example()) |
This file contains hidden or 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
{'properties': {u'\x05\u0120\x98\u019b\x9c': {'type': 'array'}, | |
u'\x98\u019b': {'properties': {u'\x08\x03': {'type': 'number'}, | |
u'\x08\x11\x86': {'properties': {u'\x11\ucb4f': {'type': 'integer'}, | |
u'D\x85': {'type': 'integer'}, | |
u'\xbcP': {'type': 'integer'}, | |
u'\u0133\x85': {'type': 'integer'}}, | |
'type': 'object'}, | |
u'\x08\x9e': {'type': 'null'}}, | |
'type': 'object'}, | |
u'\u0120\t\u019b\x13\x9c': {'type': 'array'}, | |
u'\u0120\x98\x13\t\x13': {'type': 'integer'}}, | |
'type': 'object'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment