Repository navigation
fix(pydantic): key values and update values run the field's pydantic validators - #141
Merged
kalaspuff merged 5 commits intoOct 31, 2026
Conversation
…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]>
Co-authored-by: Carl Oscar Aaro <[email protected]>
This comment has been minimized.
This comment has been minimized.
…ir fields too Co-authored-by: Carl Oscar Aaro <[email protected]>
Contributor
Author
|
On the modeling guide and the earlier changelog entry: fixed in 9b5a251. |
This comment has been minimized.
This comment has been minimized.
…guides name plain validators with info Co-authored-by: Carl Oscar Aaro <[email protected]>
This was referenced Oct 31, 2026
Contributor
Author
|
Stack of 7 pull requests, merged in this order with "Squash and merge":
|
This was referenced Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aPydanticModelthat 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")returnedNoneafter creatingMember(handle="Ada")), and an update wrote values thatFieldconstraints refuse, then raisedItemDecodeErrorafter the write.Pydantic has no public API that validates one field, so each field of a
PydanticModelgets 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,Fieldconstraints,Annotatedvalidators 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 aValidationInfoare left out, since a single value has no other fields (info.dataisNone), 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
Fieldconstraint and a normalizing validator on update values, and a validator withValidationInfo. 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
ValidationInfodo not run on key and update values, even those that don't readinfo.data, rather than running them and failing on validators that do. Values such validators would change or refuse are sent as given.model→model-fields→ fieldschema, wrapped bydefinitionsand 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
PydanticModelkey lookup andupdate(), 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
PydanticModelfields validate key values (reads, index queries, writes by key) and values set byupdate()with the field's own pydantic validators, so a key that anAfterValidatoror@field_validatornormalizes finds its item and an update no longer writes values thatFieldconstraints refuse. Each field gets a validator built on first use from its part of the model's core schema; validators that take aValidationInfoand 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 whosePlainValidatortakes aValidationInfokeeps 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 aValidationInfodo not run on key and update values.fctl review's first look at
506e965· 9 files, +256 −15 · Review agent settings