Grape: 'optional' Array with nested 'requires' params

Created on 18 Aug 2014  路  19Comments  路  Source: ruby-grape/grape

Grape v 0.8.0

Headers:
Content-Type: application/json
Accept: /
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,ru;q=0.6

Validation:

params do
  requires :docs do
    requires :type, type: String
    optional :files do 
      requires :href, type: String
    end
  end
end

Package json(with xml same problem):

{
  "docs": [
       {"type": "aaa"}, 
       {"type": "bbb", "files": [{"href": "some_href"}] }
  ]
}

Result:

<error>
  <message>docs[files][href] is missing</message>
</error>

This

{
  "docs": [
      {"type": "aaa"}
  ]
}

and this:

{
  "docs": [
     {"type": "bbb", "files": [{"href": "som_href"}] }
  ]
}

Works fine!
Help plz! What I'm doing wrong? Sorry if subject duplicate.

bug?

Most helpful comment

All 19 comments

First, add explicit types to docs and files, type: Hash and type: Array, see if it changes anything. Then can you please try against HEAD? If it's still a problem, try to add to a spec to Grape just like this.

Explicit types doesn't help.
"Then can you please try against HEAD" - I'm not sure what you mean.. so, I removed grape and then install it again. Doesn't help too.
I will try add spec at the weekend. Thanks!

Against head I mean change the reference to Grape like this in Gemfile:

gem 'grape', github: 'intridea/grape'

Then bundle update grape.

Updating git://github.com/intridea/grape.git
...
Your bundle is complete!

nothing changed. still:

{"error":"docs[files][href] is missing"}

I have a related problem. With the following parameters, at least date_string_validator is called if I don't pass incidents in the request.
Using debugger, I can see that incidentDate is marked as required when incidents isn't present.
Everything works fine if I pass an empty array for incidents.

...
optional :incidents, type: Array do
  requires :incidentType, type: String,
    values: incident_types
  requires :incidentDetails, type: String,
    incident_mappings: true
  requires :incidentDate, type: String,
    date_string: true
end
...

@ipkes, @taratatach - looks like bugs without digging deeper, it would be helpful to make pull requests with failing specs added to Grape, thanks

I was having this same issue in Grape 0.7.0, with a Hash param rather than Array
Seems like it has been fixed in 0.9.0

I want to add a test to match exactly what's above in the reported issue before closing this, @strobejb if you would like to help, that'd be great!

This is definitely fixed, closing. Please reopen if you still see something like this.

I think this needs to be re-opened. Optional Array and Hash groups do not validate properly when the required parameter is missing. See pull request #883 for specs.

I think you're right @justfalter, reopening.

Any update on this?

Nobody has submitted a fix, please contribute.

Hi,

I have a similar issue :

requires :route, type: Array, allow_blank: false do
    optional :calendar, type: Array, allow_blank: false do
        requires :starts_at, type: Time, allow_blank: false
        requires :ends_at, type: Time, allow_blank: false
    end
end

"route": [{},{}] is valid.
"route":[{},{"calendar": [{"starts_at": "2016-05-02T09:00:00+00:00","ends_at":"2016-05-02T12:00:00+00:00"}]}] returns

{
  "error": "route[0][calendar][0][starts_at] is missing, route[0][calendar][0][ends_at] is missing"
}

Hi @pocman! Can you confirm that if change type of starts_at and ends_at from Time to something else, String for example, error will disappear? I think this is not the same issue.

@dblock for this issue I think we have conflict demands.
For example

      params do
        group :children, type: Array do
          requires :name
          group :parents, type: Array do
            requires :name
          end
        end
      end
      it 'errors when a parameter is not present' do
        put_with_json '/within_array', children: [
          { name: 'Jim', parents: [{}] },
          { name: 'Job', parents: [{ name: 'Joy' }] }
        ]
        expect(last_response.status).to eq(400)
        expect(last_response.body).to eq('children[0][parents][0][name] is missing')
      end

I'm not sure this is right. And I'm not sure should be there a difference in behavior between group and optional Arrays

@ipkes Yes, I changed type of starts_at and ends_at from Time to String and the error will disappeared !

So looks like the Time problem was resolved by #1380, but the original issue was different, @justfalter re-raised https://github.com/ruby-grape/grape/pull/883 and that was fixed. The only thing left is https://github.com/ruby-grape/grape/pull/911 by @al2o3cr. If someone has time, I'd appreciate a rebased run of #911 to see what's failing and what's not.

Was this page helpful?
0 / 5 - 0 ratings