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