Class: RSpec::Rails::Api::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/api/validator.rb

Overview

Set of method to validate data and data structures

Class Method Summary collapse

Class Method Details

.format_failure_message(errors, values) ⇒ String

Returns a human-readable string from matcher errors

Parameters:

  • errors (String, Hash)

    Validation errors

  • values (String)

    JSON string representing the value

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec/rails/api/validator.rb', line 62

def format_failure_message(errors, values) # rubocop:disable Metrics/MethodLength
  if errors.is_a? Hash
    errors = errors.deep_stringify_keys.to_yaml.split("\n")
    errors.shift
    errors.map! do |line|
      "  #{line.sub(/^(\s+)"(#\d+)":(.*)$/, '\1\2:\3')}"
    end
    errors = errors.join("\n")
  end

  "    expected object structure not to have these errors:\n    \#{errors}\n\n    As a notice, here is the JSON object:\n      \#{values}\n  TXT\nend\n"

.valid_type?(type, except: []) ⇒ Boolean

Checks if a given type is in the supported types list

Parameters:

  • type (Symbol)

    Type to check

  • except ([Symbol]) (defaults to: [])

    List of types to ignore

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/rspec/rails/api/validator.rb', line 88

def valid_type?(type, except: [])
  keys = PARAM_TYPES.keys.reject { |key| except.include? key }
  keys.include?(type)
end

.validate_array(array, expected) ⇒ String, ...

Validates each entry of an array

Parameters:

  • array (*)

    The array to check

  • expected (Symbol, Hash, NilClass)

    Attributes configuration

Returns:

  • (String, Hash, NilClass)

    Nil when no error, string when not an object and dictionary of errors otherwise



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/rails/api/validator.rb', line 41

def validate_array(array, expected)
  return 'is not an array' unless array.is_a? Array
  # Arrays without an expected entry type
  return unless expected

  errors = {}
  array.each_with_index do |array_entry, index|
    value_error = validate_array_entry array_entry, expected
    errors["##{index}"] = value_error if value_error
  end

  errors unless errors.keys.empty?
end

.validate_object(actual, expected) ⇒ String, ...

Validates an object keys and values types

Parameters:

  • actual (*)

    Value to compare

  • expected (Hash, NilClass)

    Definition

Returns:

  • (String, Hash, NilClass)

    Nil when no error, string when not an object and dictionary of errors otherwise



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/rails/api/validator.rb', line 20

def validate_object(actual, expected)
  return 'is not a hash' unless actual.is_a? Hash
  # Don't validate without a definition
  return unless expected

  keys_errors = validate_object_keys actual, expected
  return keys_errors unless keys_errors.nil?

  attributes_errors = validate_object_attributes(actual, expected)

  attributes_errors unless attributes_errors.keys.empty?
end

.validate_type(value, type) ⇒ String, NilClass

Checks if a value is of the given type

Parameters:

  • value (*)

    Value to test

  • type (Symbol)

    Type to compare to

Returns:

  • (String, NilClass)

    True when the value corresponds to the given type



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rspec/rails/api/validator.rb', line 100

def validate_type(value, type) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
  if type == :any
    return 'is null' if value.nil?

    return
  end

  if type == :boolean
    return nil if value.is_a?(TrueClass) || value.is_a?(FalseClass)

    return 'is not a "boolean"'
  end

  raise "Unknown type #{type}" unless PARAM_TYPES.key? type

  return nil if value.is_a? PARAM_TYPES[type][:class]

  "is not a \"#{type}\""
end