Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
[dm-validations] Return unique set of validation errors
Browse files Browse the repository at this point in the history
 * Original idea was to not take options into account
   in GenericValidator#==, because they often do not matter

 * It did not work out well: when
   you enable inferred length validation for string, but want to                                                                                                                                           later explicitly declare one with Range, both should be added
   or explicit one won't work

 * Given possiblility of annoying duplicates in error messages because
   of "duplicated" validations, we have to "quick fix" it by simply
   returning an unique set

[#671 state:resolved]
  • Loading branch information
michaelklishin committed Mar 26, 2009
1 parent ef96417 commit 21c8b6d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dm-validations/lib/dm-validations/validation_errors.rb
Expand Up @@ -69,7 +69,7 @@ def full_messages
# array of validation errors or empty array, if there are no errors on given field
def on(field_name)
errors_for_field = errors[field_name]
errors_for_field.blank? ? nil : errors_for_field
errors_for_field.blank? ? nil : errors_for_field.uniq
end

def each
Expand Down
Expand Up @@ -85,10 +85,24 @@ def execute?(target)
true
end

# Returns true if validators are equal
#
# Note that this intentionally do
# validate options equality
#
# even though it is hard to imagine a situation
# when multiple validations will be used
# on the same field with the same conditions
# but different options,
# it happens to be the case every once in a while
# with inferred validations for strings/text and
# explicitly given validations with different option
# (usually as Range vs. max limit for inferred validation)
#
# @semipublic
def ==(other)
self.class == other.class &&
self.field_name == other.field_name &&
self.class == other.class &&
self.if_clause == other.if_clause &&
self.unless_clause == other.unless_clause &&
self.instance_variable_get(:@options) == other.instance_variable_get(:@options)
Expand Down
Expand Up @@ -5,6 +5,7 @@
require __dir__.parent.parent + "spec_helper"
require __dir__ + 'spec_helper'

# FIXME: fixture class(es) need rearrangement
describe DataMapper::Validate::LengthValidator do
it "lets user specify custom error message" do
class Jabberwock
Expand Down
4 changes: 2 additions & 2 deletions dm-validations/spec/unit/validation_errors/adding_spec.rb
Expand Up @@ -48,8 +48,8 @@
@model.should_not be_empty
end

it "allows duplication" do
@model.on(:property).should == ["can't be valid, no way", "can't be valid, no way"]
it "DOES NOT allow duplication" do
@model.on(:property).should == ["can't be valid, no way"]
end
end
end

0 comments on commit 21c8b6d

Please sign in to comment.