From: John Napiorkowski Date: Wed, 1 Apr 2009 03:11:50 +0000 (+0000) Subject: starting on the error messaging problem; X-Git-Tag: 0.01~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=41cf74575a04a14eaf00c26298268fd8e9473ed7;p=gitmo%2FMooseX-Dependent.git starting on the error messaging problem; --- diff --git a/lib/MooseX/Meta/TypeConstraint/Dependent.pm b/lib/MooseX/Meta/TypeConstraint/Dependent.pm index 83d140c..caed2ae 100644 --- a/lib/MooseX/Meta/TypeConstraint/Dependent.pm +++ b/lib/MooseX/Meta/TypeConstraint/Dependent.pm @@ -32,7 +32,8 @@ has 'dependent_type_constraint' => ( isa=>'Object', predicate=>'has_dependent_type_constraint', handles=>{ - check_dependent=>'check', + check_dependent=>'check', + get_message_dependent=>'get_message', }, ); @@ -107,6 +108,35 @@ around 'new' => sub { return $self; }; +=head2 validate + +We intercept validate in order to custom process the message + + +=cut + +around 'check' => sub { + my ($check, $self, @args) = @_; + my ($result, $message) = $self->_compiled_type_constraint->(@args); + warn $result; + return $result; +}; + +around 'validate' => sub { + my ($validate, $self, @args) = @_; + my ($result, $message) = $self->_compiled_type_constraint->(@args); + + if($result) { + return $result; + } else { + if(defined $message) { + return "Inner: $message"; + } else { warn '......................'; + return $self->get_message(@args); + } + } +}; + =head2 generate_constraint_for ($type_constraints) Given some type constraints, use them to generate validation rules for an ref @@ -117,12 +147,12 @@ of values (to be passed at check time) sub generate_constraint_for { my ($self, $callback) = @_; return sub { - my ($dependent_pair) = @_; + my $dependent_pair = shift @_; my ($dependent, $constraining) = @$dependent_pair; ## First need to test the bits unless($self->check_dependent($dependent)) { - return; + return (undef, 'bbbbbb'); } unless($self->check_constraining($constraining)) { diff --git a/t/02-depending.t b/t/02-depending.t index b1269c0..dbffb72 100644 --- a/t/02-depending.t +++ b/t/02-depending.t @@ -85,5 +85,9 @@ use Test::More tests=>24; { ok !UniqueInt2->check([10,[1,10,15]]), 'not unique in set'; ok !UniqueInt2->check([2,[3..6]]), 'FAIL dependent is too small'; ok UniqueInt2->check([3,[100..110]]), 'PASS unique in set'; - ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set'; + ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set'; + + ## Basic error messages. TODO should be it's own test + + warn UniqueInt2->validate(['a',[1,2,3]]); }