Tell a missing root: from a malformed one - #23
Open
VSN2015 wants to merge 1 commit into
Open
Conversation
VSN2015
commented
Sep 5, 2026
| # No field declares the root, so message resolution can only come from | ||
| # I18n ({} has no :message). | ||
| violations << permittable_violation({}, rule[:root].to_s, "missing") | ||
| code = permittable_absent?(value, raw, key) ? "missing" : "invalid_type" |
Owner
Author
There was a problem hiding this comment.
permittable_absent?(value, raw, key) ? "missing" : "invalid_type" provides a much cleaner error report when a client sends {"user": "bob"} instead of misleadingly claiming the user key was omitted.
{"user": "bob"} against a root: :user contract answered
{ param: "user", code: "missing" }
for a key the client had just sent, which sends it looking in exactly
the wrong place.
An absent root and a malformed one are different client mistakes and
now read differently: "missing" when the key really is absent, and
"invalid_type" when it arrived as something other than an object.
Absence is decided by the gem's own permittable_absent?, so
{"user": ""} and {"user": null} still read as missing rather than
becoming a new kind of error — consistent with how every other field
treats an empty value.
Both remain a 400: either way the envelope itself is malformed, so
nothing changes at the HTTP level and no client's status handling
moves. Only the diagnostic gets accurate.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
VSN2015
force-pushed
the
fix/malformed-root-code
branch
from
September 11, 2026 22:01
c499c76 to
391459a
Compare
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.
The bug
{ "user": "bob" }answered:
{ "param": "user", "code": "missing" }…for a key the client had just sent. That sends it looking in exactly the wrong place — the problem isn't that
useris absent, it's that it isn't an object.The fix
An absent root and a malformed one are different client mistakes, and now read differently:
{}missing{"user": null}missing{"user": ""}missing{"user": "bob"}invalid_type{"user": []}invalid_type{"user": 3}invalid_typeAbsence is decided by the gem's own
permittable_absent?, so""andnullstill read as missing rather than becoming a new kind of error — consistent with how every other field in the gem treats an empty value.Nothing moves at the HTTP level
Both remain 400 — either way the envelope itself is malformed — so no client's status handling changes. Only the diagnostic gets accurate.
invalid_typeis already the code the gem uses for "this value can't be the declared shape", and it's already listed in the exported OpenAPI error schema, so consumers need no new vocabulary.Verification
:bad_requestfor{ user: "nope" }deliberately never asserted the code, so it still passes unchanged