ruby-grape / ruby-grape/grape

Unknown validator exception for read_only when using requires/optional with Entity

Open
#2,345 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Hello Grape team,

I am using grape-entity objects for defining my endpoint's parameters, and I ran across this issue.

My entity exposes ID as a read only value, as our back end controls this value

 expose :id, documentation: {type: String, read_only: true}
 # .. other attributes

When defining a PUT request that uses the entity documentation for defining the params it generates an error

Failure/Error: requires :all, using: Entity.documentation.except(:created_at, :updated_at)

Grape::Exceptions::UnknownValidator:
  unknown validator: read_only

I found #2338 which fixed a similar issue for other documentation keys, i.e. is_array, which have been using. I noticed in the PR @mscrivo mentions there maybe other keywords that belong in RESERVED_DOCUMENTATION_KEYWORDS and I wonder if this is one of them?

At first I thought it wasn't, but as a PUT request requires an ID to determine which record to update and our back end controls ID creation, then ID should be marked as read_only to inform users of the API that they cannot change or supply their own value.

If this thinking is correct, then I would be happy to create a PR to include read_only in RESERVED_DOCUMENTATION_KEYWORDS. If not then I will re-evaluate my approach.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Entity.documentation through requires :all and reviewing RESERVED_DOCUMENTATION_KEYWORDS, especially the handling introduced by #2338. Reproduce the unknown-validator error with the issue's Ruby example, then verify the chosen behavior with a regression test; done means read_only is handled as documentation metadata rather than passed to validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.