Extract allowed chars for GUIDs to constant

Also: disallow special chars at the end of a GUID
This commit is contained in:
Benjamin Neff 2017-09-01 02:33:21 +02:00
parent 9d72c9855a
commit faf48e1dd4
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
3 changed files with 15 additions and 1 deletions

View file

@ -31,6 +31,8 @@ A network-wide, unique identifier. A random string of at least 16 and at most 25
* Numbers: `0-9`
* Special chars: `-`, `_`, `@`, `.` and `:`
Special chars aren't allowed at the end.
Example: `298962a0b8dc0133e40d406c8f31e210`
## String

View file

@ -6,7 +6,11 @@ module Validation
# * Letters: a-z
# * Numbers: 0-9
# * Special chars: '-', '_', '@', '.' and ':'
# Special chars aren't allowed at the end.
class Guid
# Allowed chars to validate a GUID with a regex
VALID_CHARS = "[0-9A-Za-z\\-_@.:]{15,254}[0-9a-z]".freeze
# The error key for this rule
# @return [Symbol] error key
def error_key
@ -15,7 +19,7 @@ module Validation
# Determines if value is a valid +GUID+
def valid_value?(value)
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,255}\z/
value.is_a?(String) && value =~ /\A#{VALID_CHARS}\z/
end
# This rule has no params.

View file

@ -45,6 +45,14 @@ describe Validation::Rule::Guid do
expect(validator.errors).to include(:guid)
end
it "fails if the string contains special chars at the end" do
validator = Validation::Validator.new(OpenStruct.new(guid: "abcdef0123456789."))
validator.rule(:guid, :guid)
expect(validator).not_to be_valid
expect(validator.errors).to include(:guid)
end
it "fails if the string contains invalid chars" do
validator = Validation::Validator.new(OpenStruct.new(guid: "ghijklmnopqrstuvwxyz++"))
validator.rule(:guid, :guid)