Skip to main content

Validators for JSON Structure schemas and instances

Project description

JSON Structure Python SDK

PyPI version Python License: MIT

Python validators for JSON Structure schemas and instances.

JSON Structure is a type-oriented schema language for JSON, designed for defining data structures that can be validated and mapped to programming language types.

Installation

pip install json-structure

Quick Start

Validate a Schema

from json_structure import SchemaValidator

schema = {
    "$schema": "https://json-structure.org/meta/core/v0/#",
    "$id": "https://example.com/person",
    "name": "Person",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "int32"},
        "email": {"type": "string"}
    },
    "required": ["name"]
}

validator = SchemaValidator()
errors = validator.validate(schema)

if errors:
    print("Schema is invalid:")
    for error in errors:
        print(f"  - {error}")
else:
    print("Schema is valid!")

Validate an Instance

from json_structure import InstanceValidator

schema = {
    "$schema": "https://json-structure.org/meta/core/v0/#",
    "$id": "https://example.com/person",
    "name": "Person",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "int32"}
    },
    "required": ["name"]
}

instance = {
    "name": "Alice",
    "age": 30
}

validator = InstanceValidator(schema)
errors = validator.validate_instance(instance)

if errors:
    print("Instance is invalid:")
    for error in errors:
        print(f"  - {error}")
else:
    print("Instance is valid!")

Features

Supported Types

All 34 types from JSON Structure Core v0 are supported:

Primitive Types:

  • string, number, integer, boolean, null
  • int8, uint8, int16, uint16, int32, uint32
  • int64, uint64, int128, uint128 (string-encoded)
  • float8, float, double, decimal
  • date, datetime, time, duration
  • uuid, uri, binary, jsonpointer

Compound Types:

  • object, array, set, map, tuple, choice, any

Extensions

  • Conditional Composition: allOf, anyOf, oneOf, not, if/then/else
  • Validation Addins: minimum, maximum, minLength, maxLength, pattern, etc.
  • Import Extension: $import, $importdefs for schema composition

Command Line Tools

# Validate a schema file
json-structure-check schema.json

# Validate an instance against a schema
json-structure-validate instance.json schema.json

API Reference

SchemaValidator

from json_structure import SchemaValidator

validator = SchemaValidator(
    allow_dollar=False,      # Allow '$' in property names
    allow_import=False,      # Enable $import/$importdefs
    import_map=None,         # Dict mapping URIs to local files
    extended=False,          # Enable extended validation features
    external_schemas=None    # List of schema dicts to sideload (matched by $id)
)

errors = validator.validate(schema_dict, source_text=None)

InstanceValidator

from json_structure import InstanceValidator

validator = InstanceValidator(
    root_schema,             # The JSON Structure schema dict
    allow_import=False,      # Enable $import/$importdefs
    import_map=None,         # Dict mapping URIs to local files
    extended=False,          # Enable extended validation features
    external_schemas=None    # List of schema dicts to sideload (matched by $id)
)

errors = validator.validate_instance(instance)

Sideloading External Schemas

When using $import to reference external schemas, you can provide those schemas directly instead of fetching them from URIs:

from json_structure import InstanceValidator

# External schema that would normally be fetched from https://example.com/address.json
address_schema = {
    "$schema": "https://json-structure.org/meta/core/v0/#",
    "$id": "https://example.com/address.json",
    "name": "Address",
    "type": "object",
    "properties": {
        "street": {"type": "string"},
        "city": {"type": "string"}
    }
}

# Main schema that imports the address schema
main_schema = {
    "$schema": "https://json-structure.org/meta/core/v0/#",
    "$id": "https://example.com/person",
    "name": "Person",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "address": {"type": {"$ref": "#/definitions/Imported/Address"}}
    },
    "definitions": {
        "Imported": {
            "$import": "https://example.com/address.json"
        }
    }
}

# Sideload the address schema - matched by $id
validator = InstanceValidator(
    main_schema,
    allow_import=True,
    external_schemas=[address_schema]
)

instance = {
    "name": "Alice",
    "address": {"street": "123 Main St", "city": "Seattle"}
}

errors = validator.validate_instance(instance)

You can supply multiple schemas to satisfy multiple imports. The schemas are matched by their $id field against the import URIs.

Development

# Clone the repository
git clone https://github.com/json-structure/sdk.git
cd sdk/python

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=json_structure --cov-report=term-missing

License

MIT License - see LICENSE for details.

Links

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

json_structure-0.6.1.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_structure-0.6.1-py3-none-any.whl (45.9 kB view details)

Uploaded Python 3

File details

Details for the file json_structure-0.6.1.tar.gz.

File metadata

  • Download URL: json_structure-0.6.1.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_structure-0.6.1.tar.gz
Algorithm Hash digest
SHA256 ae2f360830e7e52151714739df3a57eb05c4177c57be299ab9ce070a36a55a25
MD5 c9cf5c14cf4aac61d7b544b40201857a
BLAKE2b-256 3d60fb6210d5d00682f6986006d3cfdfe1e50d7d47591213aabc30998a9c2034

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_structure-0.6.1.tar.gz:

Publisher: python.yml on json-structure/sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file json_structure-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: json_structure-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 45.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_structure-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 598b082d748e77c5838d8aa99d6be88927d7a0fe3a823b4b12050b0b0054e988
MD5 f30e73dd4be033ed9453c218969af56d
BLAKE2b-256 17d25277e4be7766eff0a8914060f214dae5258c0e9dbed551e35fc2c6dfe5a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_structure-0.6.1-py3-none-any.whl:

Publisher: python.yml on json-structure/sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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