hololinked.injection.SchemaValidators
Bases: Registry
A singleton registry that decides which schema validator class validates against a given schema.
All members are class attributes and settings are applied process-wide (python process).
Which validator handles a schema is decided by the class of the payload type.
Usually pydantic models invoke a pydantic validator and dictionaries invoke a JSON schema validator,
but the mapping can be changed by registering a new validator. Use register() with a predicate function to
activate a custom validator.
from hololinked import SchemaValidators
SchemaValidators.register_lazy(
"my_package.validators",
"MsgspecValidator",
"msgspec",
predicate=lambda schema: issubklass(schema, msgspec.Struct),
)
class MyThing(Thing):
@action(input_schema=MyStruct) # validated by MsgspecValidator
def act(self, value): ...
A validator registered later is selected for a schema in preference to one registered earlier, so the built-in JSON schema and pydantic validators can be superseded for schemas they would otherwise handle.
Source code in repo/hololinked/hololinked/injection.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
Attributes
json_schema
instance-attribute
JSON Schema validator class that can be instantiated with a JSON schema to validate data against that schema.
pydantic
instance-attribute
Pydantic validator class that can be instantiated with a Pydantic model to validate data against the schema defined by that model.
modules
class-attribute
modules: dict[str, str | tuple[str, str]] = {'json_schema': 'JSONSchemaValidator', 'pydantic': 'PydanticSchemaValidator'}
predicates
class-attribute
predicates: dict[str, Callable[[Any], bool]] = {'json_schema': lambda schema: isinstance(schema, dict), 'pydantic': lambda schema: issubklass(schema, BaseModel)}
Name of a validator mapped to a predicate deciding whether it can validate against a given schema.
Insertion ordered and consulted in reverse, so a validator registered later is selected for a schema in preference to one registered earlier. Predicates must be answerable without importing the adapter they belong to, since they are what decides which adapter to import in the first place.
Functions
for_schema
classmethod
Get the validator class that validates against the given schema, importing it if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
the schema to find a validator for |
required |
Returns:
| Type | Description |
|---|---|
type[BaseSchemaValidator]
|
the validator class, to be instantiated with the schema |
Raises:
| Type | Description |
|---|---|
TypeError
|
if no registered validator matches the schema |
Source code in repo/hololinked/hololinked/injection.py
name_for_schema
classmethod
Get the name of the validator selected for the given schema.
print(SchemaValidators.name_for_schema({"type": "string"}))
# prints 'json_schema'
print(SchemaValidators.name_for_schema(MyPydanticModel))
# prints 'pydantic'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
the schema to find a validator for |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
the name the selected validator is registered under, None if no registered validator matches the schema |
Source code in repo/hololinked/hololinked/injection.py
is_supported
classmethod
Check whether any registered validator can validate against the given schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
the schema to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a registered validator matches the schema |
Source code in repo/hololinked/hololinked/injection.py
check_schema
classmethod
Check that the given object is a well formed schema, using whichever validator is selected for it.
Does nothing if the selected validator has no notion of checking a schema, as is the case for pydantic models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
the schema to check |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
if no registered validator matches the schema |
Exception
|
whatever the selected validator raises for a malformed schema |
Source code in repo/hololinked/hololinked/injection.py
register
classmethod
Register a schema validator class under a given name, overriding any validator already using that name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[BaseSchemaValidator]
|
the validator class to register, must be a subclass of |
required |
|
str
|
the name to register the validator under, for example 'json_schema' or 'pydantic' |
required |
|
Callable[[Any], bool]
|
predicate returning whether this validator can validate against a given schema, which is how a property or action picks a validator for the schema it was declared with. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
if the validator is not a subclass of |