Skip to content

Commit

Permalink
Add CustomAttribute#name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed May 1, 2018
1 parent 2d81fce commit 49a117c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/custom_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class CustomAttribute < ApplicationRecord
belongs_to :resource, :polymorphic => true
serialize :serialized_value

validates :name, :format => {:with => /\A[\p{Alpha}_][\p{Alpha}_\d\$]*\z/, :message => "must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters can be letters, underscores, digits (0-9), or dollar signs ($)"}

def value=(value)
self.serialized_value = value
self[:value] = value
Expand Down
16 changes: 16 additions & 0 deletions spec/models/custom_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@
expected = RUBY_VERSION >= "2.4.0" ? :integer : :fixnum
expect(int_custom_attribute.value_type).to eq(expected)
end

context "must have a valid name" do
let(:invalid_everywhere) { [":", " ", "-"] }
let(:valid_everywhere) { ["a", "_", "á"] }
let(:valid_subsequent) { ["1", "$"] }

it "has a valid starting character" do
valid_everywhere.each { |char| expect(CustomAttribute.new(:name => "#{char}zzzzz")).to be_valid }
(invalid_everywhere + valid_subsequent).each { |char| expect(CustomAttribute.new(:name => "#{char}zzzzz")).not_to be_valid }
end

it "has a valid subsequent characters" do
(valid_everywhere + valid_subsequent).each { |char| expect(CustomAttribute.new(:name => "a#{char}")).to be_valid }
invalid_everywhere.each { |char| expect(CustomAttribute.new(:name => "a#{char}")).not_to be_valid }
end
end
end

0 comments on commit 49a117c

Please sign in to comment.