Sha256: 3c3f7f01fb3d09154dd47c050edbe7392c5c17127f49da6d8c204c4c95ad3671

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

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|
          if param[:values]
            raise InvalidParameter, "#{param[:name]} must be of one of #{param[:values].join(',')}" unless param[:values].include?(value[param[:name]])
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gitlab-triage-1.14.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.14.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.13.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.12.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.11.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.10.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.10.0 lib/gitlab/triage/validators/params_validator.rb