Sha256: 741d29790688a94a5db5e8c6dc57b568f551e097ccc27f9f7505c25ec90e9c2b

Contents?: true

Size: 925 Bytes

Versions: 6

Compression:

Stored size: 925 Bytes

Contents

module JsonSchemaRails
  module Helpers
    extend ActiveSupport::Concern

    included do
      rescue_from JsonSchemaRails::ValidationError, with: :schema_validation_failed
    end

    module ClassMethods
      def validate_schema(options = {})
        before_filter :validate_schema!, options
      end
    end

    def validate_schema(schema_name = nil)
      validate_schema!(schema_name)
    rescue JsonSchemaRails::ValidationError
      false
    end

    def validate_schema!(schema_name = nil)
      schema_name ||= [
        "#{controller_path}/#{action_name}",
        "#{request.method}#{request.path}"
      ]
      ::JsonSchemaRails.validate!(schema_name, params)
      true
    end

    def schema_validation_failed(exception = nil)
      if exception
        raise exception if Rails.env.development?
        logger.debug exception.message
      end
      render nothing: true, status: 400
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
json_schema_rails-0.2.1 lib/json_schema_rails/helpers.rb
json_schema_rails-0.2.0 lib/json_schema_rails/helpers.rb
json_schema_rails-0.1.0 lib/json_schema_rails/helpers.rb
json_schema_rails-0.0.3 lib/json_schema_rails/helpers.rb
json_schema_rails-0.0.2 lib/json_schema_rails/helpers.rb
json_schema_rails-0.0.1 lib/json_schema_rails/helpers.rb