Skip to content

Reject coerce on non-scalar schemas at compile time - #22

Merged
cryo2010 merged 1 commit into
mainfrom
feat/coerce-guard
Jul 23, 2026
Merged

cryo2010 merged 1 commit into
mainfrom
feat/coerce-guard

Conversation

@cryo2010

Copy link
Copy Markdown
Owner

Summary

.coerce on a non-scalar schema (string.array.coerce, schemaOf(Point).coerce, json().coerce, optional, timestamp) compiled but silently did nothing, because coerceValue's non-scalar arm passes the value through untouched. The classic trap was string.array.coerce looking like element-wise coercion.

Two guard layers, following the strict() precedent:

  • Compile time: the produced type must be string, a number, or bool; anything else fails with a message pointing at the correct spelling (coerce the element: int.coerce.array).
  • Schema build time: for cases the type can't see, a Schema[string] whose node is actually a transform, primKind is checked and a ValueError explains that coerce must come before the transform in the chain. integer().coerce.transform(...) keeps working; transform(...).coerce is rejected.

All existing scalar uses (int.min(0).coerce, sized types like uint16.coerce, bool.coerce, str().coerce, coerce-then-transform) are unchanged, verified by the suite.

Tests

4 new tests (231 total, passing under orc and arc locally): scalar forms still working, non-scalar compile rejections, the transform build-time rejection, and coerce-before-transform continuing to work. README modifiers table updated.

coerce on an array, object, optional, json, or timestamp schema
compiled but silently did nothing (coerceValue's non-scalar arm passes
the value through). Non-scalar produced types now fail compilation
with a pointer at coercing the element instead, and coerce wrapped
around a transform (whose node is not a scalar even when its type is)
raises ValueError at schema build. All existing scalar uses are
unchanged.
@cryo2010
cryo2010 merged commit 09e0980 into main Jul 23, 2026
3 checks passed
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