lib/percheron/validators/container.rb in percheron-0.6.4 vs lib/percheron/validators/container.rb in percheron-0.7.0

- old
+ new

@@ -8,11 +8,11 @@ def valid? message = rules.return { |rule| send(rule) } if message - raise Errors::ContainerInvalid.new(formatted_message(message)) + fail Errors::ContainerInvalid, formatted_message(message) else true end end @@ -22,34 +22,54 @@ def formatted_message(message) if container.name "Container config for '%s' is invalid: %s" % [ container.name, message ] else - "Container config is invalid: %s" % [ message ] + "Container config is invalid: #{message}" end end def rules [ :validate_name, - :validate_version, - :validate_dockerfile + :validate_dockerfile_and_image_name, + :validate_dockerfile, + :validate_image, + :validate_version ] end + # rubocop:disable Style/GuardClause def validate_name - 'Name is invalid' if container.name.nil? || !container.name.to_s.match(/[\w\d]{3,}/) + if container.name.nil? || !container.name.to_s.match(/[\w]{3,}/) + 'Container name is invalid' + end end - def validate_version - container.version - nil - rescue ArgumentError - 'Version is invalid' + def validate_dockerfile_and_image_name + if container.dockerfile.nil? && container.docker_image.nil? + 'Container Dockerfile OR image name not provided' + end end def validate_dockerfile - 'Dockerfile is invalid' if container.dockerfile.nil? || !File.exist?(container.dockerfile) + if !container.dockerfile.nil? && !File.exist?(container.dockerfile) + 'Container Dockerfile is invalid' + end end + + def validate_image + if !container.docker_image.nil? && !container.docker_image.match(/^.+:.+$/) + 'Container Docker image is invalid' + end + end + # rubocop:enable Style/GuardClause + + def validate_version + container.version ? nil : fail(ArgumentError) + rescue ArgumentError + 'Container version is invalid' + end + end end end