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.



8
9
10
11
# File 'lib/gitlab/triage/validators/params_validator.rb', line 8

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

Instance Method Details

#validate!Object



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

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

#validate_parameter_content(value) ⇒ Object (private)



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

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

#validate_parameter_types(value) ⇒ Object (private)



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

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)



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

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