Skip to content

fix(models): key and index lookups run the field's validators, as writes do - #109

Merged
kalaspuff merged 8 commits into
mainfrom
fctl/58-https-github-com-kalaspuff-dynamoxy-issu
Oct 31, 2026
Merged

kalaspuff merged 8 commits into
mainfrom
fctl/58-https-github-com-kalaspuff-dynamoxy-issu

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

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 a GlobalIndex partition key, query(index=User.by_email, email="[email protected]") returned nothing for the item stored with [email protected], and get("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 raises ValueError before any request, and so does None, 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"}), which batch_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 with None for templated keys, and reads, table and index queries, transact_get, collection queries, writes by key and a batch_get() of an item stored before its validator, each on the in-memory backend and MiniStack. make check passes.

Fixes #80

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (modeling, reading, constraints and pydantic guides, AGENT_GUIDE.md, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No, but reads with a value the field refuses now raise ValueError, which get() and query() document for invalid key arguments (see below)

Worth confirming

  • Decision: A lookup with a value the field refuses (a constraint such as the quickstart's pattern= on email, or a validator that raises) now raises ValueError before any request, where get() returned None and queries returned no items. Keys built from templates already behaved this way. An item whose key predates such a rule is no longer found by get(), query() or delete("value"), only through a stored key (batch_get([{"user_id": ...}]), delete({...})) or the dict-level client.table(...).
  • Decision: Stored-key dicts are not validated: members.update({"handle": "Ada"}, ...) targets "Ada", while members.update(Member.key("Ada"), ...) and members.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).
  • Decision: Conditions (where(), filter(), condition=) still compare with the value as given: they take prefixes and bounds, which constraints and validators would refuse or change.
  • Decision: Pydantic models are left to Pydantic models: key lookups and update values skip pydantic's validators #107: pydantic has no public way to run one field's validators without the rest of the model.

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 ValueError instead of returning None or 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 as str.lower finds the item it stored. A value the field refuses, or None for a templated key, raises ValueError before 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 as batch_get([{"user_id": ...}]), which the guides now say.

fctl review's first look at ca7e89b · 10 files, +225 −16 · Review agent settings

…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]>
@fctl-review

This comment has been minimized.

Comment thread docs/guides/modeling.md Resolved
Comment thread tests/unit/test_models.py Resolved
fctl-sh Bot and others added 7 commits October 30, 2026 23:35
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
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
@kalaspuff
kalaspuff merged commit 0020be6 into main Oct 31, 2026
20 checks passed
@kalaspuff
kalaspuff deleted the fctl/58-https-github-com-kalaspuff-dynamoxy-issu branch October 31, 2026 00:00
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.

Key and index lookups skip the field's validators

1 participant