Jsonschema::Matchers
Jsonschema matchers
Installation
Add this line to your application's Gemfile:
gem 'jsonschema-matchers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jsonschema-matchers
Usage
Json example
RSpec.describe 'Users Requests', type: :request do
describe 'GET index' do
before { create_list(:user, 5) }
it 'matches correct schema' do
get '/users'
expect(response.body).to match_jsonschema
end
end
end
Will match/create schema
spec/jsonschemas/Users_Requests/GET_index/matches_correct_schema.json
{
"title": "Root",
"type": "array",
"minItems": 5,
"maxItems": 5,
"items": {
"properties": {
"id": {
"type": "integer"
},
"email": {
"type": "string"
},
"password_digest": {
"type": "null"
},
"created_at": {
"type": "string"
},
"updated_at": {
"type": "string"
},
"posts": {
"type": "array",
"minItems": 0,
"maxItems": 0,
"items": {
}
}
},
"type": "object",
"required": [
"id",
"email",
"password_digest",
"created_at",
"updated_at",
"posts"
]
}
}
JsonApi example
RSpec.describe 'Users Requests', type: :request do
describe 'GET show' do
let(:user) { create(:user) }
before { create(:post, user: user) }
it 'matches correct schema with jsonapi spec' do
get "/users/#{user.id}"
expect(response.body).to match_jsonschema
end
end
end
Will match/create schema
spec/jsonschemas/Users_Requests/GET_show/matches_correct_schema_with_jsonapi_spec.json
{
"title": "Root",
"properties": {
"data": {
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"attributes": {
"properties": {
"email": {
"type": "string"
}
},
"type": "object",
"required": [
"email"
]
},
"relationships": {
"properties": {
"posts": {
"properties": {
"data": {
"type": "array",
"minItems": 1,
"maxItems": 1,
"items": {
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"type": "object",
"required": [
"id",
"type"
]
}
}
},
"type": "object",
"required": [
"data"
]
}
},
"type": "object",
"required": [
"posts"
]
}
},
"type": "object",
"required": [
"id",
"type",
"attributes",
"relationships"
]
}
},
"type": "object",
"required": [
"data"
]
}
Configuration
Jsonschema::Matchers.configure do |config|
config.schema_dir = 'spec/jsonschemas' # Default path
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonschema-matchers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Jsonschema::Matchers project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.