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.85.27 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.85.26 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.85.25 lib/cc/config/validation/fetch_validator.rb
codeclimate-0.85.24 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.54 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.53 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.52 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.51 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.50 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.49 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.48 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.47 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.46 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.45 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.44 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.43 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.42 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.41 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.40 lib/cc/config/validation/fetch_validator.rb
codeclimate-fede-0.85.39 lib/cc/config/validation/fetch_validator.rb