A thin wrapper around the AJV JSON Validator for Python
Project description
AJVPY2 - Another JSON Schema Validator for Python
This project is a fork of ajvpy with the following changes:
- Replaced PyV8 with PyMiniRacer.
- Added support for Python 3.12.
Installation
-
Install the package from PyPI:
pip install ajvpy2
-
(Optional) If you prefer to clone the repository:
git clone https://github.com/yourusername/ajvpy2.git cd ajvpy2 pip install -r requirements.txt
Usage
Basic Example
from ajv import Ajv
schema = {"type": "integer"}
data = 42
ajv = Ajv()
if ajv.validate(schema, data):
print("Valid data!")
else:
print("Invalid data!")
print(ajv.errors)
Adding Custom Formats
ajv.add_format("customFormat", "^[a-z]+$")
schema = {"type": "string", "format": "customFormat"}
data = "abc"
assert ajv.validate(schema, data)
data_invalid = "ABC123"
assert not ajv.validate(schema, data_invalid)
Adding Custom Keywords
keyword_definition = """
{
validate: function(schema, data) {
return schema === data;
},
errors: false
}
"""
ajv.add_keyword("testKeyword", keyword_definition)
schema = {"type": "string", "testKeyword": "test"}
data = "test"
assert ajv.validate(schema, data)
data_invalid = "not_test"
assert not ajv.validate(schema, data_invalid)
Using Plugins
plugin_code = """
function myPlugin(ajv) {
ajv.addKeyword('testKeyword', {
validate: function(schema, data) {
return schema === data;
},
errors: false
});
}
"""
ajv.context.eval(plugin_code)
ajv.plugin("myPlugin")
schema = {"type": "string", "testKeyword": "test"}
data = "test"
assert ajv.validate(schema, data)
Running Tests
To run the tests, use pytest
:
pytest
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ajvpy2-0.1.2.tar.gz
(3.0 kB
view hashes)
Built Distribution
ajvpy2-0.1.2-py3-none-any.whl
(3.1 kB
view hashes)