Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regexp Validation #608

Closed
vptcnt opened this issue Mar 27, 2014 · 3 comments · Fixed by #614
Closed

Regexp Validation #608

vptcnt opened this issue Mar 27, 2014 · 3 comments · Fixed by #614
Labels

Comments

@vptcnt
Copy link

vptcnt commented Mar 27, 2014

Hi

I am write an API using Grape.
Writing my rspec tests, I noticed a problem with a regexp validation of a param.
My Api is :

class Methods < Grape::API
  helpers do
    def results
      {
        method:  route.route_method,
        id:      params[:id]
      }
    end
  end

  params do
    requires :id, regexp: /^\d{5}$/
  end

  namespace :tests do
    [:get, :post, :put, :delete].each do |method|
      desc "Renvoie le parametre Id via #{method}"
        route(method, method) do
          results
        end
    end
  end
end

The Rspec file 👍

describe "Test" do

  params = [[nil, 'aucun param', :missing],
            [{ :id => nil }, 'id is nul', :invalid],
            [{ :id => '' }, 'id is vide', :invalid],
            [{ :id => 'A' }, 'id is "A"', :invalid],
            [{ :id => '54' }, 'id is "54"', :invalid],
            [{ :id => 78 }, 'id is 78', :invalid],
            [{ :id => 1234 }, 'id is 1234', :invalid],
            [{ :id => 567890 }, 'id is 567890', :invalid],
            [{ :id => "0000" }, 'id is "0000"', :invalid],
            [{ :id => 0000 }, 'id is 000', :invalid]
  ]

  params.each do |pp|
    it "si #{pp[1]}" do
      get "/tests/get", pp[0]
      expect(response.status).to eql(403)
      expect(response.body).not_to be_empty
      jRes = JSON.parse(response.body, symbolize_names: true)
      expect(jRes).to have(1).items
      expect(jRes).to have_key(:error)
      expect(jRes[:error]).to be_eql("id is #{pp[2]}")
    end
  end

The problem is with the test : :id => null

The API returns a 200 and a result :

{
  "method" : "GET",
  "id" : null
}

If I want to test the id is null from a browser, I enter as an URL 👍

http://xxxxxx/test/get?id

I have a the same result, so the require and the regexp are not done...

@dblock
Copy link
Member

dblock commented Mar 27, 2014

Could you add this in a pull request to Grape HEAD, this is possibly fixed by #492.

@dblock dblock added the bug? label Mar 27, 2014
@dm1try
Copy link
Member

dm1try commented Apr 3, 2014

@dblock, seems like #492 covers only ValuesValidator flow.

@dblock
Copy link
Member

dblock commented Apr 3, 2014

@vptcnt, see whether #614 fixed your issue, thx @dm1try .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants