Skip to content

Field Alias

Alias

Field aliases can be defined explicitly in the model definition in the same way as in regular pydantic.

Prefix

Any defined prefixes are still respected.

from pydantic_jsonld.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    value: Value[int] = Field(..., prefix=example, alias="myValue")


data = [
    {
        "http://www.example.org/myValue": [{"@value": 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 {prefix}{alias} if a prefix 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.

Prefix

This causes any defined prefixes to be ignored and may prevent valid JSON-LD data from being parsed correctly.

from pydantic_jsonld.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    value: Value[int] = Field(..., prefix=example, validation_alias="myValue")


# Not a valid JSON-LD
data = [
    {
        "myValue": [{"@value": 1}],
    }
]

# The defined prefix is ignored
ExampleModel.model_validate(data)
from pydantic_jsonld.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    value: Value[int] = Field(..., prefix=example, serialization_alias="myValue")


model = ExampleModel(
    value=Value[int](value=1),
)
# Not a valid JSON-LD
dumped = model.model_dump(by_alias=True, exclude_none=True) 

# The defined prefix is ignored
assert dumped_2[0]["myValue"] == [{'@value': 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.

from pydantic import ConfigDict
from pydantic_jsonld.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    model_config = ConfigDict(alias_generator=None)        

    my_value: Value[int] = Field(..., prefix=example)

data = [
    {
        "http://www.example.org/my_value": [{"@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.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    model_config = ConfigDict(alias_generator=to_pascal)        

    my_value: Value[int] = Field(..., prefix=example)

data = [
    {
        "http://www.example.org/MyValue": [{"@value": 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.expanded import BaseModel, Field, Value

example = "http://www.example.org/"


class ExampleModel(BaseModel):
    model_config = ConfigDict(
        alias_generator=AliasGenerator(
            validation_alias=to_camel,
            serialization_alias=to_pascal,
        )
    )        

    my_value: Value[int] = Field(..., prefix=example)

data = [
    {
        "http://www.example.org/myValue": [{"@value": 1}],
    }
]

model = ExampleModel.model_validate(data)
dumped = model.model_dump(by_alias=True, exclude_none=True)

assert (
    dumped[0]["http://www.example.org/MyValue"]
    == data[0]["http://www.example.org/myValue"]
)

Prefix

Unlike the case when setting explicit validation and serialization aliases, for the last example, prefixes are not ignored.