README.md
# MediaTypes::Validation [](https://travis-ci.com/XPBytes/media_types-validation)[](https://badge.fury.io/rb/media_types-validation)[](http://opensource.org/licenses/MIT) Line lengthAdd 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 lengthThis 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: ```rubygem '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 lengthIf you add the `MediaTypes::Validation` controller concern, `validate_json_with_media_type` becomes available duringTrailing spaces
Line lengthactions. This does _not_ validate only `JSON` output, but stringifies and then parses the body as `JSON`, so the Line lengthlimitations of `JSON` apply. This step is necessary in order to make sure `rails` types and others are first correctlycasted (and formatted). ```rubyrequire 'media_types/validation' class ApiController < ActionController::API include MediaTypes::ValidationTrailing 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 hashLine 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 endend class BookController < ApiController def show content = GenerateBookResponse.call(@book) render_json_media(content) endend``` Line lengthBy default, this method only outputs to `stderr` when something is wrong; see configuration below if you want to assignyour 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: ```rubyMediaTypes::Validation.configure doTrailing 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 endTrailing spaces # Or alternatively you can always raise self.raise_on_json_invalid_media = trueend``` ### Related Line length- [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type definitions, schemes and validationsLine 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 lengthAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You canalso run `bin/console` for an interactive prompt that will allow you to experiment. Line lengthTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update theLine lengthversion 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).