Skip to content

Regexp Validation #608

Closed
Closed
@vptcnt

Description

@vptcnt

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions