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

How to allow empty array for params? #1370

Closed
qsona opened this issue Apr 23, 2016 · 5 comments
Closed

How to allow empty array for params? #1370

qsona opened this issue Apr 23, 2016 · 5 comments

Comments

@qsona
Copy link

qsona commented Apr 23, 2016

I wrote the following code.

params do
  requires :items, type: Array[JSON] do
    requires :id, type: Integer
    requires :enabled, type: Boolean
  end
end
post '/' do
# ...

A request with following params succeeds
{ items: [{ id: 1, enabled: true }, { id: 2, enabled: false }] }

But fails with following.
{ items: [] }

I changed the code to

params do
  optional :items, type: Array[JSON] do

But, all requests failed then.

I want to allow { items: [] } params. Is there any way to do it?

Thanks.

@dblock
Copy link
Member

dblock commented Apr 23, 2016

If you're POSTing JSON, I believe this just works. Care to share the code that does the POSTing?

I'll close this. Please use the Grape mailing list for questions.

@dblock dblock closed this as completed Apr 23, 2016
@liquid1982
Copy link

I have the same problem, and can't figure out what a possible solution could be. It just seems a buggy behaviour. I need to allow the user to publish an article, and optionally specify a section. I also need to accept an empty array, that will result in unpublishing the article from the specified domain.

The non-working code is the following:

desc 'Publish an article'
params do
  optional :published_in, type: Hash do
    optional :'foo.com', type: Array[String], values: ALLOWED_SECTIONS['foo'], allow_blank: true
    optional :'bar.com', type: Array[String], values: ALLOWED_SECTIONS['bar'], allow_blank: true
  end
end
post 'articles/123/actions/publish' do
  # ...
end

But if I omit the values param, things work as expected:

desc 'Publish an article'
params do
  optional :published_in, type: Hash do
    optional :'foo.com', type: Array[String], allow_blank: true
    optional :'bar.com', type: Array[String], allow_blank: true
  end
end
post 'articles/123/actions/publish' do
  # ...
end

I'd like to specify the allowed values in the params block. Any suggestion?

@dblock
Copy link
Member

dblock commented Jan 12, 2018

What is the posting code? What is the error? What content-type is it POSting @liquid1982 ?

@liquid1982
Copy link

liquid1982 commented Jan 12, 2018

@dblock It is something like:

$.ajax({
  url: url,
  method: 'POST',
  contentType: 'application/json',
  data: JSON.stringify(payload)
}).then(() => {})

and the error message is published_in[foo.com] has a value not allowed.

@dblock
Copy link
Member

dblock commented Jan 14, 2018

You should turn it into a spec @liquid1982 in this project.

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

No branches or pull requests

3 participants