Skip to content
❗️ Response validations according to a media-type
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
bin
lib/media_types
test
.gitignore
.travis.yml
CHANGELOG.md
CODE_OF_CONDUCT.md
Gemfile
Gemfile.lock
LICENSE.txt
README.md
Rakefile
media_types-validation.gemspec

README.md

MediaTypes::Validation

Build Status: master Gem Version MIT license

Add controller time validation for media types and react accordingly.

Installation

Add this line to your application's Gemfile:

gem 'media_types-validation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install media_types-validation

Usage

If you add the MediaTypes::Validation controller concern, validate_json_with_media_type becomes available during actions. This does not validate only JSON output, but stringifies and then parses the body as JSON, so the limitations of JSON apply. This step is necessary in order to make sure rails types and others are first correctly casted (and formatted).

require 'media_types/validation'

class ApiController < ActionController::API
  include MediaTypes::Validation
  
  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
    # -> 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

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:

MediaTypes::Validation.configure do
  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
  
  # Or alternatively you can always raise
  self.raise_on_json_invalid_media = true
end

Related

  • MediaTypes: 💎 Library to create media type definitions, schemes and validations
  • MediaTypes::Deserialization: 🌀 Add media types supported deserialization using your favourite parser, and media type validation.
  • MediaTypes::Serialization: 🌀 Add media types supported serialization using your favourite serializer

Development

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.

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 XPBytes/media_types-validation.

You can’t perform that action at this time.