SchemaConfig class¶
Here's the reference information for the SchemaConfig class, with all its
parameters, attributes and methods.
You can import the SchemaConfig class directly from pyrmute:
pyrmute.SchemaConfig
dataclass
¶
SchemaConfig(
schema_generator=None,
mode="validation",
by_alias=True,
ref_template="#/$defs/{model}",
extra_kwargs=dict(),
)
Configuration for JSON schema generation.
This class provides fine-grained control over how Pydantic generates JSON schemas, supporting both callable generators and Pydantic's GenerateJsonSchema classes.
| ATTRIBUTE | DESCRIPTION |
|---|---|
schema_generator |
Custom schema generator. Can be either: - A callable taking (type[BaseModel]) -> JsonSchema - A subclass of pydantic.json_schema.GenerateJsonSchema
TYPE:
|
mode |
Schema generation mode - 'validation' for input validation or 'serialization' for output serialization.
TYPE:
|
by_alias |
Whether to use field aliases in the schema.
TYPE:
|
ref_template |
Template for JSON schema $ref URIs.
TYPE:
|
extra_kwargs |
Additional arguments to pass to model_json_schema().
TYPE:
|
Example
GenerateJsonSchema Class:
from pydantic.json_schema import GenerateJsonSchema
class CustomSchemaGenerator(GenerateJsonSchema):
def generate(
self,
schema: Mapping[str, Any],
mode: JsonSchemaMode = "validation"
) -> JsonSchema:
json_schema = super().generate(schema, mode=mode)
json_schema["x-custom"] = "metadata"
return json_schema
config = SchemaConfig(
schema_generator=CustomSchemaGenerator,
mode="validation",
by_alias=True
)
Callable Generator:
merge_with
¶
Merge this config with another, with other taking precedence.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
Configuration to merge with (overrides this config).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SchemaConfig
|
New SchemaConfig with merged values. |
Source code in src/pyrmute/schema_config.py
to_kwargs
¶
Convert config to kwargs for model_json_schema().
Note: If schema_generator is a callable (JsonSchemaGenerator type), it cannot be passed to model_json_schema() and must be handled separately by calling it directly.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Dictionary of arguments for Pydantic's model_json_schema(). If |
dict[str, Any]
|
schema_generator is a callable, it will NOT be included. |
Source code in src/pyrmute/schema_config.py
is_callable_generator
¶
Check if schema_generator is a callable function.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if schema_generator is a callable (not a class). |