-
Julian/jsonschema: An(other) implementation of JSON Schema for Python
-
jsonschemais an implementation of JSON Schema for Python (supporting 2.7+ including Python 3). -
It can also be used from console:
$ jsonschema -i sample.json sample.schema
Features
-
Full support for Draft 7, Draft 6, Draft 4 and Draft 3
-
LAZY VALIDATION that can iteratively report all validation errors.
不用走過整份文件才能回報第一個錯誤? 還是預設只會回報第一個錯誤 ??
-
Programmatic querying of which properties or items failed validation.
-
-
Julian/jsonschema: An(other) implementation of JSON Schema for Python
>>> from jsonschema import validate >>> # A sample schema, like what we'd get from json.load() >>> schema = { ... "type" : "object", ... "properties" : { ... "price" : {"type" : "number"}, ... "name" : {"type" : "string"}, ... }, ... } >>> # If no exception is raised by validate(), the instance is valid. >>> validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema) >>> validate( ... instance={"name" : "Eggs", "price" : "Invalid"}, schema=schema, ... ) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... ValidationError: 'Invalid' is not of type 'number'用法很單純,
jsonschema.validate(instance, schema)沒有丟出ValidationError就是通過檢查;其中instance(JSON document) 及schema都跟json.load()的結果一樣。不過以這個例子而言,
ValidationError沒有指出是price的資料有問題,大一點的 JSON document 會不會很難找出問題 ?? -
The Basics - Schema Validation — jsonschema 3.0.1 documentation #ril
- The simplest way to validate an instance under a given schema is to use the
validate()function.
- The simplest way to validate an instance under a given schema is to use the
-
Validate data easily with JSON Schema « Python recipes « ActiveState Code #ril
-
How to JSON schema validate 10x (or 100x) faster in Python - Peterbe.com (2018-11-04) #ril
- 用 pip 安裝
jsonschema套件,支援 Python 2.7+ 與 Python 3。
參考資料:
-
Julian/jsonschema: An(other) implementation of JSON Schema for Python
jsonschemais available on PyPI. You can install using pip:$ pip install jsonschema -
jsonschema/setup.cfg at v3.0.1 · Julian/jsonschema 相依性不少?
install_requires = attrs>=17.4.0 pyrsistent>=0.14.0 setuptools six>=1.11.0 functools32;python_version<'3' [options.extras_require] format = idna jsonpointer>1.13 rfc3987 strict-rfc3339 webcolorsSchema Validation — jsonschema 3.0.1 documentation 提到
pip install jsonschema[format],跟部份 format 的檢查有關。
手冊: