[Python] JSON validation using Voluptuous

How to perform JSON value validation using the Voluptuous library in Python.

What is Voluptuous?

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

Python 2.6 and above

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())

Details

Voluptuous term

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.

Validation method

One of the following

  1. Direct specification of possible values. It is also possible to have a width with Any, Range, etc.
  2. Class (Int, float, etc., six.text_type can be specified) --Any value is OK as long as the classes match
  3. Specify the validation function --This is the validate_io_size function mentioned above --The validation function throws an Invalid exception if the condition is not met

Operation example

environment

Normal operation example

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'}
#

Example of normal operation (required parameters omitted)

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.

Error operation example

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.

Error operation example (when validation function is specified)

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

[Python] JSON validation using Voluptuous
Python #JSON
Validate JSON objects using Python DictShield
Start using Python
Scraping using Python
Operate Redmine using Python Redmine
[Python] Use JSON with Python
Fibonacci sequence using Python
Handling json in python
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
Install Voluptuous with Python 2.5
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Python notes using perl-ternary operator
Flatten using Python yield from
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
POST json with Python3 script
Using Quaternion with Python ~ numpy-quaternion ~
Try using Kubernetes Client -Python-
Python notes using perl-special variables
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Disable SSL validation without using verify = False in Python requests
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Python: Jwa / Jwe ECDH-ES validation
Web scraping using Selenium (Python)
[Python] I tried using OpenPose
Broadcast on LINE using python
Format json with Vim (with python)
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail
Read json data with python
Complement python with emacs using company-jedi
How to install python using anaconda
Harmonic mean with Python Harmonic mean (using SciPy)
[Python] Loading csv files using pandas
GUI programming in Python using Appjar
Retry post request using python requests
Python Note: About comparison using is
[Ubuntu] [Python] Object tracking using dlib
Image capture of firefox using python