Repository navigation
fix(models): key and index lookups run the field's validators, as writes do - #109
Merged
kalaspuff merged 8 commits intoOct 31, 2026
Merged
Conversation
…tes do Writes validate a field's values with its constraints and validators, but the key values given to reads only got the codec's type check, so a validator that normalizes made items unreachable: with field(unique=True, validators=(str.lower,)) as an index key, querying email="[email protected]" returned nothing for the stored "[email protected]", and get() missed items whose partition key a validator normalized. Key templates already validated their values. ModelSchema now checks every key value a caller gives (key_from, index and table queries, collection queries, writes and transactions by key) like writes check the field: constraints and validators included, and the codec alone for derived and automatic fields. A refused value, or None (which templated keys sent to DynamoDB), raises ValueError before any request, and errors of template values name their field. Stored-key dicts are used as they are, conditions compare the value as given, and pydantic models keep the type check only. Fixes #80 Co-authored-by: Carl Oscar Aaro <[email protected]>
This comment has been minimized.
This comment has been minimized.
get(), get_or_raise(), exists(), query() and Get() take key values
only, which are validated now, so the guides' "a stored key is used as
it is" promised them an escape hatch they lack: an item whose key
predates a constraint or validator is reached through the calls that
take a stored key (batch_get(), update(), delete(), batch_delete() and
transactions) or the dict-level table. The modeling guide, the agent
guide and the changelog now name them, and a test reads such an item
with batch_get() on both backends.
A test now covers None for templated keys (positional, by name and as a
template value), which produced {"pk": None} before the fix.
Co-authored-by: Carl Oscar Aaro <[email protected]>
The modeling guide listed update() among the calls that take a key as
a plain value ("u1"), which works at runtime but not for type checkers:
update() takes an instance or a mapping, so its key values are field
values such as {"device_id": device_uuid}.
Co-authored-by: Carl Oscar Aaro <[email protected]>
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
Co-authored-by: Carl Oscar Aaro <[email protected]>
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
…com-kalaspuff-dynamoxy-issu # Conflicts: # CHANGELOG.md
kalaspuff
deleted the
fctl/58-https-github-com-kalaspuff-dynamoxy-issu
branch
October 31, 2026 00:00
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.
What and why
Writes check a field's values with its constraints and validators, but the key values given to reads only got the codec's type check, so a validator that normalizes made items unreachable: with
email = field(unique=True, validators=(str.lower,))as aGlobalIndexpartition key,query(index=User.by_email, email="[email protected]")returned nothing for the item stored with[email protected], andget("Ada")missed an item whose partition key a validator stored as"ada". Key templates already validated the values they use.Key values that callers give are now checked as writes check the field, constraints and validators included, wherever dynamoxy builds a key from values:
get(),exists(),batch_get(), table and index queries (partition and sort key arguments), collection queries,Model.key(),Get(), and writes and transactions by key. A value the field refuses raisesValueErrorbefore any request, and so doesNone, which templated keys used to send to DynamoDB; errors of the values a key template uses now name their field. Stored keys ({"user_id": "u1"}), whichbatch_get(),update(),delete()and transactions take, are used as they are, so they still reach items whose keys predate a constraint or validator; the guides say which calls take one. Conditions keep using the value as given, and pydantic models keep the type check only, which the pydantic guide now says (#107 tracks it).Verified with new tests that fail without the fix:
Model.key()with validators that normalize and refuse values and withNonefor templated keys, and reads, table and index queries,transact_get, collection queries, writes by key and abatch_get()of an item stored before its validator, each on the in-memory backend and MiniStack.make checkpasses.Fixes #80
Checklist
AGENT_GUIDE.md, changelog)make checkpasses locally (lint, types, clippy, consistency, tests)ValueError, whichget()andquery()document for invalid key arguments (see below)Worth confirming
pattern=onemail, or a validator that raises) now raisesValueErrorbefore any request, whereget()returnedNoneand queries returned no items. Keys built from templates already behaved this way. An item whose key predates such a rule is no longer found byget(),query()ordelete("value"), only through a stored key (batch_get([{"user_id": ...}]),delete({...})) or the dict-levelclient.table(...).members.update({"handle": "Ada"}, ...)targets"Ada", whilemembers.update(Member.key("Ada"), ...)andmembers.delete("Ada")target"ada". A dict of the key attributes with text or number values is the stored form, which running a validator again could change (one that adds a prefix would add it twice).where(),filter(),condition=) still compare with the value as given: they take prefixes and bounds, which constraints and validators would refuse or change.fctl session
Note
Medium risk. It changes how every key value given to a read, query or write by key is checked, so lookups with a value the field refuses now raise
ValueErrorinstead of returningNoneor no items, and items stored before a constraint existed are reachable only through stored keys.What it does. Key and index key values that callers give (
get(),exists(),batch_get(), table, index and collection queries,Model.key(),Get(), writes by key) now run the field's constraints and validators, as writes do, so a normalizing validator such asstr.lowerfinds the item it stored. A value the field refuses, orNonefor a templated key, raisesValueErrorbefore any request. Stored-key dicts, conditions and pydantic models keep their old checks, and the guides, changelog and tests on both backends cover it.Behavior change. Lookups by plain value that the field's constraints or validators refuse now raise
ValueError; items whose keys predate a rule are read with a stored key such asbatch_get([{"user_id": ...}]), which the guides now say.fctl review's first look at
ca7e89b· 10 files, +225 −16 · Review agent settings