Remove nilable parameter from guid rule and use OptionalAwareValidator

This commit is contained in:
Benjamin Neff 2017-09-03 19:19:16 +02:00
parent 860aec96b9
commit b2eda1d812
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
5 changed files with 13 additions and 54 deletions

View file

@ -16,7 +16,7 @@ module DiasporaFederation
# Guid of the original post
# @see StatusMessage#guid
# @return [String] root guid
property :root_guid, :string
property :root_guid, :string, optional: true
# @!attribute [r] public
# Has no meaning at the moment

View file

@ -14,7 +14,7 @@ module DiasporaFederation
rule :remote_photo_name, :not_empty
rule :status_message_guid, guid: {nilable: true}
rule :status_message_guid, :guid
rule :text, length: {maximum: 65_535}

View file

@ -6,7 +6,7 @@ module DiasporaFederation
rule :root_author, :diaspora_id
rule :root_guid, guid: {nilable: true}
rule :root_guid, :guid
rule :author, %i[not_empty diaspora_id]

View file

@ -7,21 +7,6 @@ module Validation
# * Numbers: 0-9
# * Special chars: '-', '_', '@', '.' and ':'
class Guid
# This rule can have a +nilable+ param.
# @return [Hash] params
attr_reader :params
# Creates 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
# @return [Symbol] error key
def error_key
@ -30,7 +15,13 @@ module Validation
# Determines if value is a valid +GUID+
def valid_value?(value)
params[:nilable] && value.nil? || value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,255}\z/
value.is_a?(String) && value.downcase =~ /\A[0-9a-z\-_@.:]{16,255}\z/
end
# This rule has no params.
# @return [Hash] params
def params
{}
end
end
end

View file

@ -1,25 +1,9 @@
describe Validation::Rule::Guid do
it "allows a nilable parameter" do
it "will not accept parameters" do
validator = Validation::Validator.new({})
expect {
validator.rule(:guid, guid: {nilable: false})
}.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
validator.rule(:guid, guid: {param: true})
}.to raise_error ArgumentError
end
it "has an error key" do
@ -45,14 +29,6 @@ describe Validation::Rule::Guid do
expect(validator.errors).to be_empty
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
validator = Validation::Validator.new(OpenStruct.new(guid: "012345"))
validator.rule(:guid, :guid)
@ -77,14 +53,6 @@ describe Validation::Rule::Guid do
expect(validator.errors).to include(:guid)
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
[nil, ""].each do |val|
validator = Validation::Validator.new(OpenStruct.new(guid: val))