| 1 | package Valiant::Validator::Hash; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | has validations => (is=>'ro', required=>1); | |||||||||||||||||||||||||||||||
| 11 | 1 | has validator => (is=>'ro', required=>1); | |||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | has invalid_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid'}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=26.8
| |||||||||||||||||||||||||||||||||
| 15 | 2 | my ( $orig, $class, @args ) = @_; | |||||||||||||||||||||||||||||||
| 16 | 2 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | if($args->{namespace}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 19 | 0 | $args->{for} = $args->{namespace}; | |||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | if( (ref($args->{validations})||'') && !exists $args->{validator} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
ref $args->{"validations"} || "" and not exists $args->{"validator"}
ref $args->{"validations"} or ""
MC/DC0%0/2
ref $args->{"validations"} || "" and not exists $args->{"validator"}
ref $args->{"validations"}
not exists $args->{"validator"}
Truth table33%1/3
A and not(B)
A= ref $args->{"validations"}B= not exists $args->{"validator"}
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | # convert hashref to arrayref | ||||||||||||||||||||||||||||||||
| 25 | 2 | my @validations = (); | |||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | if( ((ref($args->{validations})||'') eq 'ARRAY') ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
ref $args->{'validations'} || ''
MC/DC0%0/1
ref $args->{'validations'} || ''
ref $args->{'validations'}
Truth table50%1/2
A
A= ref $args->{'validations'}
| |||||||||||||||||||||||||||||||||
| 27 | 1 | @validations = @{$args->{validations}}; | |||||||||||||||||||||||||||||||
| 28 | ▶ | } elsif( ((ref($args->{validations})||'') eq 'HASH') ) { | |||||||||||||||||||||||||||||||
|
Condition50%1/2
ref $args->{'validations'} || ''
MC/DC0%0/1
ref $args->{'validations'} || ''
ref $args->{'validations'}
Truth table50%1/2
A
A= ref $args->{'validations'}
| |||||||||||||||||||||||||||||||||
| 29 | @validations = map { | ||||||||||||||||||||||||||||||||
| 30 | 2 | [ $_, @{$args->{validations}{$_}} ]; | |||||||||||||||||||||||||||||||
| 31 | 1 | } keys %{$args->{validations}}; | |||||||||||||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||||||||||||||
| 33 | 0 | throw_exception General => (msg=>'validations argument in unsupported format'); | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | my $validator = use_module($args->{validator_class}||'Valiant::Proxy::Hash') | ||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | ->new( result_class => 'Valiant::Result::HashRef', %{ $args->{validator_class_args}||+{} }, | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'validator_class_args'} || {}
MC/DC0%0/1
$args->{'validator_class_args'} || {}
$args->{'validator_class_args'}
Truth table50%1/2
A
A= $args->{'validator_class_args'}
| |||||||||||||||||||||||||||||||||
| 38 | for => $args->{for}, | ||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | validations => \@validations); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'validator_class'} || 'Valiant::Proxy::Hash'
MC/DC0%0/1
$args->{'validator_class'} || 'Valiant::Proxy::Hash'
$args->{'validator_class'}
Truth table50%1/2
A
A= $args->{'validator_class'}
| |||||||||||||||||||||||||||||||||
| 40 | 2 | $args->{validator} = $validator; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 2 | return $args; | |||||||||||||||||||||||||||||||
| 44 | 1 | }; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 47 | 1 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if( (ref($arg)||'') eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $arg || ''
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
| |||||||||||||||||||||||||||||||||
| 49 | 1 | return {validations => $arg}; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub validate_each { | ||||||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 54 | 4 | my ($self, $record, $attribute, $value, $options) = @_; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | my %opts = (%{$self->options}, %{$options||{}}); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$options || {}
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||
| 57 | 4 | my $validator = $self->validator; | |||||||||||||||||||||||||||||||
| 58 | 4 | my $result = $validator->validate($value, %opts); | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if($result->invalid) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 2 | my $errors = $result->errors; | |||||||||||||||||||||||||||||||
| 62 | 2 | $errors->{__result} = $result; # hack to keep this in scope | |||||||||||||||||||||||||||||||
| 63 | 2 | $record->errors->add($attribute, $self->invalid_msg, \%opts); | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | $result->errors->each(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 66 | 5 | my ($attr, $message) = @_; | |||||||||||||||||||||||||||||||
| 67 | 5 | $record->errors->add("${attribute}.${attr}", $message); | |||||||||||||||||||||||||||||||
| 68 | 2 | }); | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 1 | 1; | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | Valiant::Validator::Hash - Verify the values of a hashref | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | package Local::Test::Person; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | use Moo; | ||||||||||||||||||||||||||||||||
| 84 | use Valiant::Validations; | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | has name => (is=>'ro'); | ||||||||||||||||||||||||||||||||
| 87 | has address => (is=>'ro'); | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | validates name => ( | ||||||||||||||||||||||||||||||||
| 90 | length => [2,30], | ||||||||||||||||||||||||||||||||
| 91 | format => qr/[A-Za-z]+/, #yes no unicode names for this test... | ||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | validates address => ( | ||||||||||||||||||||||||||||||||
| 95 | presence => 1, | ||||||||||||||||||||||||||||||||
| 96 | hash => [ | ||||||||||||||||||||||||||||||||
| 97 | [street => presence => 1, length => [2,24] ], | ||||||||||||||||||||||||||||||||
| 98 | [city => presence => 1, length => [2,24] ], | ||||||||||||||||||||||||||||||||
| 99 | [zip => presence => 1, numericality => 'positive_integer', format => qr/\d\d\d\d\d/ ], | ||||||||||||||||||||||||||||||||
| 100 | ], | ||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # long form example | ||||||||||||||||||||||||||||||||
| 104 | validates address => ( | ||||||||||||||||||||||||||||||||
| 105 | presence => 1, | ||||||||||||||||||||||||||||||||
| 106 | hash => { | ||||||||||||||||||||||||||||||||
| 107 | validations => { | ||||||||||||||||||||||||||||||||
| 108 | street => [format => qr/[^\=]/, message => 'cannot have silly characters'], | ||||||||||||||||||||||||||||||||
| 109 | zip => [length => [5,5]], | ||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | my $person = Local::Test::Person->new( | ||||||||||||||||||||||||||||||||
| 115 | name => '12', | ||||||||||||||||||||||||||||||||
| 116 | address => +{ | ||||||||||||||||||||||||||||||||
| 117 | street => '=', | ||||||||||||||||||||||||||||||||
| 118 | city => 'Elgin', | ||||||||||||||||||||||||||||||||
| 119 | zip => '2aa', | ||||||||||||||||||||||||||||||||
| 120 | }, | ||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | $person->validate->invalid; # True, the object is invalid. | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | warn $person->errors->_dump; | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | $VAR1 = { | ||||||||||||||||||||||||||||||||
| 128 | address => [ | ||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||
| 130 | street => [ | ||||||||||||||||||||||||||||||||
| 131 | "Street is too short (minimum is 2 characters)", | ||||||||||||||||||||||||||||||||
| 132 | "Street cannot have silly characters", | ||||||||||||||||||||||||||||||||
| 133 | ], | ||||||||||||||||||||||||||||||||
| 134 | zip => [ | ||||||||||||||||||||||||||||||||
| 135 | "Zip must be an integer", | ||||||||||||||||||||||||||||||||
| 136 | "Zip does not match the required pattern", | ||||||||||||||||||||||||||||||||
| 137 | "Zip is too short (minimum is 5 characters)", | ||||||||||||||||||||||||||||||||
| 138 | ], | ||||||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||||||
| 140 | ], | ||||||||||||||||||||||||||||||||
| 141 | 'name' => [ | ||||||||||||||||||||||||||||||||
| 142 | 'Name does not match the required pattern' | ||||||||||||||||||||||||||||||||
| 143 | ] | ||||||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | Perform validations on values when the value associated with the attribute | ||||||||||||||||||||||||||||||||
| 149 | is a hashref. You can use this when inflating an object seems like silly | ||||||||||||||||||||||||||||||||
| 150 | work. | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | This validator supports the following attributes: | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | =head2 validations | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | Either 1 or an arrayref of validation rules. Each item in the arrayref | ||||||||||||||||||||||||||||||||
| 159 | is an arrayref that contains anything you may pass to C<validates>. Typically | ||||||||||||||||||||||||||||||||
| 160 | this will be a key name for the hashref followed by a list of validation | ||||||||||||||||||||||||||||||||
| 161 | rules. | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =head2 for / namespace | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | When defining an inline validation ruleset against an associated object that | ||||||||||||||||||||||||||||||||
| 166 | does not itself have validation rules, you must set this to something that | ||||||||||||||||||||||||||||||||
| 167 | ISA or DOES the class you are defining inline validations on. This is not | ||||||||||||||||||||||||||||||||
| 168 | currently strictly enforced, but this value is used to find any locale files | ||||||||||||||||||||||||||||||||
| 169 | or custom validator classes. | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head2 validator | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | This contains an instance of L<Valiant::Proxy::Hash> or subclass. Default value | ||||||||||||||||||||||||||||||||
| 174 | does the right thing but you can override if you need a special subclass | ||||||||||||||||||||||||||||||||
| 175 | or you need to pass one in that's already constructed. | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | =head2 validator_class | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | Defaults to L<Valiant::Proxy::Hash>, which value should be a subclass of. You probably | ||||||||||||||||||||||||||||||||
| 180 | only need this again if you are doing very custom validations. You probably only | ||||||||||||||||||||||||||||||||
| 181 | want do to this if there's no other idea. | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | =head2 validator_class_args | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | A hashref of args that get passed to the C<new> method of C<validator_class>. | ||||||||||||||||||||||||||||||||
| 186 | Defaults to an empty hashref. You might need this if you build a custom validator | ||||||||||||||||||||||||||||||||
| 187 | class and have special arguments it needs. | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | =head2 invalid_msg | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | The message returned when the hash is generically invalid. A hash becomes invalid | ||||||||||||||||||||||||||||||||
| 192 | should any validations you define on its values fail to validate. Default is | ||||||||||||||||||||||||||||||||
| 193 | translation tag 'invalid'. | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | validates attribute => ( hash => \@validation_rules, ... ); | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | Which is the same as: | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | validates attribute => ( | ||||||||||||||||||||||||||||||||
| 204 | hash => { | ||||||||||||||||||||||||||||||||
| 205 | validations => \@validation_rules, | ||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | ); | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | But less typing and probably makes sense unless you need to set some of the more | ||||||||||||||||||||||||||||||||
| 210 | rarely used attributes such as C<validator_class> etc. | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||||||
| 215 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | See L<Valiant> | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | See L<Valiant> | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||||||