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.96.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.95.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.94.1 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.94.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.93.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.92.1 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.92.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.91.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.90.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.89.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.88.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.5 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.4 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.3 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.2 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.1 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.87.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.86.0 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.85.29 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.85.28 lib/cc/config/validation/fetch_validator.rb