<commit_before>module Percheron
  module Validators
    class Container

      def initialize(container)
        @container = container
      end

      def valid?
        message = rules.return { |rule| send(rule) }

        if message
          fail Errors::ContainerInvalid, formatted_message(message)
        else
          true
        end
      end

      private

        attr_reader :container

        def formatted_message(message)
          if container.name
            "Container config for '%s' is invalid: %s" % [ container.name, message ]
          else
            "Container config is invalid: #{message}"
          end
        end

        def rules
          [
            :validate_name,
            :validate_version,
            :validate_dockerfile
          ]
        end

        def validate_name
          'Container name is invalid' if container.name.nil? || !container.name.to_s.match(/[\w\d]{3,}/)
        end

        def validate_version
          container.version
          nil
        rescue ArgumentError
          'Container version is invalid'
        end

        def validate_dockerfile
          'Container Dockerfile is invalid' if container.dockerfile.nil? || !File.exist?(container.dockerfile)
        end
    end
  end
end<commit_msg>When validating a container, ensure dockerfile / image_name is supported<commit_after>module Percheron
  module Validators
    class Container

      def initialize(container)
        @container = container
      end

      def valid?
        message = rules.return { |rule| send(rule) }

        if message
          fail Errors::ContainerInvalid, formatted_message(message)
        else
          true
        end
      end

      private

        attr_reader :container

        def formatted_message(message)
          if container.name
            "Container config for '%s' is invalid: %s" % [ container.name, message ]
          else
            "Container config is invalid: #{message}"
          end
        end

        def rules
          [
            :validate_name,
            :validate_dockerfile_and_image_name,
            :validate_dockerfile,
            :validate_image,
            :validate_version
          ]
        end

        def validate_name
          'Container name is invalid' if container.name.nil? || !container.name.to_s.match(/[\w]{3,}/)
        end

        def validate_dockerfile_and_image_name
          'Container Dockerfile OR image name not provided' if container.dockerfile.nil? && container.docker_image.nil?
        end

        def validate_dockerfile
          'Container Dockerfile is invalid' if !container.dockerfile.nil? && !File.exist?(container.dockerfile)
        end

        def validate_image
          'Container Docker image is invalid' if !container.docker_image.nil? && !container.docker_image.match(/^.+:.+$/)
        end

        def validate_version
          container.version ? nil : fail(ArgumentError)
        rescue ArgumentError
          'Container version is invalid'
        end

    end
  end
end
{'fraction_non_alphanumeric': 0.07140517523747134, 'fraction_numerical': 0.0006550933508024893, 'mean_word_length': 1.7967032967032968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '394467', 'n_tokens_mistral': 801, 'n_tokens_neox': 757, 'n_words': 214}