Commit 62874176 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add format validation to package models

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent ae7687f8
......@@ -3,7 +3,16 @@ class Packages::MavenMetadatum < ActiveRecord::Base
belongs_to :package
validates :package, presence: true
validates :path, presence: true
validates :app_group, presence: true
validates :app_name, presence: true
validates :path,
presence: true,
format: { with: Gitlab::Regex.maven_path_regex }
validates :app_group,
presence: true,
format: { with: Gitlab::Regex.maven_app_group_regex }
validates :app_name,
presence: true,
format: { with: Gitlab::Regex.maven_app_name_regex }
end
......@@ -7,5 +7,8 @@ class Packages::Package < ActiveRecord::Base
accepts_nested_attributes_for :maven_metadatum
validates :project, presence: true
validates :name, presence: true
validates :name,
presence: true,
format: { with: Gitlab::Regex.package_name_regex }
end
......@@ -12,6 +12,22 @@ module EE
def environment_scope_regex_message
"can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', '*' and spaces"
end
def package_name_regex
@package_name_regex ||= /\A(([\w\-\.]*)\/)*([\w\-\.]*)\z/.freeze
end
def maven_path_regex
package_name_regex
end
def maven_app_name_regex
@maven_app_name_regex ||= /\A[\w\-\.]+\z/.freeze
end
def maven_app_group_regex
maven_app_name_regex
end
end
end
end
......@@ -8,5 +8,23 @@ RSpec.describe Packages::MavenMetadatum, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:package) }
describe '#app_name' do
it { is_expected.to allow_value("my-app").for(:app_name) }
it { is_expected.to_not allow_value("my/app").for(:app_name) }
it { is_expected.to_not allow_value("my(app)").for(:app_name) }
end
describe '#app_group' do
it { is_expected.to allow_value("my.domain.com").for(:app_group) }
it { is_expected.to_not allow_value("my/domain/com").for(:app_group) }
it { is_expected.to_not allow_value("my(domain)").for(:app_group) }
end
describe '#path' do
it { is_expected.to allow_value("my/domain/com/my-app").for(:path) }
it { is_expected.to allow_value("my/domain/com/my-app/1.0-SNAPSHOT").for(:path) }
it { is_expected.to_not allow_value("my(domain)com.my-app").for(:path) }
end
end
end
......@@ -9,5 +9,11 @@ RSpec.describe Packages::Package, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
describe '#name' do
it { is_expected.to allow_value("my/domain/com/my-app").for(:name) }
it { is_expected.to allow_value("my.app-11.07.2018").for(:name) }
it { is_expected.to_not allow_value("my(dom$$$ain)com.my-app").for(:name) }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment