0.0
No commit activity in last 3 years
No release in over 3 years
Use power of ruby DSL to validate json!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 2.0

Runtime

 Project Readme

SchemaMatcher

Use power of ruby DSL to validate json!

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'schema_matcher'
end

And then execute:

$ bundle

Or install it yourself as:

$ gem install schema_matcher

Add include to spec/spec_helper.rb or spec/rails_helper.rb if using Rails

require "schema_matcher/rspec"

Usage

  1. Write your schema
SchemaMatcher.build_schema do
  define :user do
    attribute :id, type: :number
    attribute :first_name #it uses type string by default
    attribute :last_name
    attribute :avatar, nullable: true
    attribute :disabled, type: :boolean
    attribute :role, optional: true
    attribute :posts, array: true, ref: :post
  end

  define :post do
    attribute :id, type: :number
    attribute :content
    attribute :author do
      attribute :id, type: :number
      attribute :first_name
      attribute :last_name
    end
  end
end
  1. Check responses:
RSpec.describe 'Users' do
 let(:json_response) { JSON.parse(response.body) }
 
  it 'returns user' do
    get '/users/1'
    expect(json_response).to match_json_schema(:user)
  end

  it 'returns a few users' do
    get '/users'
    expect(json_response).to match_json_schema(:user, array: true)
  end
end
  1. Enjoy!

Contributing

  • Fork the project.
  • Add a breaking test for your change.
  • Make the tests pass.
  • Run rubocop -a
  • Push your fork.
  • Submit a pull request.

License

The gem is available as open source under the terms of the MIT License.