Split off validating full paths

The first part of a full path needs to be validated as a `top_level`
while the rest need to be validated as `wildcard`
This commit is contained in:
Bob Van Landuyt 2017-04-11 15:51:33 +02:00
parent e4f5b7ca21
commit f7511caa5f
2 changed files with 28 additions and 6 deletions

View file

@ -77,12 +77,14 @@ class NamespaceValidator < ActiveModel::EachValidator
STRICT_RESERVED = (TOP_LEVEL_ROUTES | WILDCARD_ROUTES) STRICT_RESERVED = (TOP_LEVEL_ROUTES | WILDCARD_ROUTES)
def self.valid_full_path?(full_path) def self.valid_full_path?(full_path)
pieces = full_path.split('/') path_segments = full_path.split('/')
first_part = pieces.first root_segment = path_segments.shift
pieces.all? do |namespace|
type = first_part == namespace ? :top_level : :wildcard valid?(root_segment, type: :top_level) && valid_wildcard_segments?(path_segments)
valid?(namespace, type: type) end
end
def self.valid_wildcard_segments?(segments)
segments.all? { |segment| valid?(segment, type: :wildcard) }
end end
def self.valid?(value, type: :strict) def self.valid?(value, type: :strict)

View file

@ -81,6 +81,26 @@ describe NamespaceValidator do
end end
end end
describe '#valid_full_path' do
it "isn't valid when the top level is reserved" do
test_path = 'u/should-be-a/reserved-word'
expect(described_class.valid_full_path?(test_path)).to be(false)
end
it "isn't valid if any of the path segments is reserved" do
test_path = 'the-wildcard/wikis/is-a-reserved-path'
expect(described_class.valid_full_path?(test_path)).to be(false)
end
it "is valid if the path doen't contain reserved words" do
test_path = 'there-are/no-wildcards/in-this-path'
expect(described_class.valid_full_path?(test_path)).to be(true)
end
end
describe '#validation_type' do describe '#validation_type' do
it 'uses top level validation for groups without parent' do it 'uses top level validation for groups without parent' do
group = build(:group) group = build(:group)