ruby-grape / ruby-grape/grape

Extend given to be used outside the current block

Open
#1,226 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This does not work as expected

    it 'includes level 2 nested parameter within #declared(params)' do
      subject.params do
        requires :bar, type: Hash do
          optional :a

          optional :c, type: Hash do
            given :a do
              requires :b
            end
          end
        end
      end
      subject.get('/nested') { declared(params).to_json }
      get '/nested', bar: { a: true, c: { b: 'yes' } }
      expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => "true", 'b' => 'yes' })
    end

This is because given dsl already does the validation and only looks in the current block context. This could be changed so that given also looks in the parent blocks

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 locating the parameter DSL implementation for given, requires, optional, and declared(params), then find the tests covering nested parameter validation. Reproduce the shown nested example and inspect how given resolves values in its current block. Done means the example passes with given :a reading the parent block value without breaking existing validation tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.