| 1 | package Valiant::Validator::Length; | ||||||||||||||||
| 2 | |||||||||||||||||
| 3 | 11 | ▶ | use Moo; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 4 | 11 | ▶ | use Valiant::I18N; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 5 | |||||||||||||||||
| 6 | 11 | with 'Valiant::Validator::Each'; | |||||||||||||||
| 7 | |||||||||||||||||
| 8 | 11 | has maximum => (is=>'ro', predicate=>'has_maximum'); | |||||||||||||||
| 9 | 11 | has minimum => (is=>'ro', predicate=>'has_minimum'); | |||||||||||||||
| 10 | 11 | has in => (is=>'ro', predicate=>'has_in'); | |||||||||||||||
| 11 | 11 | has is => (is=>'ro', predicate=>'has_is'); | |||||||||||||||
| 12 | |||||||||||||||||
| 13 | 21 | ▶ | has too_long => (is=>'ro', required=>1, default=>sub {_t 'too_long'}); | ||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||
| 14 | 21 | ▶ | has too_short => (is=>'ro', required=>1, default=>sub {_t 'too_short'}); | ||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||
| 15 | 21 | ▶ | has wrong_length => (is=>'ro', required=>1, default=>sub {_t 'wrong_length'}); | ||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||
| 16 | |||||||||||||||||
| 17 | 15 | ▶ | sub normalize_shortcut { | ||||||||||||||
|
Subroutine
normalize_shortcut: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 18 | 15 | my ($class, $arg) = @_; | |||||||||||||||
| 19 | 15 | return +{ in => $arg }; | |||||||||||||||
| 20 | } | ||||||||||||||||
| 21 | |||||||||||||||||
| 22 | |||||||||||||||||
| 23 | 21 | ▶ | sub BUILD { | ||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 24 | 21 | my ($self, $args) = @_; | |||||||||||||||
| 25 | 21 | $self->_requires_one_of($args, 'maximum', 'minimum', 'in', 'is'); | |||||||||||||||
| 26 | } | ||||||||||||||||
| 27 | |||||||||||||||||
| 28 | 48 | ▶ | sub validate_each { | ||||||||||||||
|
Subroutine
validate_each: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||
| 29 | 48 | my ($self, $record, $attribute, $value, $options) = @_; | |||||||||||||||
| 30 | 48 | ▶ | my $length = length($value||'') || 0; # TODO not sure if this is best behavior | ||||||||||||||
|
Condition100%4/4
length($value || '') || 0
$value || ''
MC/DC100%1/1
length($value || '') || 0
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||
| 31 | 48 | ▶ | my %opts = (%{$self->options}, %{$options||+{}}); | ||||||||||||||
|
Condition50%1/2
$options || {}
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||
| 32 | 48 | ▶ | if($self->has_maximum) { | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 33 | 9 | my $max = $self->_cb_value($record, $self->maximum); | |||||||||||||||
| 34 | 9 | ▶ | $record->errors->add($attribute, $self->too_long, +{%opts, count=>$max}) | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 35 | if $length > $max ; | ||||||||||||||||
| 36 | } | ||||||||||||||||
| 37 | 48 | ▶ | if($self->has_minimum) { | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 38 | 9 | my $min = $self->_cb_value($record, $self->minimum); | |||||||||||||||
| 39 | 9 | ▶ | $record->errors->add($attribute, $self->too_short, +{%opts, count=>$min}) | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 40 | if $length < $min; | ||||||||||||||||
| 41 | } | ||||||||||||||||
| 42 | 48 | ▶ | if($self->has_in) { | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 43 | 39 | my ($min, $max) = @{$self->in}; | |||||||||||||||
| 44 | 39 | $max = $self->_cb_value($record, $max); | |||||||||||||||
| 45 | 39 | $min = $self->_cb_value($record, $min); | |||||||||||||||
| 46 | 39 | ▶ | $record->errors->add($attribute, $self->too_long, +{%opts, count=>$max}) | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 47 | if $length > $max; | ||||||||||||||||
| 48 | 39 | ▶ | $record->errors->add($attribute, $self->too_short, +{%opts, count=>$min}) | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 49 | if $length < $min; | ||||||||||||||||
| 50 | } | ||||||||||||||||
| 51 | 48 | ▶ | if($self->has_is) { | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 52 | 2 | my $match_value = $self->_cb_value($record, $self->is); | |||||||||||||||
| 53 | 2 | ▶ | $record->errors->add($attribute, $self->wrong_length, +{%opts, count=>$match_value}) | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 54 | unless $length == $match_value; | ||||||||||||||||
| 55 | } | ||||||||||||||||
| 56 | } | ||||||||||||||||
| 57 | |||||||||||||||||
| 58 | 11 | 1; | |||||||||||||||
| 59 | |||||||||||||||||
| 60 | =head1 NAME | ||||||||||||||||
| 61 | |||||||||||||||||
| 62 | Valiant::Validator::Length - Validate the length of an attributes string value | ||||||||||||||||
| 63 | |||||||||||||||||
| 64 | =head1 SYNOPSIS | ||||||||||||||||
| 65 | |||||||||||||||||
| 66 | package Local::Test::Length; | ||||||||||||||||
| 67 | |||||||||||||||||
| 68 | use Moo; | ||||||||||||||||
| 69 | use Valiant::Validations; | ||||||||||||||||
| 70 | |||||||||||||||||
| 71 | has name => (is=>'ro'); | ||||||||||||||||
| 72 | has equals => (is=>'ro', required=>1, default=>5); | ||||||||||||||||
| 73 | |||||||||||||||||
| 74 | validates name => ( | ||||||||||||||||
| 75 | length => { | ||||||||||||||||
| 76 | maximum => 10, | ||||||||||||||||
| 77 | minimum => 3, | ||||||||||||||||
| 78 | is => sub { shift->equals }, | ||||||||||||||||
| 79 | } | ||||||||||||||||
| 80 | ); | ||||||||||||||||
| 81 | |||||||||||||||||
| 82 | my $object = Local::Test::Length->new(name=>'Li'); | ||||||||||||||||
| 83 | $object->validate; # Returns false | ||||||||||||||||
| 84 | |||||||||||||||||
| 85 | warn $object->errors->_dump; | ||||||||||||||||
| 86 | |||||||||||||||||
| 87 | $VAR1 = { | ||||||||||||||||
| 88 | 'name' => [ | ||||||||||||||||
| 89 | 'Name is too short (minimum is 3 characters', | ||||||||||||||||
| 90 | 'Name is the wrong length (should be 5 characters)', | ||||||||||||||||
| 91 | ] | ||||||||||||||||
| 92 | }; | ||||||||||||||||
| 93 | |||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||
| 95 | |||||||||||||||||
| 96 | Validates the length of a scalar, usually a string. Supports a number of | ||||||||||||||||
| 97 | constraint parameters that allow you to place various limits on this length | ||||||||||||||||
| 98 | (like many other validators the value of the constraints can be a constant | ||||||||||||||||
| 99 | or a coderef that gets the object instances as an argument). You also | ||||||||||||||||
| 100 | have parameters to override the default error for each constraint (these | ||||||||||||||||
| 101 | arguments match the tag name). | ||||||||||||||||
| 102 | |||||||||||||||||
| 103 | =head1 CONSTRAINTS | ||||||||||||||||
| 104 | |||||||||||||||||
| 105 | This validator supports the following constraints. | ||||||||||||||||
| 106 | |||||||||||||||||
| 107 | =over | ||||||||||||||||
| 108 | |||||||||||||||||
| 109 | =item maximum | ||||||||||||||||
| 110 | |||||||||||||||||
| 111 | Accepts numeric value or coderef. Returns error message tag C<too_long> if | ||||||||||||||||
| 112 | the attribute length exceeds the value. | ||||||||||||||||
| 113 | |||||||||||||||||
| 114 | =item minimum | ||||||||||||||||
| 115 | |||||||||||||||||
| 116 | Accepts numeric value or coderef. Returns error message tag C<too_short> if | ||||||||||||||||
| 117 | the attribute length is smaller than the value. | ||||||||||||||||
| 118 | |||||||||||||||||
| 119 | =item in | ||||||||||||||||
| 120 | |||||||||||||||||
| 121 | Accepts an arrayref where the first item is a minimum length and the second item | ||||||||||||||||
| 122 | is a maximum lenth or coderef that returns such an arrayref. Returns error message | ||||||||||||||||
| 123 | of either C<too_short> or C<too_long> if the value length is outside the range specified. | ||||||||||||||||
| 124 | |||||||||||||||||
| 125 | =item is | ||||||||||||||||
| 126 | |||||||||||||||||
| 127 | Accepts numeric value or coderef. Returns error message tag C<wrong_length> unless | ||||||||||||||||
| 128 | the attribute value's length is exactly the check value. | ||||||||||||||||
| 129 | |||||||||||||||||
| 130 | =back | ||||||||||||||||
| 131 | |||||||||||||||||
| 132 | =head1 SHORTCUT FORM | ||||||||||||||||
| 133 | |||||||||||||||||
| 134 | This validator supports the follow shortcut forms: | ||||||||||||||||
| 135 | |||||||||||||||||
| 136 | validates attribute => ( length => [1, 10], ... ); | ||||||||||||||||
| 137 | |||||||||||||||||
| 138 | Which is the same as: | ||||||||||||||||
| 139 | |||||||||||||||||
| 140 | validates attribute => ( | ||||||||||||||||
| 141 | length => { | ||||||||||||||||
| 142 | in => [1, 10], | ||||||||||||||||
| 143 | }, | ||||||||||||||||
| 144 | ); | ||||||||||||||||
| 145 | |||||||||||||||||
| 146 | =head1 GLOBAL PARAMETERS | ||||||||||||||||
| 147 | |||||||||||||||||
| 148 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||
| 149 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||
| 150 | |||||||||||||||||
| 151 | =head1 AUTHOR | ||||||||||||||||
| 152 | |||||||||||||||||
| 153 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||
| 154 | |||||||||||||||||
| 155 | =head1 SEE ALSO | ||||||||||||||||
| 156 | |||||||||||||||||
| 157 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||
| 158 | |||||||||||||||||
| 159 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||
| 160 | |||||||||||||||||
| 161 | Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||
| 162 | |||||||||||||||||
| 163 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||
| 164 | the same terms as Perl itself. | ||||||||||||||||
| 165 | |||||||||||||||||
| 166 | =cut | ||||||||||||||||