Sha256: c32d44632e5c5518cb8170d0096cd632b8598ac4a9e78426ca34542a0ed252f2

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Gitlab
  module Triage
    class ParamsValidator
      InvalidParameter = Class.new(ArgumentError)

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

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

      private

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

      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

      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gitlab-triage-1.44.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.44.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.43.2 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.43.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.43.0 lib/gitlab/triage/validators/params_validator.rb