Skip to content

Pydantic models: key lookups and update values skip pydantic's validators #107

Closed

Description

@fctl-sh

Priority: medium. dynamoxy checks values outside instances with FieldSpec.validate, and the fields of a PydanticModel carry no dynamoxy constraints or validators (pydantic validates instances), so for pydantic models that check is the codec's type check only. Two places skip pydantic's validation:

  • Key values of reads and writes by key. The fix of Key and index lookups skip the field's validators #80 runs a native field's constraints and validators on them, but a pydantic validator that normalizes a key or index key field still makes items unreachable (example below). docs/guides/pydantic.md documents it: normalize the value before the lookup.
  • Values that update() sets (repository.py, _plan_update and _upsert_completion). The writing guide says they are validated like constructor arguments before any I/O. For a pydantic model, cards.update(Card.key("c1"), attr(Card.name).set("x")) with name: str = Field(min_length=3) writes "x" to DynamoDB and then raises ItemDecodeError ("the update was applied, but the item it returned does not decode"); a value an AfterValidator(str.lower) would normalize is stored as given.
class Member(dynamoxy.PydanticModel, table="members"):
    handle: Annotated[str, dynamoxy.PartitionKey(), AfterValidator(str.lower)]
    email: Annotated[str, dynamoxy.Unique()]

    by_email = dynamoxy.GlobalIndex(partition_key="email")

    @field_validator("email")
    @classmethod
    def lower(cls, value: str) -> str:
        return value.lower()

After members.create(Member(handle="Ada", email="[email protected]")), members.get("Ada") returns None and members.query(index=Member.by_email, email="[email protected]") returns [].

Pydantic has no public API that validates the value of one field with the model's validators. Options:

  • Build a validator per field from that field's schema in __pydantic_core_schema__ (with the schema's definitions), and use it where FieldSpec.validate runs for native models. It runs Annotated validators, Field constraints and @field_validators, but not validators that read other fields (info.data), and it relies on the layout of the core schema.
  • __pydantic_validator__.validate_assignment(Model.model_construct(), name, value) runs the field's validators, but also the model validators, on an instance whose other fields are missing: a mode="after" model validator that reads them fails. Not safe.
  • Document both as limitations.

Found while fixing #80 (fctl session).

Activity

  1. added
    bug
    on Oct 30, 2026
  2. changed the title [-]Key and index lookups of pydantic models skip pydantic's validators[/-] [+]Pydantic models: key lookups and update values skip pydantic's validators[/+] on Oct 30, 2026
  3. added a commit that references this issue on Oct 31, 2026
    00af322
  4. added a commit that references this issue on Oct 31, 2026
    457d17e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions