Skip to content

FastJSONSchemaValidator

Unlike JSONSchemaValidator and PydanticSchemaValidator, this one is not registered under a name in SchemaValidators, as a default JSON schema validator is available. To use FastJSONSchemaValidator:

from hololinked import SchemaValidators
from hololinked.schema_validators import FastJSONSchemaValidator

SchemaValidators.register(FastJSONSchemaValidator, "json_schema", predicate=lambda schema: isinstance(schema, dict))

It needs the validators extra dependencies (pip install hololinked[validators]).

hololinked.schema_validators.fast_json_schema.FastJSONSchemaValidator

Bases: BaseSchemaValidator

JSON schema validator according to fast JSON schema.

pip install fastjsonschema to use.

power_supply_output_schema = {
    "type": "object",
    "properties": {
        "current": {"type": "number", "minimum": 0},
        "power": {"type": "number", "minimum": 0, "maximum": 100},
    },
}
validator = FastJSONSchemaValidator(power_supply_output_schema)
validator.validate({"current": 50, "power": 75})  # valid
validator.validate({"current": 65, "power": 110})  # raises

Useful for performance with dictionary based schema specification, which msgspec has no built in support for. Normally, for speed, one should try to use msgspec's struct concept.

Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
class FastJSONSchemaValidator(BaseSchemaValidator):
    """
    JSON schema validator according to fast JSON schema.

    `pip install fastjsonschema` to use.

    ```python
    power_supply_output_schema = {
        "type": "object",
        "properties": {
            "current": {"type": "number", "minimum": 0},
            "power": {"type": "number", "minimum": 0, "maximum": 100},
        },
    }
    validator = FastJSONSchemaValidator(power_supply_output_schema)
    validator.validate({"current": 50, "power": 75})  # valid
    validator.validate({"current": 65, "power": 110})  # raises
    ```

    Useful for performance with dictionary based schema specification, which msgspec has no built in support for.
    Normally, for speed, one should try to use msgspec's struct concept.
    """

    def __init__(self, schema: JSONSchemaType) -> None:
        """
        Initialize the validator.

        Parameters
        ----------
        schema: JSONSchemaType
            The JSON schema to validate against
        """
        super().__init__(schema)
        self.validator = fastjsonschema.compile(schema)

    @classmethod
    def check_schema(cls, schema: JSONSchemaType) -> None:
        """
        Check that the given object is itself a valid JSON schema.

        `fastjsonschema` has no dedicated schema checker, so the schema is compiled and the result discarded.

        Parameters
        ----------
        schema: JSONSchemaType
            the schema to check

        Raises
        ------
        fastjsonschema.JsonSchemaDefinitionException
            if the schema is not a valid JSON schema
        """
        fastjsonschema.compile(schema)

    def validate(self, data) -> None:  # noqa: D102
        self.validator(data)

    def validate_method_call(self, args, kwargs) -> None:  # noqa: D102
        if len(args) > 0:
            kwargs = json_schema_merge_args_to_kwargs(self.schema, args, kwargs)
            # TODO fix type definition
        self.validate(kwargs)

    def json(self) -> JSONSchemaType:  # noqa: D102
        return self.schema

    def __get_state__(self) -> JSONSchemaType:
        return self.schema

    def __set_state__(self, schema: JSONSchemaType):
        return FastJSONSchemaValidator(schema)

Attributes

schema instance-attribute

schema = schema

validator instance-attribute

validator = compile(schema)

Functions

__init__

__init__(schema: JSONSchemaType) -> None

Initialize the validator.

Parameters:

Name Type Description Default
schema
JSONSchemaType

The JSON schema to validate against

required
Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
def __init__(self, schema: JSONSchemaType) -> None:
    """
    Initialize the validator.

    Parameters
    ----------
    schema: JSONSchemaType
        The JSON schema to validate against
    """
    super().__init__(schema)
    self.validator = fastjsonschema.compile(schema)

json

json() -> JSONSchemaType
Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
def json(self) -> JSONSchemaType:  # noqa: D102
    return self.schema

validate

validate(data) -> None
Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
def validate(self, data) -> None:  # noqa: D102
    self.validator(data)

validate_method_call

validate_method_call(args, kwargs) -> None
Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
def validate_method_call(self, args, kwargs) -> None:  # noqa: D102
    if len(args) > 0:
        kwargs = json_schema_merge_args_to_kwargs(self.schema, args, kwargs)
        # TODO fix type definition
    self.validate(kwargs)

check_schema classmethod

check_schema(schema: JSONSchemaType) -> None

Check that the given object is itself a valid JSON schema.

fastjsonschema has no dedicated schema checker, so the schema is compiled and the result discarded.

Parameters:

Name Type Description Default
schema
JSONSchemaType

the schema to check

required

Raises:

Type Description
JsonSchemaDefinitionException

if the schema is not a valid JSON schema

Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
@classmethod
def check_schema(cls, schema: JSONSchemaType) -> None:
    """
    Check that the given object is itself a valid JSON schema.

    `fastjsonschema` has no dedicated schema checker, so the schema is compiled and the result discarded.

    Parameters
    ----------
    schema: JSONSchemaType
        the schema to check

    Raises
    ------
    fastjsonschema.JsonSchemaDefinitionException
        if the schema is not a valid JSON schema
    """
    fastjsonschema.compile(schema)