Sha256: 493f8277a2a4ed890429bf01d57b8898947d427374af2081788378db071f84e4

Contents?: true

Size: 1.85 KB

Versions: 97

Compression:

Stored size: 1.85 KB

Contents

require "uri"
require "pathname"

module CC
  class Config
    module Validation
      class FetchValidator
        include HashValidations

        attr_reader :errors, :warnings

        def initialize(data)
          @data = data

          @errors = []
          @warnings = []

          validate
        end

        private

        attr_reader :data

        def validate
          if data.is_a?(String)
            validate_url(data)
          elsif data.is_a?(Hash)
            validate_fetch_hash
          else
            errors << "fetch section should be a string or a hash"
          end
        end

        def validate_url(url)
          unless valid_url?(url)
            errors << "fetch section: invalid URL '#{url}'"
          end
        end

        def valid_url?(url)
          uri = URI.parse(url)
          uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
        rescue URI::InvalidURIError
          false
        end

        def validate_fetch_hash
          if !data.key?("path") || !data.key?("url")
            errors << "fetch section must include 'url' & 'path'"
          end

          validate_key_type("path", String)
          validate_key_type("url", String)

          validate_path(data["path"])
          validate_url(data["url"])

          warn_unrecognized_keys(%w[path url])
        end

        def validate_path(path)
          if path.nil? || path.length.zero?
            errors << "fetch section's 'path' cannot be empty"
          else
            pathname = Pathname.new(path)
            if pathname.absolute?
              errors << "fetch section: absolute path '#{path}' is invalid"
            end
            if pathname.cleanpath.to_s != pathname.to_s || path.include?("..")
              errors << "fetch section: relative path elements in '#{path}' are invalid"
            end
          end
        end
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
codeclimate-0.78.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.77.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.76.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.75.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.74.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.73.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.72.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.71.2 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.71.1 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.71.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.6 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.5 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.4 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.3 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.2 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.1 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.70.0 lib/cc/config/validation/fetch_validator.rb