XPBytes/media_types-validation

View on GitHub
README.md

Summary

Maintainability
Test Coverage
# MediaTypes::Validation
 
[![Build Status: master](https://travis-ci.com/XPBytes/media_types-validation.svg)](https://travis-ci.com/XPBytes/media_types-validation)
[![Gem Version](https://badge.fury.io/rb/media_types-validation.svg)](https://badge.fury.io/rb/media_types-validation)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
 
Line length
Add controller time validation for [media types](https://github.com/SleeplessByte/media_types-ruby) and react accordingly.
 
## Deprecated since `media-types-serialization@1.0.0`
 
Line length
This library will nog longer receive updates because it has been completely obsoleted by changes in the `media-types-serialization`. That library now takes care validating _all_ media-type based serialization responses and media-type based deserialization requests.
 
## Installation
 
Add this line to your application's Gemfile:
 
```ruby
gem 'media_types-validation'
```
 
And then execute:
 
Code block style
Dollar signs used before commands without showing output
$ bundle
 
Or install it yourself as:
 
Code block style
Dollar signs used before commands without showing output
$ gem install media_types-validation
 
## Usage
 
Line length
If you add the `MediaTypes::Validation` controller concern, `validate_json_with_media_type` becomes available during
Trailing spaces
Line length
actions. This does _not_ validate only `JSON` output, but stringifies and then parses the body as `JSON`, so the
Line length
limitations of `JSON` apply. This step is necessary in order to make sure `rails` types and others are first correctly
casted (and formatted).
 
```ruby
require 'media_types/validation'
 
class ApiController < ActionController::API
include MediaTypes::Validation
Trailing spaces
def render_json_media(media, status: :ok)
# serialize_media is a fictional method that returns a serializer
# -> serializer has .to_hash which returns the body as a hash
Line length
# -> serializer has .current_media_type which returns the MediaType::Constructable for the current state
serializer = serialize_media(media)
render json: validate_json_with_media_type(serializer.to_hash, media_type: serializer.current_media_type),
status: status,
content_type: request.format.to_s
end
end
 
class BookController < ApiController
def show
content = GenerateBookResponse.call(@book)
render_json_media(content)
end
end
```
 
Line length
By default, this method only outputs to `stderr` when something is wrong; see configuration below if you want to assign
your own behaviour, such as adding a `Warn` header, or raising a server error.
 
### Configuration
 
In an initializer you can set procs in order to change the default behaviour:
 
```ruby
MediaTypes::Validation.configure do
Trailing spaces
self.json_invalid_media_proc = proc do |media_type:, err:, body:|
response['Warn'] = '199 media type %s is invalid (%s)' % [media_type, err]
warn response['Warn'] + "\n" + body
end
Trailing spaces
# Or alternatively you can always raise
self.raise_on_json_invalid_media = true
end
```
 
### Related
 
Line length
- [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type definitions, schemes and validations
Line length
- [`MediaTypes::Deserialization`](https://github.com/XPBytes/media_types-deserialization): :cyclone: Add media types supported deserialization using your favourite parser, and media type validation.
Line length
- [`MediaTypes::Serialization`](https://github.com/XPBytes/media_types-serialization): :cyclone: Add media types supported serialization using your favourite serializer
 
## Development
 
Line length
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
also run `bin/console` for an interactive prompt that will allow you to experiment.
 
Line length
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
Line length
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](https://rubygems.org).
 
## Contributing
 
Bug reports and pull requests are welcome on GitHub at [XPBytes/media_types-validation](https://github.com/XPBytes/media_types-validation).