Class: Committee::SchemaValidator::HyperSchema::ResponseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/schema_validator/hyper_schema/response_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(link, options = {}) ⇒ ResponseValidator

Returns a new instance of ResponseValidator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/committee/schema_validator/hyper_schema/response_validator.rb', line 9

def initialize(link, options = {})
  @link = link
  @validate_success_only = options[:validate_success_only]
  @allow_blank_structures = options[:allow_blank_structures]

  @validator = nil
  @validators = {}
  if link.is_a? Drivers::OpenAPI2::Link
    link.target_schemas.each do |status, schema|
      @validators[status] = JsonSchema::Validator.new(target_schema(link))
    end
  else
    @validator = JsonSchema::Validator.new(target_schema(link))
  end
end

Instance Attribute Details

#allow_blank_structuresObject (readonly)

Returns the value of attribute allow_blank_structures.



7
8
9
# File 'lib/committee/schema_validator/hyper_schema/response_validator.rb', line 7

def allow_blank_structures
  @allow_blank_structures
end

#validate_success_onlyObject (readonly)

Returns the value of attribute validate_success_only.



7
8
9
# File 'lib/committee/schema_validator/hyper_schema/response_validator.rb', line 7

def validate_success_only
  @validate_success_only
end

Instance Method Details

#call(status, headers, data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/committee/schema_validator/hyper_schema/response_validator.rb', line 25

def call(status, headers, data)
  unless [204, 304].include?(status) # 204 No Content or 304 Not Modified
    response = Rack::Response.new(data, status, headers)
    check_content_type!(response)
  end

  # List is a special case; expect data in an array.
  #
  # This is poor form that's here so as not to introduce breaking behavior.
  # The "instances" value of "rel" is a Heroku-ism and was originally
  # introduced before we understood how to use "targetSchema". It's not
  # meaningful with the context of the hyper-schema specification and
  # should be eventually be removed.
  if legacy_hyper_schema_rel?(@link)
    if !data.is_a?(Array)
      raise InvalidResponse, "List endpoints must return an array of objects."
    end

    # only consider the first object during the validation from here on
    # (but only in cases where `targetSchema` is not set)
    data = data[0]

    # if the array was empty, allow it through
    return if data == nil
  end

  if allow_blank_structures && @link.is_a?(Committee::Drivers::OpenAPI2::Link) && !@link.target_schema
    return if data.nil?
  end

  begin
    if Committee::Middleware::ResponseValidation.validate?(status, validate_success_only)
      if @link.is_a?(Drivers::OpenAPI2::Link)
        raise InvalidResponse, "Invalid response.#{@link.href} status code #{status} definition does not exist" if @validators[status].nil?
        if !@validators[status].validate(data)
          errors = JsonSchema::SchemaError.aggregate(@validators[status].errors).join("\n")
          raise InvalidResponse, "Invalid response.\n\n#{errors}"
        end
      else
        if !@validator.validate(data)
          errors = JsonSchema::SchemaError.aggregate(@validator.errors).join("\n")
          raise InvalidResponse, "Invalid response.\n\n#{errors}"
        end
      end
    end
  rescue => e
    raise InvalidResponse, "Invalid response.\n\nschema is undefined" if /undefined method .all_of. for nil/ =~ e.message
    raise e
  end
end