Class: Rack::JsonSchema::RequestValidation::Validator

Inherits:
BaseRequestHandler show all
Defined in:
lib/rack/json_schema/request_validation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: nil, schema: nil, strict: nil) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • env (Hash) (defaults to: nil)

    Rack env

  • schema (JsonSchema::Schema) (defaults to: nil)

    Schema object

  • strict (Boolean) (defaults to: nil)

    Strict mode or not (default: true)



31
32
33
34
35
# File 'lib/rack/json_schema/request_validation.rb', line 31

def initialize(env: nil, schema: nil, strict: nil)
  @env = env
  @schema = schema
  @strict = strict
end

Class Method Details

.call(**args) ⇒ Object

Utility wrapper method



24
25
26
# File 'lib/rack/json_schema/request_validation.rb', line 24

def self.call(**args)
  new(**args).call
end

Instance Method Details

#callObject

Raises an error if any error detected



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/json_schema/request_validation.rb', line 39

def call
  if has_link_for_current_action?
    if has_body? && !has_valid_content_type?
      if strict?
        raise InvalidContentType
      end
    else
      case
      when content_type_json? && has_body? && !has_valid_json?
        raise InvalidJson
      when content_type_json? && has_schema? && !has_valid_parameter?
        raise InvalidParameter, "Invalid request.\n#{schema_validation_error_message}"
      end
    end
  elsif strict?
    raise LinkNotFound, "Could not find the link definition for request path #{path}."
  end
end