Bases: BaseSchemaValidator
JSON schema validator extending the standard python JSON schema package.
power_supply_output_schema = {
"type": "object",
"properties": {
"current": {"type": "number", "minimum": 0},
"power": {"type": "number", "minimum": 0, "maximum": 100},
},
}
validator = JSONSchemaValidator(power_supply_output_schema)
validator.validate({"current": 50, "power": 75}) # valid
validator.validate({"current": 65, "power": 110}) # raises
This class is largely used internally and there is no need to explicitly instantiate it.
Consider FastJSONSchemaValidator (pip install fastjsonschema) or
pydantic annotation based validation for performance if necessary.
Source code in repo/hololinked/hololinked/schema_validators/json_schema.py
| class JSONSchemaValidator(BaseSchemaValidator):
"""
JSON schema validator extending the standard python JSON schema package.
```python
power_supply_output_schema = {
"type": "object",
"properties": {
"current": {"type": "number", "minimum": 0},
"power": {"type": "number", "minimum": 0, "maximum": 100},
},
}
validator = JSONSchemaValidator(power_supply_output_schema)
validator.validate({"current": 50, "power": 75}) # valid
validator.validate({"current": 65, "power": 110}) # raises
```
This class is largely used internally and there is no need to explicitly instantiate it.
Consider `FastJSONSchemaValidator` (`pip install fastjsonschema`) or
pydantic annotation based validation for performance if necessary.
"""
def __init__(self, schema: JSONSchemaType) -> None:
"""
Initialize the validator.
Parameters
----------
schema: JSONSchemaType
The JSON schema to validate against
"""
self.check_schema(schema)
super().__init__(schema)
self.validator = jsonschema.Draft7Validator(schema)
@classmethod
def check_schema(cls, schema: JSONSchemaType) -> None:
"""
Check that the given object is itself a valid JSON schema.
Parameters
----------
schema: JSONSchemaType
the schema to check
Raises
------
jsonschema.SchemaError
if the schema is not a valid JSON schema
"""
jsonschema.Draft7Validator.check_schema(schema)
def validate(self, data) -> None: # noqa: D102
self.validator.validate(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 JSONSchemaValidator(schema)
|
Attributes
schema
instance-attribute
validator
instance-attribute
validator = Draft7Validator(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/json_schema.py
| def __init__(self, schema: JSONSchemaType) -> None:
"""
Initialize the validator.
Parameters
----------
schema: JSONSchemaType
The JSON schema to validate against
"""
self.check_schema(schema)
super().__init__(schema)
self.validator = jsonschema.Draft7Validator(schema)
|
json
Source code in repo/hololinked/hololinked/schema_validators/json_schema.py
| def json(self) -> JSONSchemaType: # noqa: D102
return self.schema
|
validate
Source code in repo/hololinked/hololinked/schema_validators/json_schema.py
| def validate(self, data) -> None: # noqa: D102
self.validator.validate(data)
|
validate_method_call
validate_method_call(args, kwargs) -> None
Source code in repo/hololinked/hololinked/schema_validators/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.
Parameters:
| Name |
Type |
Description |
Default |
schema
|
JSONSchemaType
|
|
required
|
Raises:
| Type |
Description |
SchemaError
|
if the schema is not a valid JSON schema
|
Source code in repo/hololinked/hololinked/schema_validators/json_schema.py
| @classmethod
def check_schema(cls, schema: JSONSchemaType) -> None:
"""
Check that the given object is itself a valid JSON schema.
Parameters
----------
schema: JSONSchemaType
the schema to check
Raises
------
jsonschema.SchemaError
if the schema is not a valid JSON schema
"""
jsonschema.Draft7Validator.check_schema(schema)
|