Field Alias
Alias
Field aliases can be defined explicitly in the model definition in the same way as in regular pydantic.
Terms
Any defined terms are still respected.
from pydantic_jsonld.compact import BaseModel, Field, Value
class ExampleModel(BaseModel):
value: Value[int] = Field(..., alias="myValue")
data = {
"@context": {"@vocab": "http://www.example.org/"},
"myValue": 1,
}
model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped == data
from pydantic_jsonld.compact import BaseModel, Field, Value
class ExampleModel(BaseModel):
value: Value[int] = Field(..., term="ex", alias="myValue")
data = {
"@context": {"ex": "http://www.example.org/"},
"ex:myValue": 1,
}
model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped == data
Tip
Validation and serialization aliases are set to {term}:{alias}
if a term is provided.
Validation / Serialization Alias
Similarly, validation as well as serialization aliases can be defined explicitly in the model definition in the same way as in regular pydantic.
Terms
This causes any defined terms to be ignored and may prevent valid JSON-LD data from being parsed correctly.
from pydantic_jsonld.compact import BaseModel, Field, Value
class ExampleModel(BaseModel):
value: Value[int] = Field(..., term="ex", validation_alias="myValue")
# Not a valid JSON-LD
data = {
"@context": {"ex": "http://www.example.org/"},
"myValue": 1,
}
# The defined term is ignored
ExampleModel.model_validate(data)
from pydantic_jsonld.compact import BaseModel, Field, Value
class ExampleModel(BaseModel):
value: Value[int] = Field(..., serialization_alias="myValue")
model = ExampleModel(
context={"@vocab": "http://www.example.org/"},
value=1,
)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped["myValue"] == 1
from pydantic_jsonld.compact import BaseModel, Field, Value
class ExampleModel(BaseModel):
value: Value[int] = Field(..., term="ex", serialization_alias="myValue")
model = ExampleModel(
context={"ex": "http://www.example.org/"},
value=1,
)
# Not a valid JSON-LD
dumped = model.model_dump(by_alias=True, exclude_none=True)
# The defined term is ignored
assert dumped["myValue"] == 1
Alias Generator
By default BaseModel uses the to_camel alias generator from pydantic.
Different alias generators can be defined in the same way as in pydantic.
Context Alias
The only exception to this is the context property which always has alias @context
regardless of the chosen alias generator.
from pydantic import ConfigDict
from pydantic_jsonld.compact import BaseModel, Value
class ExampleModel(BaseModel):
model_config = ConfigDict(alias_generator=None)
my_value: Value[int]
data = {
"@context": {"@vocab": "http://www.example.org/"},
"my_value": 1,
}
model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped == data
from pydantic import ConfigDict
from pydantic.alias_generators import to_pascal
from pydantic_jsonld.compact import BaseModel, Value
class ExampleModel(BaseModel):
model_config = ConfigDict(alias_generator=to_pascal)
my_value: Value[int]
data = {
"@context": {"@vocab": "http://www.example.org/"},
"MyValue": 1,
}
model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped == data
from pydantic import AliasGenerator, ConfigDict
from pydantic.alias_generators import to_camel, to_pascal
from pydantic_jsonld.compact import BaseModel, Value
class ExampleModel(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(
validation_alias=to_camel,
serialization_alias=to_pascal,
)
)
my_value: Value[int]
data = {
"@context": {"@vocab": "http://www.example.org/"},
"myValue": 1,
}
model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)
assert dumped["MyValue"] == data["myValue"]
Terms
Unlike the case when setting explicit validation and serialization aliases, for the last example, terms are not ignored.