allow guid to be nilable
This commit is contained in:
parent
cff74982bf
commit
cf45416344
2 changed files with 51 additions and 10 deletions
|
|
@ -7,6 +7,21 @@ module Validation
|
||||||
# * Numbers: 0-9
|
# * Numbers: 0-9
|
||||||
# * Special chars: '-', '_', '@', '.' and ':'
|
# * Special chars: '-', '_', '@', '.' and ':'
|
||||||
class Guid
|
class Guid
|
||||||
|
# This rule can have a +nilable+ param
|
||||||
|
# @return [Hash] params
|
||||||
|
attr_reader :params
|
||||||
|
|
||||||
|
# create a new rule for guid validation
|
||||||
|
# @param [Hash] params
|
||||||
|
# @option params [Boolean] :nilable guid allowed to be nil
|
||||||
|
def initialize(params={})
|
||||||
|
if params.include?(:nilable) && !params[:nilable].is_a?(TrueClass) && !params[:nilable].is_a?(FalseClass)
|
||||||
|
raise ArgumentError, ":nilable needs to be a boolean"
|
||||||
|
end
|
||||||
|
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
# The error key for this rule
|
# The error key for this rule
|
||||||
# @return [Symbol] error key
|
# @return [Symbol] error key
|
||||||
def error_key
|
def error_key
|
||||||
|
|
@ -15,13 +30,7 @@ module Validation
|
||||||
|
|
||||||
# Determines if value is a valid +GUID+
|
# Determines if value is a valid +GUID+
|
||||||
def valid_value?(value)
|
def valid_value?(value)
|
||||||
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/
|
params[:nilable] && value.nil? || value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,}\z/
|
||||||
end
|
|
||||||
|
|
||||||
# This rule has no params
|
|
||||||
# @return [Hash] params
|
|
||||||
def params
|
|
||||||
{}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,25 @@
|
||||||
describe Validation::Rule::Guid do
|
describe Validation::Rule::Guid do
|
||||||
it "will not accept parameters" do
|
it "allows a nilable parameter" do
|
||||||
validator = Validation::Validator.new({})
|
validator = Validation::Validator.new({})
|
||||||
expect {
|
expect {
|
||||||
validator.rule(:guid, guid: {param: true})
|
validator.rule(:guid, guid: {nilable: false})
|
||||||
}.to raise_error ArgumentError
|
}.not_to raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't require a nilable parameter" do
|
||||||
|
validator = Validation::Validator.new({})
|
||||||
|
expect {
|
||||||
|
validator.rule(:guid, :guid)
|
||||||
|
}.not_to raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it "requires a boolean as parameter" do
|
||||||
|
validator = Validation::Validator.new({})
|
||||||
|
[nil, "", 42, 5.5].each do |val|
|
||||||
|
expect {
|
||||||
|
validator.rule(:guid, guid: {nilable: val})
|
||||||
|
}.to raise_error ArgumentError, ":nilable needs to be a boolean"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "has an error key" do
|
it "has an error key" do
|
||||||
|
|
@ -28,6 +44,14 @@ describe Validation::Rule::Guid do
|
||||||
expect(validator.errors).to be_empty
|
expect(validator.errors).to be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "validates a nil guid if nilable is true" do
|
||||||
|
validator = Validation::Validator.new(OpenStruct.new(guid: nil))
|
||||||
|
validator.rule(:guid, guid: {nilable: true})
|
||||||
|
|
||||||
|
expect(validator).to be_valid
|
||||||
|
expect(validator.errors).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
it "fails if the string is too short" do
|
it "fails if the string is too short" do
|
||||||
validator = Validation::Validator.new(OpenStruct.new(guid: "012345"))
|
validator = Validation::Validator.new(OpenStruct.new(guid: "012345"))
|
||||||
validator.rule(:guid, :guid)
|
validator.rule(:guid, :guid)
|
||||||
|
|
@ -44,6 +68,14 @@ describe Validation::Rule::Guid do
|
||||||
expect(validator.errors).to include(:guid)
|
expect(validator.errors).to include(:guid)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "fails if the string contains invalid chars if nilable is true" do
|
||||||
|
validator = Validation::Validator.new(OpenStruct.new(guid: "ghijklmnopqrstuvwxyz++"))
|
||||||
|
validator.rule(:guid, guid: {nilable: true})
|
||||||
|
|
||||||
|
expect(validator).not_to be_valid
|
||||||
|
expect(validator.errors).to include(:guid)
|
||||||
|
end
|
||||||
|
|
||||||
it "fails if the string is empty" do
|
it "fails if the string is empty" do
|
||||||
[nil, ""].each do |val|
|
[nil, ""].each do |val|
|
||||||
validator = Validation::Validator.new(OpenStruct.new(guid: val))
|
validator = Validation::Validator.new(OpenStruct.new(guid: val))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue