Skip to main content

A thin wrapper around the AJV JSON Validator for Python

Project description

Python CI PyPI version License CodeQL

AJVPY2 - Another JSON Schema Validator for Python

This project is a fork of ajvpy with the following changes:

  1. Replaced PyV8 with PyMiniRacer.
  2. Added support for Python 3.12.

Installation

  1. Install the package from PyPI:

    pip install ajvpy2
    
  2. (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


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)

Uploaded Source

Built Distribution

ajvpy2-0.1.2-py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page