Skip to content

Schemas

This page documents the schema classes used in Eunomia Core for data validation and serialization.

Attribute Schemas

eunomia_core.schemas.Attribute

Bases: BaseModel

Source code in pkgs/core/src/eunomia_core/schemas.py
class Attribute(BaseModel):
    key: str = Field(..., description="Attribute key")
    value: str = Field(..., description="Attribute value")

eunomia_core.schemas.AttributeInDb

Bases: Attribute

Source code in pkgs/core/src/eunomia_core/schemas.py
class AttributeInDb(Attribute):
    updated_at: datetime = Field(
        description="Time when this attribute was last updated"
    )
    registered_at: datetime = Field(
        description="Time when this attribute was first registered"
    )

    model_config = ConfigDict(from_attributes=True)

Entity Schemas

eunomia_core.schemas.EntityBase

Bases: BaseModel

Source code in pkgs/core/src/eunomia_core/schemas.py
class EntityBase(BaseModel):
    uri: str = Field(..., description="Unique identifier for the entity")
    attributes: list[Attribute] = Field(..., description="Entity attributes")
    type: EntityType = Field(..., description="Type of entity")

    @field_validator("attributes", mode="before")
    @classmethod
    def from_dict(cls, v: list[Attribute] | dict) -> list[Attribute]:
        if isinstance(v, dict):
            return [Attribute(key=k, value=str(val)) for k, val in v.items()]
        return v

    @field_validator("attributes", mode="after")
    @classmethod
    def unique_attribute_keys(cls, v: list[Attribute]) -> list[Attribute]:
        keys = {}
        for i, attr in enumerate(v):
            if attr.key in keys:
                raise ValueError(
                    f"Duplicate attribute key: '{attr.key}' at positions {keys[attr.key]} and {i}"
                )
            keys[attr.key] = i
        return v

eunomia_core.schemas.EntityCreate

Bases: EntityBase

Source code in pkgs/core/src/eunomia_core/schemas.py
class EntityCreate(EntityBase):
    uri: Optional[str] = Field(
        default_factory=lambda: generate_uri(),
        description="Unique identifier for the entity, generated if not provided",
    )

    @field_validator("attributes", mode="before")
    @classmethod
    def at_least_one_attribute(cls, v: list[Attribute]) -> list[Attribute]:
        if not v:
            raise ValueError("At least one attribute must be provided")
        return v

    @field_validator("uri", mode="after")
    @classmethod
    def enforce_uri(cls, v: str | None) -> str:
        if v is None:
            return generate_uri()
        return v

eunomia_core.schemas.EntityUpdate

Bases: EntityBase

Source code in pkgs/core/src/eunomia_core/schemas.py
class EntityUpdate(EntityBase):
    type: EntityType = EntityType.any  # type is not required for the attributes update

    @field_validator("attributes", mode="before")
    @classmethod
    def at_least_one_attribute(cls, v: list[Attribute]) -> list[Attribute]:
        if not v:
            raise ValueError("At least one attribute must be provided")
        return v

eunomia_core.schemas.EntityAccess

Bases: EntityBase

Source code in pkgs/core/src/eunomia_core/schemas.py
class EntityAccess(EntityBase):
    uri: Optional[str] = Field(
        default=None, description="Unique identifier for the entity"
    )
    attributes: Optional[list[Attribute]] = Field(
        default_factory=list, description="Entity attributes"
    )

    @model_validator(mode="after")
    def either_uri_or_attributes(self) -> "EntityAccess":
        if not self.uri and not self.attributes:
            raise ValueError("Either 'uri' or non-empty 'attributes' must be provided")
        return self

eunomia_core.schemas.EntityInDb

Bases: EntityBase

Source code in pkgs/core/src/eunomia_core/schemas.py
class EntityInDb(EntityBase):
    attributes: list[AttributeInDb] = Field(..., description="Entity attributes")
    registered_at: datetime = Field(description="Time when this entity was registered")

    model_config = ConfigDict(from_attributes=True)

Access Control Schemas

eunomia_core.schemas.ResourceAccess

Bases: EntityAccess

Source code in pkgs/core/src/eunomia_core/schemas.py
class ResourceAccess(EntityAccess):
    type: Literal[EntityType.resource, EntityType.any] = EntityType.resource

    # The type is always overridden to "resource", although it can accept "any" as input.
    @field_validator("type", mode="after")
    @classmethod
    def override_type(cls, v):
        return EntityType.resource

eunomia_core.schemas.PrincipalAccess

Bases: EntityAccess

Source code in pkgs/core/src/eunomia_core/schemas.py
class PrincipalAccess(EntityAccess):
    type: Literal[EntityType.principal, EntityType.any] = EntityType.principal

    # The type is always overridden to "principal", although it can accept "any" as input.
    @field_validator("type", mode="after")
    @classmethod
    def override_type(cls, v):
        return EntityType.principal

eunomia_core.schemas.AccessRequest

Bases: BaseModel

Source code in pkgs/core/src/eunomia_core/schemas.py
class AccessRequest(BaseModel):
    principal: PrincipalAccess = Field(
        ..., description="The principal requesting access"
    )
    resource: ResourceAccess = Field(..., description="The resource being accessed")
    action: Literal["allow"] = Field(
        default="allow",
        description="Action to be performed on the resource. "
        "Currently only 'allow' is supported.",
    )

eunomia_core.schemas.Policy

Bases: BaseModel

Source code in pkgs/core/src/eunomia_core/schemas.py
class Policy(BaseModel):
    rules: list[AccessRequest] = Field(..., description="List of access rules")