Aliases
plateforme.core.schema.aliases
This module provides utilities for managing schema aliases within the Plateforme framework using Pydantic features.
AliasPath
dataclass
Bases: AliasPath
A data class to specify a path to a model field using aliases.
This class is used in models and resources to specify a path to a field using aliases. It is typically used for indexing into nested data structures like lists or dictionaries within JSON payloads.
Attributes:
Name | Type | Description |
---|---|---|
path |
list[int | str]
|
A list representing the path to the target field using string keys for dictionary access or integer indices for list access. |
Examples:
>>> from plateforme import BaseModel, Field
>>> class User(BaseModel):
... first_name: str = Field(validation_alias=AliasPath('names', 0))
... last_name: str = Field(validation_alias=AliasPath('names', 1))
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
convert_to_aliases
convert_to_alias
convert_to_alias() -> str
Converts arguments to an alias string.
It joins the path elements with underscores to create a single string representing the alias path.
Returns:
Type | Description |
---|---|
str
|
The alias path as an alias string. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
AliasChoices
dataclass
Bases: AliasChoices
A data class to specify a list of possible aliases for a model field.
This class is used in models and resources to specify one or multiple possible aliases for a field, allowing the model to accept different payload structures with variable field names but identical meanings.
Attributes:
Name | Type | Description |
---|---|---|
choices |
list[str | AliasPath]
|
A list of strings representing the alternative names for the
model field. It can also include |
Examples:
>>> from plateforme import BaseModel, Field
>>> class User(BaseModel):
... first_name: str = Field(
... validation_alias=AliasChoices('first_name', 'fname'))
... last_name: str = Field(
... validation_alias=AliasChoices('last_name', 'lname'))
>>> user_data = {'fname': 'John', 'lname': 'Doe'}
>>> user = User.model_validate(user_data)
>>> print(user)
first_name='John' last_name='Doe'
>>> user_data_alt = {'first_name': 'John', 'lname': 'Doe'}
>>> user = User.model_validate(user_data_alt)
>>> print(user)
first_name='John' last_name='Doe'
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
convert_to_aliases
Converts arguments to a list of lists containing string or integer aliases.
Returns:
Type | Description |
---|---|
list[list[str | int]]
|
The list of aliases. |
Source code in .venv/lib/python3.12/site-packages/pydantic/aliases.py
convert_to_alias
convert_to_alias() -> str
Converts arguments to an alias string.
It returns the first choice as an alias string. If the first choice
is an AliasPath
, it converts it to an alias string.
Returns:
Type | Description |
---|---|
str
|
The first choice as an alias string. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/aliases.py
AliasGenerator
dataclass
AliasGenerator(
alias: Callable[[str], str] | None = None,
validation_alias: Callable[
[str], str | AliasPath | AliasChoices
]
| None = None,
serialization_alias: Callable[[str], str] | None = None,
)
Bases: AliasGenerator
A data class used to specify generators to create various aliases.
It is a class that allows to specify multiple alias generators for a models
and resources. It can be used to specify different alias generators for
validation
and serialization
.
This is particularly useful in scenarios where it is necessary to generate different naming conventions for loading and saving data, but it is not desired to specify the validation and serialization aliases for each field individually.
Attributes:
Name | Type | Description |
---|---|---|
alias |
Callable[[str], str] | None
|
A callable that takes a field name and returns an alias for it. |
validation_alias |
Callable[[str], str | AliasPath | AliasChoices] | None
|
A callable that takes a field name and returns a validation alias for it. |
serialization_alias |
Callable[[str], str] | None
|
A callable that takes a field name and returns a serialization alias for it. |
generate_aliases
generate_aliases(
field_name: str,
) -> tuple[
str | None,
str | AliasPath | AliasChoices | None,
str | None,
]
Generate aliases for a field.
It generates alias
, validation_alias
, and serialization_alias
for a field using the class generators.
Returns:
Type | Description |
---|---|
tuple[str | None, str | AliasPath | AliasChoices | None, str | None]
|
A tuple of three aliases - validation, alias, and serialization. |