class Rack::Spec::RequestValidation::Validator

Public Class Methods

call(**args) click to toggle source

Utility wrapper method

# File lib/rack/spec/request_validation.rb, line 22
def self.call(**args)
  new(**args).call
end
new(env: nil, schema: nil) click to toggle source

@param env [Hash] Rack env @param schema [JsonSchema::Schema] Schema object

# File lib/rack/spec/request_validation.rb, line 28
def initialize(env: nil, schema: nil)
  @env = env
  @schema = schema
end

Public Instance Methods

call() click to toggle source

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

# File lib/rack/spec/request_validation.rb, line 35
def call
  case
  when !has_link_for_current_action?
    raise LinkNotFound
  when has_body? && !has_valid_content_type?
    raise InvalidContentType
  when has_body? && !has_valid_json?
    raise InvalidJson
  when has_body? && has_schema? && !has_valid_parameter?
    raise InvalidParameter, "Invalid request.\n#{schema_validation_error_message}"
  end
end

Private Instance Methods

body() click to toggle source

@return [String] request body

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

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

# File lib/rack/spec/request_validation.rb, line 68
def has_body?
  !body.empty?
end
has_schema?() click to toggle source

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

# File lib/rack/spec/request_validation.rb, line 63
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/spec/request_validation.rb, line 73
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/spec/request_validation.rb, line 50
def has_valid_json?
  parameters
  true
rescue MultiJson::ParseError
  false
end
has_valid_parameter?() click to toggle source

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

# File lib/rack/spec/request_validation.rb, line 58
def has_valid_parameter?
  schema_validation_result[0]
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/spec/request_validation.rb, line 95
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 [MultiJson::ParseError]

# File lib/rack/spec/request_validation.rb, line 112
def parameters
  @parameters ||= begin
    if has_body?
      MultiJson.decode(body)
    else
      {}
    end
  end
end
schema_validation_error_message() click to toggle source

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

# File lib/rack/spec/request_validation.rb, line 88
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/spec/request_validation.rb, line 83
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/spec/request_validation.rb, line 78
def schema_validation_result
  @schema_validation_result ||= link.schema.validate(parameters)
end