voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 242 forks source link

undefined method `each' for "<property>":String #409

Closed niightly closed 6 years ago

niightly commented 6 years ago

Guys I'm trying to write some tests using a json matcher... but without luck

JSON Object from tests

{
   "search":{
      "entry":[
         {
            "dn":"Here is some fake data",
            "attribute":[
               {
                  "name":"description",
                  "value":[
                     "Fake Name"
                  ]
               },
               {
                  "name":"objectclass",
                  "value":[
                     "teste",
                     "top"
                  ]
               }
            ]
         }
      ]
   }
}

Schema to be validated

class FakeObject
  def self.schema
    {
      type: 'object',
      required: 'search',

      properties: {
        search: {
          type: 'object',
          required: 'entry',

          properties: {
            entry: {
              type: 'array',

              items: {
                type: 'object',
                required: %w[dn attribute],

                properties: {
                  dn: { type: 'string' },
                  attribute: {
                    type: 'array',

                    items: {
                      type: 'object',
                      required: %w[name value],

                      properties: {
                        name: { type: 'string' },
                        value: { type: 'array' }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  end
end

Custom Matcher

RSpec::Matchers.define :match_response_schema do |schema|
    match do |response|
        if schema.include?('_')
            class_name = schema.split('_')
            class_name = class_name.collect(&:capitalize)
            schema = class_name.join('')
        end

        JSON::Validator.validate!("#{schema}".safe_constantize.schema, response, strict: true)
    end
end

RSpec Test

RSpec.describe BluepagesService do
    describe '#Positive scenarios' do
        it 'Fetch data from FakeObject' do
            data = FakeService.fetch_data
            expect(data).to match_response_schema('FakeObject')
        end
    end
end

Getting this Error

1) FakeService#Positive scenarios Fetch data from FakeObject
     Failure/Error: JSON::Validator.validate!("#{schema}".safe_constantize.schema, response, strict: true)

     NoMethodError:
       undefined method `each' for "search":String
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/attributes/required.rb:12:in `validate'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:25:in `block in validate'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:23:in `each'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:23:in `validate'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema.rb:33:in `validate'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/validator.rb:113:in `validate'
     # /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/validator.rb:254:in `validate!'
     # ./spec/support/api_schema_matcher.rb:11:in `block (2 levels) in <top (required)>'
     # ./spec/services/fake_service_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 2.96 seconds (files took 2.46 seconds to load)

Versions

Gems installed

I'm not sure which one will be important on this issue, if you need more I will be happy to update it.

niightly commented 6 years ago

Guys anyone? Is this repo active?

niightly commented 6 years ago

found the problem... Looks like the required MUST be an array even when there is only one property on it.