Skip to content

Off the back of #2464, we might also want to remove the string exampl… #2568

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

Conversation

pieterocp
Copy link
Contributor

Off the back of #2464, we might also want to remove the string example from the readme too.

Since these dont work at all, just error and mislead.

Let me know if I'm just a big noob and have misconfigured my project or something.

…ing example from the readme too

Since these dont work at all, just error and mislead
@ericproulx
Copy link
Contributor

It should work with a String since the type responds to length. Do you have an example ?

@pieterocp
Copy link
Contributor Author

It should work with a String since the type responds to length. Do you have an example ?

Does work well, messed up and was using an older version in my project, thought that a more complex scenario would also error but was user error on my side.

Will close this, but a String example inside the spec might help peanut brains like myself out?

e.g.

# frozen_string_literal: true

describe Grape::Validations::Validators::LengthValidator do
  let_it_be(:app) do
    Class.new(Grape::API) do
      # ...

      params do
        requires :list, type: String, length: { min: 1, max: 3 }
      end
      post 'string_example' do
      end
      
      # ...
      
  describe '/string_example' do
    context 'when length is between limits' do
      it do
        post '/string_example', list: 'ab'
        expect(last_response.status).to eq(201)
        expect(last_response.body).to eq('')
      end
    end
    context 'when length is greater than limit' do
      it do
        post '/string_example', list: 'too long'
        expect(last_response.status).to eq(400)
        expect(last_response.body).to eq('list is expected to have length within 1 and 3')
      end
    end
  end
  
  # ...
end

@pieterocp pieterocp closed this Jun 7, 2025
@dblock
Copy link
Member

dblock commented Jun 8, 2025

@pieterocp Do PR it!

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.

3 participants