brooklynDev/airborne

expect_json_types for elements of an array not working

Opened this issue · 4 comments

Hi,
I want to validate the json schema for an array and it's elements but I'm unable to do that with Airborne. Continuously getting errors.

Here is part of my data :-

description: [
{
program_id: 317103604,
program_description: "This animated series tells of the adventures of boy reporter Tintin, his dog, Snowy, and their friend Captain Haddock. From the stories by Herge.",
program_description_type: "Generic Description"
},
{
program_id: 317103604,
program_description: "Go on wild adventures with a boy reporter Tintin, his dog and Captain Haddock.",
program_description_type: "Generic Description"
}
]

So as per documentation if I try this :-
expect_json_types('description.*',program_id: :integer, program_description: :string, program_description_type: :string)

I must be able to validate the json types but I'm getting this error :-

Failures:

  1. api /program/:programid: should validate types and values of movie data
    Failure/Error: expect_json_types('description.*',program_id: :integer, program_description: :string, program_description_type: :string)
 Airborne::PathError:
   Expected Array
   to be an object with property description
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:21:in `rescue in block in get_by_path'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:18:in `block in get_by_path'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `each_with_index'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:9:in `get_by_path'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:137:in `call_with_path'
 # /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/request_expectations.rb:12:in `expect_json_types'
 # ./spec/program_programid_spec/api_program_programid_for_mov_happy_path_new_spec.rb:52:in `block (2 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # TypeError:
 #   no implicit conversion of Symbol into Integer
 #   /home/vj/.rvm/gems/ruby-2.1.4/gems/airborne-0.2.5/lib/airborne/path_matcher.rb:57:in `[]'

Finished in 0.61998 seconds (files took 0.41055 seconds to load)
1 example, 1 failure

Please can you look into this. issue.

Is description a key on an object?

Pretty sure thats not valid JSON. It should look like:

{
    "description": [{
        "program_id": 317103604,
        "program_description": "This animated series tells of the adventures of boy reporter Tintin, his dog, Snowy, and their friend Captain Haddock. From the stories by Herge.",
        "program_description_type": "Generic Description"
    }, {
        "program_id": 317103604,
        "program_description": "Go on wild adventures with a boy reporter Tintin, his dog and Captain Haddock.",
        "program_description_type": "Generic Description"
    }]
}

Yes Seth, description is a key on a bigger object.
I just pasted a part of the json. I'm sorry for that.

And yes, what I had earlier posted was not a valid json. What you have corrected is the valid one.
Forgive me for copying the key values directly from the browser and not the raw version.

An update on this issue :-

I'm able to do schema validation for all arrays but not able to do the same for any of the arrays.

So,
expect_json_types('description.*', {program_id: :integer,program_description: :string,program_description_type: :string}) works fine.

But,
expect_json_types('description.?', {program_id: :integer,program_description: :string,program_description_type: :string}) does'nt work.

It still checks all arrays.

My Scenario: I have a json response of programs in which the majority of programs have descriptions which consist of an array of hashes, but for one particular program the description key is null.

So my test has inevitably become if any of the description key matches the following schema.

Thanks for your support once again. :)

Regards,
VJ