Skip to content

fix(pydantic): key values and update values run the field's pydantic validators - #141

Merged
kalaspuff merged 5 commits into
issues-stack/4-nested-undeclaredfrom
issues-stack/5-pydantic-values
Oct 31, 2026
Merged

kalaspuff merged 5 commits into
issues-stack/4-nested-undeclaredfrom
issues-stack/5-pydantic-values

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

dynamoxy checks values given outside an instance (key values of reads and writes by key, and values update() sets) with the field's own check. For a PydanticModel that was only the codec's type check, because pydantic validates instances. So a key or index key that a validator normalizes (AfterValidator(str.lower), a @field_validator) found no item (members.get("Ada") returned None after creating Member(handle="Ada")), and an update wrote values that Field constraints refuse, then raised ItemDecodeError after the write.

Pydantic has no public API that validates one field, so each field of a PydanticModel gets a validator built on first use from its part of the model's core schema (__pydantic_core_schema__, with its definitions and the model's config). It runs the field's type, Field constraints, Annotated validators and @field_validators, and it applies to key values, index key values and update values, before any I/O. It is not run again on instances, which pydantic has already validated. Validators that take a ValidationInfo are left out, since a single value has no other fields (info.data is None), and so are model validators. A plain validator that takes one cannot be left out, so such a field keeps the codec's check.

Integration tests on both backends cover normalized key lookups, index queries, a Field constraint and a normalizing validator on update values, and a validator with ValidationInfo. A unit test covers definitions, nested models, model validators and a plain validator with info. The integration test failed before the change. uv run pytest, ruff, pyrefly and the consistency checks pass, and the pydantic guide and usage guide describe the behaviour.

Fixes #107

Worth confirming

  • Decision: validators that take a ValidationInfo do not run on key and update values, even those that don't read info.data, rather than running them and failing on validators that do. Values such validators would change or refuse are sent as given.
  • Assumption: the layout of pydantic's core schema (model → model-fields → field schema, wrapped by definitions and by the function nodes of model validators) stays as it is in pydantic 2.7–2.14. If it changes, the validator falls back to the type check rather than failing.

fctl session


Note

Medium risk. It changes which values are accepted or rewritten for every PydanticModel key lookup and update(), and it reads pydantic's non-public core schema, though it falls back to the old type check when the layout is unfamiliar.

What it does. Makes PydanticModel fields validate key values (reads, index queries, writes by key) and values set by update() with the field's own pydantic validators, so a key that an AfterValidator or @field_validator normalizes finds its item and an update no longer writes values that Field constraints refuse. Each field gets a validator built on first use from its part of the model's core schema; validators that take a ValidationInfo and model validators are left out. Since the last review, errors for values outside instances start with the location pydantic reports (for example a list index), and the pydantic guide and usage guide say that a field whose PlainValidator takes a ValidationInfo keeps only its type check.

Pydantic internals. The validator reads __pydantic_core_schema__, which is not public API, and falls back silently to the codec's type check if the layout differs; validators that take a ValidationInfo do not run on key and update values.

fctl review's first look at 506e965 · 9 files, +256 −15 · Review agent settings

fctl-sh Bot and others added 3 commits October 31, 2026 04:53
…validators

dynamoxy checked values given outside an instance (key values of
lookups, values update() sets) with FieldSpec.validate, which for
pydantic models is the codec's type check: normalized keys found
nothing, and updates wrote values Field constraints refuse. Pydantic has
no public one-field validation, so each field gets a validator built
from its schema in the model's core schema (with its definitions and
the model's config), without validators that take a ValidationInfo
(they may read other fields) and without model validators.

Fixes #107

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/_pydantic.py Resolved
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

On the modeling guide and the earlier changelog entry: fixed in 9b5a251. docs/guides/modeling.md now says pydantic models run the field's pydantic validators on key values, and the earlier [Unreleased] entry no longer says pydantic models check key values by type only.

@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/_pydantic.py Resolved
Comment thread docs/guides/pydantic.md Resolved
…guides name plain validators with info

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 7 pull requests, merged in this order with "Squash and merge":

  1. fix(memory): PartiQL index reads refuse attributes the index does not hold #137 fix(memory): PartiQL index reads refuse attributes the index does not hold
  2. fix(models): writes through loaded instances use keys stored in another form #138 fix(models): writes through loaded instances use keys stored in another form
  3. fix(models): repositories refuse instances of subclasses and items of other models #139 fix(models): repositories refuse instances of subclasses and items of other models
  4. fix(models): documents read from an item keep the keys their class does not declare #140 fix(models): documents read from an item keep the keys their class does not declare
  5. fix(pydantic): key values and update values run the field's pydantic validators #141 fix(pydantic): key values and update values run the field's pydantic validators ← this pull request
  6. fix(repository): updates render derived attributes from the stored item, and require it unchanged #142 fix(repository): updates render derived attributes from the stored item, and require it unchanged
  7. fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before #143 fix(tables): provisioned billing gives global indexes capacity, and table steps wait for the one before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant