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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file ajvpy2-0.1.2.tar.gz.

File metadata

  • Download URL: ajvpy2-0.1.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ajvpy2-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8e1b1864564baaf21caca5bf67eaf47e31a6e46c5146769f48dcac646f33b257
MD5 178abbefc407f1265f36b776a410cf92
BLAKE2b-256 3ba0371237a58085574fefe09833b7a58dd85205f1338813e53149f86581b9ef

See more details on using hashes here.

File details

Details for the file ajvpy2-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ajvpy2-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for ajvpy2-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 50cb3e04b1c747d5cb8273efd7dd67e8c176878ab590624c77da11815f26c192
MD5 74680b472413d6730b1ae214148562df
BLAKE2b-256 9ec4eb2492733dca0b828eae79fea95f3d80e8dc50e3d45035827e3d2e98d1fe

See more details on using hashes here.

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