Last active
March 29, 2018 11:45
-
-
Save ripiuk/73e393ff19b13030cf0c29d09f26a397 to your computer and use it in GitHub Desktop.
A simple dictionary structure validation
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
import trafaret as t | |
from trafaret import Dict | |
some_template = { | |
'api': { | |
'status': 'ok', | |
'locale': 'Some str', | |
'error': 'None', | |
'data': { | |
'result': { | |
'hotels': {} | |
}, | |
'filters': [], | |
'hotel_count': 1, | |
'environment': {}, | |
'request': {}, | |
'result_count': 2 | |
}, | |
} | |
} | |
validation = Dict({ | |
t.Key('api'): Dict({ | |
t.Key('status'): t.Atom('ok'), | |
t.Key('locale'): t.String, | |
t.Key('error'): t.Atom('None'), | |
t.Key('data'): Dict({ | |
t.Key('result'): Dict({ | |
t.Key('hotels'): t.Dict().allow_extra('*') | |
}).allow_extra('*') | |
}).allow_extra('*') | |
}).allow_extra('*') | |
}).allow_extra('*') | |
try: | |
validation.check(some_template) | |
except t.DataError as error: | |
print('Validation error occurred: {err_msg}'.format(err_msg=error)) | |
else: | |
print('Validation was successfully completed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment