class Rack::JsonSchema::RequestValidation::Validator

Public Class Methods

call(**args) click to toggle source

Utility wrapper method

# File lib/rack/json_schema/request_validation.rb, line 31
def self.call(**args)
  new(**args).call
end
new(env: nil, ignore_invalid_content_type: false, ignore_missing_path: false, schema: nil) click to toggle source

@param env [Hash] Rack env @param ignore_invalid_content_type [false, true] @param ignore_missing_path [false, true] @param schema [JsonSchema::Schema] Schema object

# File lib/rack/json_schema/request_validation.rb, line 39
def initialize(env: nil, ignore_invalid_content_type: false, ignore_missing_path: false, schema: nil)
  @env = env
  @ignore_invalid_content_type = ignore_invalid_content_type
  @ignore_missing_path = ignore_missing_path
  @schema = schema
end

Public Instance Methods

call() click to toggle source

Raises an error if any error detected @raise [Rack::JsonSchema::RequestValidation::Error]

# File lib/rack/json_schema/request_validation.rb, line 48
def call
  if has_link_for_current_action?
    if has_body? && !has_valid_content_type?
      unless ignore_invalid_content_type?
        raise InvalidContentType
      end
    else
      case
      when content_type_json? && has_body? && !has_valid_json?
        raise InvalidJson
      when content_type_json? && has_schema? && !has_hash_request_body?
        raise InvalidParameter, "Invalid request. Request body must be an Object in JSON."
      when content_type_json? && has_schema? && !has_valid_parameter?
        raise InvalidParameter, "Invalid request.\n#{schema_validation_error_message}"
      end
    end
  elsif !ignore_missing_path?
    raise LinkNotFound, "Could not find the link definition for request path #{path}."
  end
end

Private Instance Methods

body() click to toggle source

@return [String] request body

# File lib/rack/json_schema/request_validation.rb, line 140
def body
  if instance_variable_defined?(:@body)
    @body
  else
    @body = request.body.read
    request.body.rewind
    @body
  end
end
content_type_json?() click to toggle source

@return [true, false] True if the current link supports json format

# File lib/rack/json_schema/request_validation.rb, line 113
def content_type_json?
  Rack::Mime.match?(link.enc_type, "application/json")
end
has_body?() click to toggle source

@return [true, false] True if request body is not empty

# File lib/rack/json_schema/request_validation.rb, line 93
def has_body?
  !body.empty?
end
has_hash_request_body?() click to toggle source
# File lib/rack/json_schema/request_validation.rb, line 71
def has_hash_request_body?
  parameters_from_body.is_a?(Hash)
end
has_schema?() click to toggle source

@return [true, false] True if any schema is defined for the current action

# File lib/rack/json_schema/request_validation.rb, line 88
def has_schema?
  !!link.schema
end
has_valid_content_type?() click to toggle source

@return [true, false] True if no or matched content type given

# File lib/rack/json_schema/request_validation.rb, line 98
def has_valid_content_type?
  mime_type.nil? || Rack::Mime.match?(link.enc_type, mime_type)
end
has_valid_json?() click to toggle source
# File lib/rack/json_schema/request_validation.rb, line 75
def has_valid_json?
  parsed_body
  true
rescue JSON::JSONError
  false
end
has_valid_parameter?() click to toggle source

@return [true, false] True if request parameters are all valid

# File lib/rack/json_schema/request_validation.rb, line 83
def has_valid_parameter?
  !!schema_validation_result[0]
end
ignore_invalid_content_type?() click to toggle source

@return [false, true]

# File lib/rack/json_schema/request_validation.rb, line 103
def ignore_invalid_content_type?
  !!@ignore_invalid_content_type
end
ignore_missing_path?() click to toggle source

@return [false, true]

# File lib/rack/json_schema/request_validation.rb, line 108
def ignore_missing_path?
  !!@ignore_missing_path
end
mime_type() click to toggle source

@return [String, nil] Request MIME Type specified in Content-Type header field @example

mime_type #=> "application/json"
# File lib/rack/json_schema/request_validation.rb, line 135
def mime_type
  request.content_type.split(";").first if request.content_type
end
parameters() click to toggle source

@return [Hash] Request parameters decoded from JSON @raise [JSON::JSONError]

# File lib/rack/json_schema/request_validation.rb, line 152
def parameters
  @parameters ||= parameters_from_query.merge(parameters_from_body)
end
parameters_from_body() click to toggle source

@return [Hash] Request parameters decoded from JSON @raise [JSON::JSONError]

# File lib/rack/json_schema/request_validation.rb, line 158
def parameters_from_body
  if has_body?
    parsed_body
  else
    {}
  end
end
parameters_from_query() click to toggle source

@return [Hash] Request parameters extracted from URI query

# File lib/rack/json_schema/request_validation.rb, line 167
def parameters_from_query
  request.GET
end
parsed_body() click to toggle source
# File lib/rack/json_schema/request_validation.rb, line 171
def parsed_body
  @parsed_body ||= JSON.parse(body)
end
schema_validation_error_message() click to toggle source

@return [String] Joined error message to the result of schema validation

# File lib/rack/json_schema/request_validation.rb, line 128
def schema_validation_error_message
  ::JsonSchema::SchemaError.aggregate(schema_validation_errors).join("\n")
end
schema_validation_errors() click to toggle source

@return [Array] Errors of schema validation

# File lib/rack/json_schema/request_validation.rb, line 123
def schema_validation_errors
  schema_validation_result[1]
end
schema_validation_result() click to toggle source

@return [Array] A result of schema validation for the current action

# File lib/rack/json_schema/request_validation.rb, line 118
def schema_validation_result
  @schema_validation_result ||= link.schema.validate(parameters)
end