| 1 | package Valiant::Validator::Format; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 9 | 11 | has match => (is=>'ro', predicate=>'has_match'); | |||||||||||||||||||||||||||
| 10 | 11 | has without => (is=>'ro', predicate=>'has_without'); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 20 | ▶ | has invalid_format_match => (is=>'ro', required=>1, default=>sub {_t 'invalid_format_match'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 20 | ▶ | has invalid_format_without => (is=>'ro', required=>1, default=>sub {_t 'invalid_format_without'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 20 | ▶ | has exclusion => (is=>'ro', required=>1, default=>sub {_t 'exclusion'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub BUILD { | ||||||||||||||||||||||||||||
| 18 | 20 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 20 | $self->_requires_one_of($args, 'match', 'without'); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Stolen from Email::Valid | ||||||||||||||||||||||||||||
| 23 | 11 | our $RFC822PAT = <<'EOF'; | |||||||||||||||||||||||||||
| 24 | [\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\ | ||||||||||||||||||||||||||||
| 25 | xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xf | ||||||||||||||||||||||||||||
| 26 | f\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\x | ||||||||||||||||||||||||||||
| 27 | ff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015 | ||||||||||||||||||||||||||||
| 28 | "]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\ | ||||||||||||||||||||||||||||
| 29 | xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80 | ||||||||||||||||||||||||||||
| 30 | -\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]* | ||||||||||||||||||||||||||||
| 31 | )*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\ | ||||||||||||||||||||||||||||
| 32 | \\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\ | ||||||||||||||||||||||||||||
| 33 | x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x8 | ||||||||||||||||||||||||||||
| 34 | 0-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n | ||||||||||||||||||||||||||||
| 35 | \015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x | ||||||||||||||||||||||||||||
| 36 | 80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^ | ||||||||||||||||||||||||||||
| 37 | \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040 | ||||||||||||||||||||||||||||
| 38 | \t]*)*)*@[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([ | ||||||||||||||||||||||||||||
| 39 | ^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\ | ||||||||||||||||||||||||||||
| 40 | \\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\ | ||||||||||||||||||||||||||||
| 41 | x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80- | ||||||||||||||||||||||||||||
| 42 | \xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015() | ||||||||||||||||||||||||||||
| 43 | ]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\ | ||||||||||||||||||||||||||||
| 44 | x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\04 | ||||||||||||||||||||||||||||
| 45 | 0\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\ | ||||||||||||||||||||||||||||
| 46 | n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\ | ||||||||||||||||||||||||||||
| 47 | 015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?! | ||||||||||||||||||||||||||||
| 48 | [^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\ | ||||||||||||||||||||||||||||
| 49 | ]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\ | ||||||||||||||||||||||||||||
| 50 | x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\01 | ||||||||||||||||||||||||||||
| 51 | 5()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*|(?:[^(\040)<>@,;:". | ||||||||||||||||||||||||||||
| 52 | \\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff] | ||||||||||||||||||||||||||||
| 53 | )|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^ | ||||||||||||||||||||||||||||
| 54 | ()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037]*(?:(?:\([^\\\x80-\xff\n\0 | ||||||||||||||||||||||||||||
| 55 | 15()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][ | ||||||||||||||||||||||||||||
| 56 | ^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)|"[^\\\x80-\xff\ | ||||||||||||||||||||||||||||
| 57 | n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^()<>@,;:".\\\[\]\ | ||||||||||||||||||||||||||||
| 58 | x80-\xff\000-\010\012-\037]*)*<[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(? | ||||||||||||||||||||||||||||
| 59 | :(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80- | ||||||||||||||||||||||||||||
| 60 | \xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:@[\040\t]* | ||||||||||||||||||||||||||||
| 61 | (?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015 | ||||||||||||||||||||||||||||
| 62 | ()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015() | ||||||||||||||||||||||||||||
| 63 | ]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\0 | ||||||||||||||||||||||||||||
| 64 | 40)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\ | ||||||||||||||||||||||||||||
| 65 | [^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\ | ||||||||||||||||||||||||||||
| 66 | xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]* | ||||||||||||||||||||||||||||
| 67 | )*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80 | ||||||||||||||||||||||||||||
| 68 | -\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x | ||||||||||||||||||||||||||||
| 69 | 80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t | ||||||||||||||||||||||||||||
| 70 | ]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\ | ||||||||||||||||||||||||||||
| 71 | \[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff]) | ||||||||||||||||||||||||||||
| 72 | *\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x | ||||||||||||||||||||||||||||
| 73 | 80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80 | ||||||||||||||||||||||||||||
| 74 | -\xff\n\015()]*)*\)[\040\t]*)*)*(?:,[\040\t]*(?:\([^\\\x80-\xff\n\015( | ||||||||||||||||||||||||||||
| 75 | )]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\ | ||||||||||||||||||||||||||||
| 76 | \x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*@[\040\t | ||||||||||||||||||||||||||||
| 77 | ]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\0 | ||||||||||||||||||||||||||||
| 78 | 15()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015 | ||||||||||||||||||||||||||||
| 79 | ()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^( | ||||||||||||||||||||||||||||
| 80 | \040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]| | ||||||||||||||||||||||||||||
| 81 | \\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80 | ||||||||||||||||||||||||||||
| 82 | -\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015() | ||||||||||||||||||||||||||||
| 83 | ]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x | ||||||||||||||||||||||||||||
| 84 | 80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^ | ||||||||||||||||||||||||||||
| 85 | \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040 | ||||||||||||||||||||||||||||
| 86 | \t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:". | ||||||||||||||||||||||||||||
| 87 | \\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff | ||||||||||||||||||||||||||||
| 88 | ])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\ | ||||||||||||||||||||||||||||
| 89 | \x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x | ||||||||||||||||||||||||||||
| 90 | 80-\xff\n\015()]*)*\)[\040\t]*)*)*)*:[\040\t]*(?:\([^\\\x80-\xff\n\015 | ||||||||||||||||||||||||||||
| 91 | ()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\ | ||||||||||||||||||||||||||||
| 92 | \\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)?(?:[^ | ||||||||||||||||||||||||||||
| 93 | (\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000- | ||||||||||||||||||||||||||||
| 94 | \037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\ | ||||||||||||||||||||||||||||
| 95 | n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]| | ||||||||||||||||||||||||||||
| 96 | \([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\)) | ||||||||||||||||||||||||||||
| 97 | [^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff | ||||||||||||||||||||||||||||
| 98 | \n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\x | ||||||||||||||||||||||||||||
| 99 | ff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*( | ||||||||||||||||||||||||||||
| 100 | ?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\ | ||||||||||||||||||||||||||||
| 101 | 000-\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\ | ||||||||||||||||||||||||||||
| 102 | xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\x | ||||||||||||||||||||||||||||
| 103 | ff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*) | ||||||||||||||||||||||||||||
| 104 | *\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[\040\t]*(?:\([^\\\x80-\x | ||||||||||||||||||||||||||||
| 105 | ff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80- | ||||||||||||||||||||||||||||
| 106 | \xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*) | ||||||||||||||||||||||||||||
| 107 | *(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\ | ||||||||||||||||||||||||||||
| 108 | ]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\] | ||||||||||||||||||||||||||||
| 109 | )[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80- | ||||||||||||||||||||||||||||
| 110 | \xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\x | ||||||||||||||||||||||||||||
| 111 | ff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*( | ||||||||||||||||||||||||||||
| 112 | ?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80 | ||||||||||||||||||||||||||||
| 113 | -\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)< | ||||||||||||||||||||||||||||
| 114 | >@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x8 | ||||||||||||||||||||||||||||
| 115 | 0-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?: | ||||||||||||||||||||||||||||
| 116 | \([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()] | ||||||||||||||||||||||||||||
| 117 | *(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*) | ||||||||||||||||||||||||||||
| 118 | *\)[\040\t]*)*)*>) | ||||||||||||||||||||||||||||
| 119 | EOF | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 11 | $RFC822PAT =~ s/\n//g; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # credit_card | ||||||||||||||||||||||||||||
| 124 | # domain_FQDN | ||||||||||||||||||||||||||||
| 125 | # IsISO8601 | ||||||||||||||||||||||||||||
| 126 | # uppercase | ||||||||||||||||||||||||||||
| 127 | # lowercase | ||||||||||||||||||||||||||||
| 128 | # is_url | ||||||||||||||||||||||||||||
| 129 | # IP address | ||||||||||||||||||||||||||||
| 130 | # us zip6 / us zip10 | ||||||||||||||||||||||||||||
| 131 | # us social security | ||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | our %prebuilt_formats = ( | |||||||||||||||||||||||||||
| 135 | alpha => [ qr/^[a-z]+$/i, _t('not_alpha') ], | ||||||||||||||||||||||||||||
| 136 | words => [ qr/^[a-z ]+$/i, _t('not_words') ], # letters and spaces only | ||||||||||||||||||||||||||||
| 137 | alpha_numeric => [ qr/^\w+$/, _t('not_alpha_numeric') ], | ||||||||||||||||||||||||||||
| 138 | alphanumeric => [ qr/^\w+$/, _t('not_alpha_numeric') ], # likely common error | ||||||||||||||||||||||||||||
| 139 | email => [ qr/^$RFC822PAT$/o, _t('not_email') ], | ||||||||||||||||||||||||||||
| 140 | zip => [ qr/^\d{5}(?:[- ]\d{4})?$/, _t('not_zip') ], | ||||||||||||||||||||||||||||
| 141 | zip5 => [ qr/^\d\d\d\d\d$/, _t('not_zip5') ], | ||||||||||||||||||||||||||||
| 142 | zip9 => [ qr/^\d\d\d\d\d[- ]\d\d\d\d$/, _t('not_zip9') ], | ||||||||||||||||||||||||||||
| 143 | ascii => [qr/^\p{IsASCII}*\z/, _t('not_ascii') ], | ||||||||||||||||||||||||||||
| 144 | word => [qr/^\w*\z/, _t('not_word') ], | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 36 | ▶ | sub prebuilt_formats { return \%prebuilt_formats } | ||||||||||||||||||||||||||
|
Subroutine
prebuilt_formats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | around BUILDARGS => sub { | ||||||||||||||||||||||||||||
| 150 | 20 | ▶ | my ( $orig, $class, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 151 | 20 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 20 | foreach my $key (qw/match without/) { | |||||||||||||||||||||||||||
| 154 | 28 | ▶ | next unless exists $args->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 20 | ▶ | next if (ref($args->{$key})||'') eq 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $args->{$key} || ''
MC/DC100%1/1
ref $args->{$key} || ''
ref $args->{$key}
Truth table100%2/2
A
A= ref $args->{$key}
| |||||||||||||||||||||||||||||
| 156 | 12 | ▶ | next unless $class->prebuilt_formats->{$args->{$key}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 12 | my $prebuilt = $args->{$key}; | |||||||||||||||||||||||||||
| 158 | 12 | $args->{$key} = $class->prebuilt_formats->{$prebuilt}->[0]; | |||||||||||||||||||||||||||
| 159 | 12 | ▶ | $args->{message} = $class->prebuilt_formats->{$prebuilt}->[1] unless exists $args->{message}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 12 | return $args; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 8 | return $args; | |||||||||||||||||||||||||||
| 164 | 11 | }; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub normalize_shortcut { | ||||||||||||||||||||||||||||
| 167 | 18 | ▶ | my ($class, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 18 | return +{ match => $arg }; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub validate_each { | ||||||||||||||||||||||||||||
| 172 | 131 | ▶ | my ($self, $record, $attribute, $value, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 131 | ▶ | if($self->has_match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 129 | my $with = $self->_cb_value($record, $self->match); | |||||||||||||||||||||||||||
| 176 | 129 | ▶ | $record->errors->add($attribute, $self->invalid_format_match, $opts) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value =~ /$with/
MC/DC100%2/2
defined $value and $value =~ /$with/
defined $value
$value =~ /$with/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /$with/
| |||||||||||||||||||||||||||||
| 177 | unless defined($value) && $value =~m/$with/; | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 131 | ▶ | if($self->has_without) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 2 | my $with = $self->_cb_value($record, $self->without); | |||||||||||||||||||||||||||
| 181 | 2 | ▶ | if($value =~m/$with/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 1 | $record->errors->add($attribute, $self->invalid_format_without, $opts); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 11 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 NAME | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Valiant::Validator::Format - Validate a value based on a regular expression | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | package Local::Test::Format; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | use Moo; | ||||||||||||||||||||||||||||
| 198 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | has phone => (is=>'ro'); | ||||||||||||||||||||||||||||
| 201 | has name => (is=>'ro'); | ||||||||||||||||||||||||||||
| 202 | has email => (is=>'ro'); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | validates phone => ( | ||||||||||||||||||||||||||||
| 205 | format => +{ | ||||||||||||||||||||||||||||
| 206 | match => qr/\d\d\d-\d\d\d-\d\d\d\d/, | ||||||||||||||||||||||||||||
| 207 | }, | ||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | validates name => ( | ||||||||||||||||||||||||||||
| 211 | format => +{ | ||||||||||||||||||||||||||||
| 212 | without => qr/\d+/, | ||||||||||||||||||||||||||||
| 213 | }, | ||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | validates email => | ||||||||||||||||||||||||||||
| 217 | format => 'email'; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $object = Local::Test::Format->new( | ||||||||||||||||||||||||||||
| 220 | phone => '387-1212', | ||||||||||||||||||||||||||||
| 221 | name => 'jjn1056', | ||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $object->validate; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | $VAR1 = { | ||||||||||||||||||||||||||||
| 229 | 'phone' => [ | ||||||||||||||||||||||||||||
| 230 | 'Phone does not match the required pattern' | ||||||||||||||||||||||||||||
| 231 | ], | ||||||||||||||||||||||||||||
| 232 | 'name' => [ | ||||||||||||||||||||||||||||
| 233 | 'Name contains invalid characters' | ||||||||||||||||||||||||||||
| 234 | ], | ||||||||||||||||||||||||||||
| 235 | 'email' => [ | ||||||||||||||||||||||||||||
| 236 | 'Email is not an email address' | ||||||||||||||||||||||||||||
| 237 | ], | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | }; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Validates that the attribute value either matches a given regular expression (C<match) | ||||||||||||||||||||||||||||
| 244 | or that it fails to match an exclusion expression (C<without>). | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Values that fail the C<match> condition (which can be a regular expression or a | ||||||||||||||||||||||||||||
| 247 | reference to a method that provides one) will add an error matching the tag | ||||||||||||||||||||||||||||
| 248 | C<invalid_format_match>, which can be overridden as with other tags. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Values that match the C<without> conditions (also either a regular expression or | ||||||||||||||||||||||||||||
| 251 | a coderef that provides one) with all an error matching the tag C<invalid_format_without> | ||||||||||||||||||||||||||||
| 252 | which can also be overridden via a passed parameter. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | A number of format shortcuts are built in to help you bootstrap faster. Use the text | ||||||||||||||||||||||||||||
| 255 | string in place of the regexp (for example:) | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | validates email => ( | ||||||||||||||||||||||||||||
| 258 | format => +{ | ||||||||||||||||||||||||||||
| 259 | match => 'email', | ||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | You can add to the prebuilts by adding keys to the global C<%prebuilt_formats> | ||||||||||||||||||||||||||||
| 264 | hash. See code for more. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =over 4 | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =item email | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Uses a regexp to validate that a string looks like an email. This is naive but | ||||||||||||||||||||||||||||
| 271 | probably acceptable for most uses. For real detailed eail checking you'll need | ||||||||||||||||||||||||||||
| 272 | to build something on L<Email::Valid>. Uses translation tag C<not_email>. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =item alpha | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Text can only be upper or lowercase letter. Uses translation tag C<not_alpha>. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =item alpha_numeric | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Text can only be upper or lowercase letters, numbers or '_'. Uses translation | ||||||||||||||||||||||||||||
| 281 | tag C<not_alpha_numeric>. | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =item words | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Only letters and spaces. Error message default is translation tag C<not_words>. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =item word | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Must contain only a single word. Adds error C<not_word> if fails. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item zip | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =item zip5 | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item zip9 | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Match US zipcodes. This is a pattern match only, we don't actually check if the zip | ||||||||||||||||||||||||||||
| 298 | is a true valid zipcode, just that it looks like one. C<zip5> matches a common 5 | ||||||||||||||||||||||||||||
| 299 | digit zipcode and return C<not_zip5> translation tag if not. C<zip9> looks for the | ||||||||||||||||||||||||||||
| 300 | 'Zip +4' extended zipcode, such as 11111-2222 (the separator can be either '-' or a | ||||||||||||||||||||||||||||
| 301 | space). Adds errors C<not_zip9> if not. Finally C<zip> will match either zip or zip +4 | ||||||||||||||||||||||||||||
| 302 | and add error C<not_zip> on failure to match. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =item ascii | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | Must contain only ASCII characters. Adds error C<not_ascii> if fails. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | validates attribute => ( format => qr/\d\d\d/, ... ); | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Which is the same as: | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | validates attribute => ( | ||||||||||||||||||||||||||||
| 319 | format => +{ | ||||||||||||||||||||||||||||
| 320 | match => qr/\d\d\d/, | ||||||||||||||||||||||||||||
| 321 | }, | ||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | We choose to shortcut the 'match' pattern based on experiene that suggested | ||||||||||||||||||||||||||||
| 325 | it is more common to required a specific pattern than to have an exclusion | ||||||||||||||||||||||||||||
| 326 | pattern. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | This also works for the coderef form. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | validates attribute => ( format => \&pattern, ... ); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Which is the same as: | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | validates attribute => ( | ||||||||||||||||||||||||||||
| 335 | format => +{ | ||||||||||||||||||||||||||||
| 336 | match => \&pattern, | ||||||||||||||||||||||||||||
| 337 | }, | ||||||||||||||||||||||||||||
| 338 | ); | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | And of course its very handy for using one of the built in pattern tags: | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | validates email => (format => 'email'); | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Which is the same as: | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | validates email => ( | ||||||||||||||||||||||||||||
| 347 | format => +{ | ||||||||||||||||||||||||||||
| 348 | match => 'email', | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | ); | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 355 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | See L<Valiant> | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | See L<Valiant> | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||