Sha256: 1412260b2191fad3280844cf5679a7bf900b888c0fc11eb676d7e11f888bd39d

Contents?: true

Size: 1.25 KB

Versions: 29

Compression:

Stored size: 1.25 KB

Contents

module Gitlab
  module Triage
    class ParamsValidator
      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 ArgumentError, "#{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 ArgumentError, "#{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 ArgumentError, "#{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

29 entries across 29 versions & 1 rubygems

Version Path
gitlab-triage-1.9.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.8.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.7.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.7.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.6.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.6.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.5.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.5.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.4.2 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.4.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.4.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.3.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.2.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.1.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.1.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.0.1 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-1.0.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-0.17.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-0.16.0 lib/gitlab/triage/validators/params_validator.rb
gitlab-triage-0.15.0 lib/gitlab/triage/validators/params_validator.rb