Skip to content

fix(ack): align discriminator errors across parse and encode#111

Merged
leoafarias merged 2 commits into
mainfrom
leoafarias/discriminated-encode-regression
May 28, 2026
Merged

fix(ack): align discriminator errors across parse and encode#111
leoafarias merged 2 commits into
mainfrom
leoafarias/discriminated-encode-regression

Conversation

@leoafarias
Copy link
Copy Markdown
Collaborator

Summary

  • Encode now returns the same focused SchemaConstraintsErrors as parse for missing, non-string, and unknown discriminator values, by sharing _validateDiscriminatorKey between the parse path and validateRuntimeWithContext.
  • Map runtimes that carry the discriminator still short-circuit to the named branch; model- and codec-backed runtimes that synthesize the discriminator still iterate branches.
  • Adds _encodeBranch to dedupe the per-branch try/catch → encoderThrew and the boundary checks ("must emit key", "value matches branch label") that the fast and slow paths both run.

Test plan

  • dart test test/schemas/discriminated_object_schema_test.dart — 27/27
  • dart test (full ack suite) — 898/898
  • dart analyze --fatal-infos — clean
  • dart format --set-exit-if-changed — clean
  • New regression tests cover: parse/encode missing-discriminator parity, map-runtime codec that synthesizes the discriminator, and the three encode error shapes (required-property, invalid-type, enum) for union-owned discriminators

Encode was returning aggregated nested errors when the discriminator
was missing, malformed, or unknown, while parse returned focused
SchemaConstraintsErrors. The two paths now share
`_validateDiscriminatorKey`, so encode emits the same required/invalid-
type/enum constraint error users already see at parse time.

Encode still short-circuits to the named branch when the runtime map
carries the discriminator, and still tries each branch for model- or
codec-backed runtimes that synthesize the discriminator during encode.
A new `_encodeBranch` helper centralizes the per-branch try/catch and
the boundary checks ("must emit key", "value matches branch label")
that the fast and slow paths both perform.

Adds regression coverage for parse/encode error parity, a map-runtime
codec that emits the discriminator, and the three focused encode error
shapes (missing, non-string, unknown) for union-owned discriminators.
@docs-page
Copy link
Copy Markdown

docs-page Bot commented May 28, 2026

To view this pull requests documentation preview, visit the following URL:

docs.page/btwld/ack~111

Documentation is deployed and generated using docs.page.

The previous commit added `_matchesAnyBranchRuntime` plus a map-block
in `validateRuntimeWithContext` so that calling validateRuntime
directly stayed consistent with what encode would do for a map missing
the discriminator. The consistency was edge-case and the cost was
~21 lines that most users never observe.

Drop both. The encode fast path now calls `_validateDiscriminatorKey`
inline to keep focused errors for non-string and unknown discriminator
values (this used to fall out of the validateRuntime block).
validateRuntime called directly is now stricter than encode for the
specific case of "a JsonMap missing the discriminator key", which is a
defensible invariant: a valid discriminated runtime carries its label.

Behavior preserved: PR #107 union-owned discriminator (branches that
omit the property) still works via `effectiveBranch`, and PR #109
codec branches that synthesize the discriminator at encode time still
succeed through the slow path.

Net: -33 lines on this PR.
@leoafarias leoafarias merged commit 9d8e7e2 into main May 28, 2026
1 check passed
@leoafarias leoafarias deleted the leoafarias/discriminated-encode-regression branch May 28, 2026 16:53
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.

1 participant