How to perform JSON value validation using the Voluptuous library in Python.
Validation library such as JSON of Python.
Official repository: https://github.com/alecthomas/voluptuous
Voluptuous, despite the name, is a Python data validation library. It is primarily intended for validating data coming into Python as JSON, YAML, etc.
As mentioned in the description in the repository, it's a great name.
voluptuous
1 Indulge in lust, drown in liquor, sensual.
2 Sensual, sexy, adorable, sexy.
3 <Feeling, etc.> Comfortable.
install
Can be installed with pip.
# pip install voluptuous
Python 2.5 Although it is not officially supported, it can be installed and operated at this article level with Install Voluptuous with Python2.5.
Sample program A sample program that shows the operation. Where the Schema declaration in get_schema is at the core. This is the definition of JSON schema in Voluptuous.
from __future__ import with_statement
import re
import sys
import json # Python 2.5 is simplejson
import six
from voluptuous import Schema, Required, Any, Range, Invalid, ALLOW_EXTRA
def validate_io_size(v):
if re.search("^[0-9]+[bkm]", v) is None:
raise Invalid("not a valid value (%s)" % str(v))
def get_schema():
schema = Schema({
"comment" : six.text_type,
Required("operation") : Any("read", "write"),
"thread" : Range(min=1),
Required("io_size") : validate_io_size,
Required("access_percentage") : Range(min=1, max=100),
}, extra=ALLOW_EXTRA)
return schema
def main():
schema = get_schema()
with open(sys.argv[1], "r") as fp:
dict_sample = json.load(fp)
schema(dict_sample)
six.print_(dict_sample)
return 0
if __name__ == '__main__':
sys.exit(main())
Keywords | meaning |
---|---|
Required | Required parameter. No error will occur even if parameters without this are omitted. |
Any | Any value in the declaration is OK. |
Range | min or more and max or less is OK. You can also specify only min or max. |
ALLOW_EXTRA | Allows the existence of undefined parameters in the schema. |
One of the following
valid1.json
{
"comment" : "comment",
"operation" : "write",
"thread" : 8,
"io_size" : "8k",
"access_percentage" : 100
}
# python voluptuous_sample.py valid1.json
{u'comment': u'comment', u'operation': u'write', u'access_percentage': 100, u'thread': 8, u'io_size': u'8k'}
#
valid2.json
{
"operation" : "write",
"io_size" : "8k",
"access_percentage" : 100
}
# python voluptuous_sample.py valid2.json
{u'operation': u'write', u'access_percentage': 100, u'io_size': u'8k'}
No error occurs even if there are no required parameters.
invalid.json
{
"comment" : "comment",
"operation" : "<invalid value>",
"thread" : 8,
"io_size" : "8k",
"access_percentage" : 100
}
# python voluptuous_sample.py invalid.json
Traceback (most recent call last):
File "voluptuous_sample.py", line 36, in <module>
sys.exit(main())
File "voluptuous_sample.py", line 29, in main
schema(dict_sample)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 337, in __call__
return self._compiled([], data)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 635, in validate_dict
return base_validate(path, iteritems(data), out)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 471, in validate_mapping
raise MultipleInvalid(errors)
voluptuous.MultipleInvalid: not a valid value for dictionary value @ data[u'operation']
#
If there is a parameter that does not match the schema, voluptuous.MultipleInvalid will be sent and I get a message that the value of the parameter (operation in the example) is incorrect.
invalid2.json
{
"comment" : "comment",
"operation" : "write",
"thread" : 8,
"io_size" : "a8k",
"access_percentage" : 100
}
# python voluptuous_sample.py invalid2.json
Traceback (most recent call last):
File "voluptuous_sample.py", line 38, in <module>
sys.exit(main())
File "voluptuous_sample.py", line 31, in main
schema(dict_sample)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 337, in __call__
return self._compiled([], data)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 635, in validate_dict
return base_validate(path, iteritems(data), out)
File "/usr/local/lib/python2.7/dist-packages/voluptuous.py", line 471, in validate_mapping
raise MultipleInvalid(errors)
voluptuous.MultipleInvalid: not a valid value (a8k) for dictionary value @ data[u'io_size']
This is also the case when the validation function is specified.
Recommended Posts