Add controller time validation for media types and react accordingly.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install media_types-validation
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
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.
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
💎Library to create media type definitions, schemes and validations
🌀Add media types supported deserialization using your favourite parser, and media type validation.
🌀Add media types supported serialization using your favourite serializer
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake test to run the tests. You can
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.
Bug reports and pull requests are welcome on GitHub at XPBytes/media_types-validation.