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
Attributes
Functions
__init__
Initialize the validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
JSONSchemaType
|
The JSON schema to validate against |
required |
Source code in repo/hololinked/hololinked/schema_validators/fast_json_schema.py
json
validate
validate_method_call
check_schema
classmethod
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 |
|---|---|---|---|
|
JSONSchemaType
|
the schema to check |
required |
Raises:
| Type | Description |
|---|---|
JsonSchemaDefinitionException
|
if the schema is not a valid JSON schema |