Skip to content

Tell a missing root: from a malformed one - #23

Open
VSN2015 wants to merge 1 commit into
masterfrom
fix/malformed-root-code
Open

Tell a missing root: from a malformed one#23
VSN2015 wants to merge 1 commit into
masterfrom
fix/malformed-root-code

Conversation

@VSN2015

@VSN2015 VSN2015 commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Third and smallest of the bug fixes from the same hunt (#21, #22).

The bug

permit_params :create, root: :user do
  required :name, :string
end
{ "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 user is 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:

Request Code
{} missing
{"user": null} missing
{"user": ""} missing
{"user": "bob"} invalid_type
{"user": []} invalid_type
{"user": 3} invalid_type

Absence is decided by the gem's own permittable_absent?, so "" and null still 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_type is 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

  • 201 examples, 0 failures (2 new, written before the fix), covering all six shapes above
  • The pre-existing spec asserting :bad_request for { user: "nope" } deliberately never asserted the code, so it still passes unchanged
  • RuboCop clean

@VSN2015 VSN2015 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of PR #23: Clear diagnostic improvement. Distinguishing between a truly absent root (missing) and an incorrectly typed root (invalid_type) eliminates developer confusion while maintaining 400 Bad Request semantics.

Comment thread lib/permittable.rb
# 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"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
VSN2015 force-pushed the fix/malformed-root-code branch from c499c76 to 391459a Compare September 11, 2026 22:01
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