Class: Gitlab::Triage::ParamsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/validators/params_validator.rb

Direct Known Subclasses

LimiterValidator

Constant Summary collapse

InvalidParameter =
Class.new(ArgumentError)

Instance Method Summary collapse

Constructor Details

#initialize(parameter_definitions, value) ⇒ ParamsValidator

Returns a new instance of ParamsValidator.



6
7
8
9
# File 'lib/gitlab/triage/validators/params_validator.rb', line 6

def initialize(parameter_definitions, value)
  @parameter_definitions = parameter_definitions
  @value = value
end

Instance Method Details

#validate!Object



11
12
13
14
15
# File 'lib/gitlab/triage/validators/params_validator.rb', line 11

def validate!
  validate_required_parameters(@value)
  validate_parameter_types(@value)
  validate_parameter_content(@value)
end

#validate_parameter_content(value) ⇒ Object (private)



34
35
36
37
38
39
40
# File 'lib/gitlab/triage/validators/params_validator.rb', line 34

def validate_parameter_content(value)
  @parameter_definitions.each do |param|
    if param[:values]
      raise InvalidParameter, "#{param[:name]} must be one of #{param[:values].join(',')}" unless param[:values].include?(value[param[:name]])
    end
  end
end

#validate_parameter_types(value) ⇒ Object (private)



25
26
27
28
29
30
31
32
# File 'lib/gitlab/triage/validators/params_validator.rb', line 25

def validate_parameter_types(value)
  @parameter_definitions.each do |param|
    if value.has_key?(param[:name])
      param_types = Array(param[:type]).flatten
      raise InvalidParameter, "#{param[:name]} must be of type #{param[:type]}" unless param_types.any? { |type| value[param[:name]].is_a?(type) }
    end
  end
end

#validate_required_parameters(value) ⇒ Object (private)



19
20
21
22
23
# File 'lib/gitlab/triage/validators/params_validator.rb', line 19

def validate_required_parameters(value)
  @parameter_definitions.each do |param|
    raise InvalidParameter, "#{param[:name]} is a required parameter" unless value[param[:name]]
  end
end