| 1 | package Valiant::Validator::Collection; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 6 | with 'Valiant::Validator'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 6 | has if => (is=>'ro', predicate=>'has_if'); | |||||||||||||||||||
| 9 | 6 | has unless => (is=>'ro', predicate=>'has_unless'); | |||||||||||||||||||
| 10 | 6 | has on => (is=>'ro', predicate=>'has_on'); | |||||||||||||||||||
| 11 | 6 | has message => (is=>'ro', predicate=>'has_message'); | |||||||||||||||||||
| 12 | 6 | has strict => (is=>'ro', predicate=>'has_strict'); | |||||||||||||||||||
| 13 | 6 | has validators => (is=>'ro', required=>1); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 19 | ▶ | sub options { | ||||||||||||||||||
|
Subroutine
options: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 16 | 19 | my $self = shift; | |||||||||||||||||||
| 17 | 19 | my %opts = (@_); | |||||||||||||||||||
| 18 | 19 | ▶ | $opts{strict} = $self->strict if $self->has_strict; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 19 | ▶ | $opts{message} = $self->message if $self->has_message; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 19 | return \%opts; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # would be great if we could rationalize this with Each | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 21 | ▶ | sub validate { | ||||||||||||||||||
|
Subroutine
validate: called CC=25 SCAR=38.6
Pod
undocumented
| |||||||||||||||||||||
| 26 | 21 | my ($self, $object, $options) = @_; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 21 | ▶ | if($self->has_if) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 3 | my $if = $self->if; | |||||||||||||||||||
| 30 | 3 | ▶ | if((ref($if)||'') eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $if || ''
MC/DC100%1/1
ref $if || ''
ref $if
Truth table100%2/2
A
A= ref $if
| |||||||||||||||||||||
| 31 | 1 | ▶ | return unless $if->($object); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||
| 33 | 2 | ▶ | if(my $method_cb = $object->can($if)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 2 | ▶ | return unless $method_cb->($object, $options); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||
| 36 | 0 | throw_exception MissingMethod => (object=>$object, method=>$if); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 21 | ▶ | if($self->has_unless) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | my $unless = $self->unless; | |||||||||||||||||||
| 43 | 0 | ▶ | if((ref($unless)||'') eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $unless || ''
MC/DC0%0/1
ref $unless || ''
ref $unless
Truth table0%0/2
A
A= ref $unless
| |||||||||||||||||||||
| 44 | 0 | ▶ | return if $unless->($object); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||
| 46 | 0 | ▶ | if(my $method_cb = $object->can($unless)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 0 | ▶ | return if $method_cb->($object, $options); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||
| 49 | 0 | throw_exception MissingMethod => (object=>$object, method=>$unless); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 21 | ▶ | if($self->has_on) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | ▶ | my @on = ref($self->on) ? @{$self->on} : ($self->on); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 4 | ▶ | my $context = $options->{context}||''; | ||||||||||||||||||
|
Condition100%2/2
$options->{'context'} || ''
MC/DC100%1/1
$options->{'context'} || ''
$options->{'context'}
Truth table100%2/2
A
A= $options->{'context'}
| |||||||||||||||||||||
| 57 | 4 | ▶ | my @context = ref($context) ? @$context : ($context); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 4 | my $matches = 0; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 4 | OUTER: foreach my $c (@context) { | |||||||||||||||||||
| 61 | 4 | foreach my $o (@on) { | |||||||||||||||||||
| 62 | 4 | ▶ | if($c eq $o) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 2 | $matches = 1; | |||||||||||||||||||
| 64 | 2 | last OUTER; # We only need to match one context | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 4 | ▶ | return unless $matches; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 19 | my $opts = $self->options(%$options); | |||||||||||||||||||
| 72 | 19 | foreach my $validator (@{ $self->validators }) { | |||||||||||||||||||
| 73 | 19 | ▶ | if( (ref($validator)||'') eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $validator || ''
MC/DC0%0/1
ref $validator || ''
ref $validator
Truth table50%1/2
A
A= ref $validator
| |||||||||||||||||||||
| 74 | 13 | ▶ | $validator->[0]->($object, +{ %{$validator->[1]||+{}}, %$opts }); | ||||||||||||||||||
|
Condition50%1/2
$validator->[1] || {}
MC/DC0%0/1
$validator->[1] || {}
$validator->[1]
Truth table50%1/2
A
A= $validator->[1]
| |||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||
| 76 | 6 | $validator->validate($object, $opts); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 6 | 1; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head1 NAME | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | Valiant::Validator::Collection - A validator that contains and runs other validators | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head1 SYNOPSIS | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | NA | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | This is used internally by L<Valiant> and I can't imagine a good use for it elsewhere | ||||||||||||||||||||
| 94 | so the documentation here is light. There's no reason to NOT use it if for some | ||||||||||||||||||||
| 95 | reason a good use comes to mind (I don't plan to change this so you can consider it | ||||||||||||||||||||
| 96 | public API but just perhaps tricky bits for very advanced use cases). | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | I guess it could be used to make very complicated nested validations. I'm not going | ||||||||||||||||||||
| 99 | to show you how to do that since I think only people that can figure it out should | ||||||||||||||||||||
| 100 | be allowed. If you think I'm wrong ping me on IRC and submit a doc patch. | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | This validator role provides the following attributes | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head2 if / unless | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Accepts a coderef or the name of a method which executes and is expected to | ||||||||||||||||||||
| 109 | return true or false. If false we skip the validation (or true for C<unless>). | ||||||||||||||||||||
| 110 | Recieves the object, the attribute name and the value to be checked as arguments. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head2 message | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Provide a global error message override for the constraint. Will accept either | ||||||||||||||||||||
| 115 | a string message or a translation tag. Please not that many validators also | ||||||||||||||||||||
| 116 | provide error type specific messages for providing custom errors (as well as | ||||||||||||||||||||
| 117 | the ability to setup your own errors in a localization file. Using this attribute | ||||||||||||||||||||
| 118 | is the easiest but probably not always your best option. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head2 strict | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | When true instead of adding a message to the errors list, will throw exception with the | ||||||||||||||||||||
| 123 | error instead. If the true value is the name of a class that provides a C<throw> | ||||||||||||||||||||
| 124 | message, will use that instead. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head2 on | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | A scalar or list of contexts that can be used to control the situation ('context') | ||||||||||||||||||||
| 129 | under which the validation is executed. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 METHODS | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | This role provides the following methods. You may wish to review the source | ||||||||||||||||||||
| 134 | code of the prebuild validators for examples of usage. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head2 options | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Used to properly construct a options hashref that you should pass to any | ||||||||||||||||||||
| 139 | calls to add an error. You need this for passing special values to the translation | ||||||||||||||||||||
| 140 | method or for setting overrides such as C<strict> or C<message>. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head1 AUTHOR | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | See L<Valiant> | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | See L<Valiant> | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||