When building an API in JSON it is important to follow conventions so that the clients know how what to expect in the response when operating on the available resources.
JSON API Conformant provides a narrow interface for validating JSON objects against the JSON API specification using the official schema and provides a custom RSpec matcher for use in your tests.
Important: Currently v1.0 specification is used as the default base schema. The base schema is not a perfect document. The fact that a JSON document validates against this schema, it does not necessarily mean it is a valid JSON API document. The schema is provided by JSON API for a base level sanity check.
Add this line to your application's Gemfile:
gem 'json_api_conformant'
And then execute:
$ bundle
Or install it yourself as:
$ gem install json_api_conformant
Validate if your JSON object is JSON API conformant.
valid_schema = { "data" => [{ "type" => "posts", "id" => "1" }] }
invalid_schema = { "data" => [{ "posts" => "1" }] }
JSON::API::Conformant.valid?(valid_schema) # => true
JSON::API::Conformant.valid?(invalid_schema) # => false
Get errors when your JSON object is JSON API not conformant.
valid_schema = { "data" => [{ "type" => "posts", "id" => "1" }] }
invalid_schema = { "data" => [{ "posts" => "1" }] }
JSON::API::Conformant.validate(valid_schema) # => []
JSON::API::Conformant.validate(invalid_schema) # => ["The property '#/' of type object did not match any of the..."]
JSON API Conformant wraps json-schema, so other options that validator accepts will work here too.
schema = { "data" => [{ "posts" => "1" }] }
JSON::API::Conformant.valid?(schema, insert_defaults: true)
JSON::API::Conformant.validate(schema, errors_as_objects: true)
Only tested for RSpec 3.
It is pretty straighword to use.
it "validates that's JSON API conformant" do
data = { "data" => [{ "posts" => "1" }] }
expect(data).to be_json_api_conformant
end
- Fork it ( https://github.com/[my-github-username]/json_api_conformant/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Run the test suite (
rake
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Everyone interacting in this codebase and issue tracker is expected to follow the code of conduct.
See LICENSE for details.