CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-jsonschema

An implementation of JSON Schema validation for Python

Pending
Overview
Eval results
Files

core-validation.mddocs/

Core Validation

Essential validation functionality providing the main entry points for JSON Schema validation. These functions offer the most convenient ways to validate data against schemas.

Capabilities

Main Validation Function

The primary validation function that automatically selects the appropriate validator and validates data against a schema.

def validate(instance, schema, cls=None, *args, **kwargs):
    """
    Validate an instance under the given schema.
    
    Parameters:
    - instance: The data to validate
    - schema: The JSON schema to validate against
    - cls: Validator class to use (optional, auto-detected from schema)
    - format_checker: FormatChecker instance for format validation
    - registry: Schema registry for reference resolution
    
    Raises:
    - ValidationError: If the instance is invalid
    - SchemaError: If the schema itself is invalid
    """

Usage example:

from jsonschema import validate, ValidationError

schema = {"type": "string", "minLength": 5}

try:
    validate("hello", schema)  # Valid
    validate("hi", schema)     # Raises ValidationError
except ValidationError as e:
    print(f"Validation failed: {e.message}")

Validator Selection

Automatically determine the appropriate validator class for a given schema based on its $schema keyword.

def validator_for(schema, default=_UNSET):
    """
    Retrieve the validator class appropriate for validating the given schema.
    
    Parameters:
    - schema: The schema to analyze
    - default: Default validator class if $schema not found
    
    Returns:
    - Validator class appropriate for the schema
    
    Raises:
    - SchemaError: If no appropriate validator can be determined
    """

Usage example:

from jsonschema import validator_for

# Schema with explicit $schema
schema = {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {"name": {"type": "string"}}
}

ValidatorClass = validator_for(schema)
validator = ValidatorClass(schema)

# Use the validator
if validator.is_valid({"name": "John"}):
    print("Valid!")

Quick Validation Check

Check if data is valid without raising exceptions:

from jsonschema import Draft202012Validator

validator = Draft202012Validator(schema)
is_valid = validator.is_valid(data)  # Returns boolean

Detailed Error Information

Get all validation errors for comprehensive feedback:

from jsonschema import Draft202012Validator

validator = Draft202012Validator(schema)
errors = list(validator.iter_errors(data))

for error in errors:
    print(f"Error at {'.'.join(str(p) for p in error.path)}: {error.message}")

Install with Tessl CLI

npx tessl i tessl/pypi-jsonschema

docs

core-validation.md

error-handling.md

format-validation.md

index.md

type-checking.md

validator-creation.md

validators.md

tile.json