Skip to content

Instantly share code, notes, and snippets.

@ripiuk
Last active March 29, 2018 11:45
Show Gist options
  • Save ripiuk/73e393ff19b13030cf0c29d09f26a397 to your computer and use it in GitHub Desktop.
Save ripiuk/73e393ff19b13030cf0c29d09f26a397 to your computer and use it in GitHub Desktop.
A simple dictionary structure validation
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