Skip to content

Commit

Permalink
When build is present, add a validation for service names to not incl…
Browse files Browse the repository at this point in the history
…ude caps.
  • Loading branch information
rupakg committed Jul 20, 2015
1 parent 2feb39d commit cdcb4d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/models/compose_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ def validate_expose_format(value, path, errors)

def validate_service_name(value, path, errors)
regex = /^[a-zA-Z0-9]*$/
regex_with_build = /^[a-z0-9]*$/
service_name = path[0]
unless service_name =~ regex
errors << Kwalify::ValidationError.new('Invalid service name. Valid characters are [a-zA-Z0-9]', path)
if value.include?('build')
unless service_name =~ regex_with_build
errors << Kwalify::ValidationError.new('Invalid service name. Valid characters are [a-z0-9]', path)
end
else
unless service_name =~ regex
errors << Kwalify::ValidationError.new('Invalid service name. Valid characters are [a-zA-Z0-9]', path)
end
end
end
end
Expand Down
21 changes: 18 additions & 3 deletions spec/models/compose_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@
expect(errors.first.message).to eq ('Invalid service name. Valid characters are [a-zA-Z0-9]')
end

it 'adds an error if service name format is invalid' do
it 'does not add an error if service name format is valid' do
subject.validate_hook({'image' => 'foo'}, rule, ["Drupal728"], errors)
expect(errors).to be_empty
end

it 'adds an error if service name format has caps, when build is present' do
subject.validate_hook({'build' => 'foo'}, rule, ["Drupal728"], errors)
expect(errors.first.message).to eq ('Invalid service name. Valid characters are [a-z0-9]')
end

it 'does not add an error if service name format has no caps, when build is present' do
subject.validate_hook({'build' => 'foo'}, rule, ["drupal728"], errors)
expect(errors).to be_empty
end

it 'does not add an error if service name format has caps, when build is not present' do
subject.validate_hook({'image' => 'foo'}, rule, ["Drupal728"], errors)
expect(errors).to be_empty
end

it 'adds an error if image and build are both present' do
subject.validate_hook({ 'image' => 'foo', 'build' => 'foo'}, rule, path, errors)
subject.validate_hook({ 'image' => 'foo', 'build' => 'foo'}, rule, ["drupal728"], errors)
expect(errors.first.message).to eq ('service must use either image or build, not both')
end

Expand All @@ -36,7 +51,7 @@
end

it 'does not add an error if build is present without image' do
subject.validate_hook({ 'build' => 'foo'}, rule, path, errors)
subject.validate_hook({ 'build' => 'foo'}, rule, ["drupal728"], errors)
expect(errors).to be_empty
end

Expand Down

0 comments on commit cdcb4d0

Please sign in to comment.