| 1 | package Valiant::Validator::Date; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | our $_pattern = '%Y-%m-%d'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | has min => (is=>'ro', required=>0, predicate=>'has_min'); | |||||||||||||||||||||||||||
| 14 | 3 | has max => (is=>'ro', required=>0, predicate=>'has_max'); | |||||||||||||||||||||||||||
| 15 | 3 | has min_eq => (is=>'ro', required=>0, predicate=>'has_min_eq'); | |||||||||||||||||||||||||||
| 16 | 3 | has max_eq => (is=>'ro', required=>0, predicate=>'has_max_eq'); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | has cb => (is=>'ro', required=>0, predicate=>'has_cb'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | has pattern => (is=>'ro', required=>1, default=>sub { $_pattern }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has _strp => ( | ||||||||||||||||||||||||||||
| 23 | is=>'ro', | ||||||||||||||||||||||||||||
| 24 | required=>1, | ||||||||||||||||||||||||||||
| 25 | lazy=>1, | ||||||||||||||||||||||||||||
| 26 | 4 | ▶ | default=>sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 4 | return DateTime::Format::Strptime->new(time_zone=>$self->tz, pattern => $self->pattern); | |||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||
| 30 | 3 | ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | has below_min_msg => (is=>'ro', required=>1, default=>sub {_t 'below_min'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | has below_min_eq_msg => (is=>'ro', required=>1, default=>sub {_t 'below_min_eq'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | has above_max_msg => (is=>'ro', required=>1, default=>sub {_t 'above_max'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | has above_max_eq_msg => (is=>'ro', required=>1, default=>sub {_t 'above_max_eq'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | has invalid_date_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid_date'}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | has tz => (is=>'ro', required=>1, default=>sub { 'UTC' }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | sub normalize_shortcut { | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: called CC=6 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 2 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||
| 44 | 2 | ▶ | return +{ } if $arg eq '1'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | return +{ cb => $arg } if ((ref($arg)||'') eq 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $arg || ''
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
| |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | return +{ min => sub { pop->now } } if $arg eq 'is_future'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return +{ max => sub { pop->now } } if $arg eq 'is_past'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 15 | ▶ | sub validate_each { | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 15 | my ($self, $record, $attribute, $value, $opts) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 15 | ▶ | unless(defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $record->errors->add($attribute, $self->invalid_date_msg, $opts); | |||||||||||||||||||||||||||
| 56 | 0 | return; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 15 | my $dt = $self->_strp->parse_datetime($value); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 15 | ▶ | unless($dt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | $record->errors->add($attribute, $self->invalid_date_msg, $opts); | |||||||||||||||||||||||||||
| 63 | 2 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 13 | ▶ | if($self->has_min) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 9 | my $min = $self->_cb_value($record, $self->min); | |||||||||||||||||||||||||||
| 68 | 9 | my $min_dt_obj = $self->parse_if_needed($min); | |||||||||||||||||||||||||||
| 69 | 9 | ▶ | $record->errors->add($attribute, $self->below_min_msg, +{%$opts, min=>$min_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | unless $dt > $min_dt_obj; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 13 | ▶ | if($self->has_max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 4 | my $max = $self->_cb_value($record, $self->max); | |||||||||||||||||||||||||||
| 75 | 4 | my $max_dt_obj = $self->parse_if_needed($max); | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | $record->errors->add($attribute, $self->above_max_msg, +{%$opts, max=>$max_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | unless $dt < $max_dt_obj; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 13 | ▶ | if($self->has_min_eq) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | my $min = $self->_cb_value($record, $self->min_eq); | |||||||||||||||||||||||||||
| 82 | 4 | my $min_dt_obj = $self->parse_if_needed($min); | |||||||||||||||||||||||||||
| 83 | 4 | ▶ | $record->errors->add($attribute, $self->below_min_eq_msg, +{%$opts, min=>$min_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | unless $dt >= $min_dt_obj; | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 13 | ▶ | if($self->has_max_eq) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 4 | my $max = $self->_cb_value($record, $self->max_eq); | |||||||||||||||||||||||||||
| 89 | 4 | my $max_dt_obj = $self->parse_if_needed($max); | |||||||||||||||||||||||||||
| 90 | 4 | ▶ | $record->errors->add($attribute, $self->above_max_eq_msg, +{%$opts, max=>$max_dt_obj->strftime($self->pattern)}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | unless $dt <= $max_dt_obj; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 13 | ▶ | if($self->has_cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 8 | $self->cb->($record, $attribute, $dt, $self, $opts); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub to_pattern { | ||||||||||||||||||||||||||
|
Subroutine
to_pattern: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $dt) = @_; | |||||||||||||||||||||||||||
| 101 | 0 | return $dt->strftime($self->pattern); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 21 | ▶ | sub parse_if_needed { | ||||||||||||||||||||||||||
|
Subroutine
parse_if_needed: called CC=3 SCAR=15.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 21 | my ($self, $value_proto) = @_; | |||||||||||||||||||||||||||
| 106 | 21 | ▶ | return $value_proto if blessed($value_proto) && $value_proto->isa('DateTime'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($value_proto) and $value_proto->isa("DateTime")
MC/DC0%0/2
builtin::blessed($value_proto) and $value_proto->isa("DateTime")
builtin::blessed($value_proto)
$value_proto->isa("DateTime")
Truth table33%1/3
A and B
A= builtin::blessed($value_proto)B= $value_proto->isa("DateTime")
| |||||||||||||||||||||||||||||
| 107 | 0 | my $dt = $self->_strp->parse_datetime($value_proto); | |||||||||||||||||||||||||||
| 108 | 0 | return $dt; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub looks_like_a_date { | ||||||||||||||||||||||||||
|
Subroutine
looks_like_a_date: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 113 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 114 | 0 | return $dt; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub is_future { | ||||||||||||||||||||||||||
|
Subroutine
is_future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 120 | 0 | return $dt > $self->_datetime->today; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub is_past { | ||||||||||||||||||||||||||
|
Subroutine
is_past: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 0 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | my $dt = $self->parse_if_needed($value); | |||||||||||||||||||||||||||
| 126 | 0 | return $dt < $self->_datetime->today; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 13 | ▶ | sub now { shift->_datetime->now } | ||||||||||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub today { shift->_datetime->today } | ||||||||||||||||||||||||||
|
Subroutine
today: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub datetime { | ||||||||||||||||||||||||||
|
Subroutine
datetime: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 8 | my($self, %args) = @_; | |||||||||||||||||||||||||||
| 135 | 8 | ▶ | $args{time_zone} ||= $self->tz; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"time_zone"} ||= $self->tz
MC/DC0%0/2
$args{"time_zone"} ||= $self->tz
$args{"time_zone"}
$self->tz
Truth table33%1/3
A ||= B
A= $args{"time_zone"}B= $self->tz
| |||||||||||||||||||||||||||||
| 136 | 8 | return DateTime->new(%args); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 21 | ▶ | sub _datetime { | ||||||||||||||||||||||||||
|
Subroutine
_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 21 | my($self) = @_; | |||||||||||||||||||||||||||
| 141 | 21 | return 'DateTime'; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 8 | ▶ | sub years_ago { | ||||||||||||||||||||||||||
|
Subroutine
years_ago: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 8 | my($self, $years) = @_; | |||||||||||||||||||||||||||
| 146 | 8 | return $self->_datetime->now->subtract(years => $years); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub years_from_now { | ||||||||||||||||||||||||||
|
Subroutine
years_from_now: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 0 | my($self, $years) = @_; | |||||||||||||||||||||||||||
| 151 | 0 | return $self->_datetime->now->add(years => $years); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 NAME | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Valiant::Validator::Date - Verify that a value is is a standard Date (YYY-MM-DD) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | package Local::Test::Date; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | use Moo; | ||||||||||||||||||||||||||||
| 165 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | has birthday => (is=>'ro'); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | validates birthday => ( | ||||||||||||||||||||||||||||
| 170 | date => { | ||||||||||||||||||||||||||||
| 171 | min => sub { pop->years_ago(120) }, # Oldest person I think... | ||||||||||||||||||||||||||||
| 172 | max => sub { pop->now }, | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | my $object = Local::Test::Date->new(birthday=>'2100-01-01'); | ||||||||||||||||||||||||||||
| 177 | $object->validate; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | warn $object->errors->_dump; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | $VAR1 = { | ||||||||||||||||||||||||||||
| 182 | 'birthday' => [ | ||||||||||||||||||||||||||||
| 183 | 'chosen date can't be later than {{max}}', # In real life {{max}} would be | ||||||||||||||||||||||||||||
| 184 | # interpolated as DateTime->now | ||||||||||||||||||||||||||||
| 185 | ] | ||||||||||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Validates a string pattern to make sure its in a standard date (YYYY-MM-DD) format, | ||||||||||||||||||||||||||||
| 191 | which is commonly used in databases as a Date field and its also the canonical | ||||||||||||||||||||||||||||
| 192 | pattern for the HTML5 input date type. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Can accept a 'min' and 'max' attribute, which should be either a string in the | ||||||||||||||||||||||||||||
| 195 | standard form or a L<DateTime> object. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | If you are using the Form helpers the max and min attributes can be reflected into | ||||||||||||||||||||||||||||
| 198 | the date input type automatically. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 A NOTE ON TIMEZONES | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Please keep in mind that a lot of the shortcut helpers just call methods directly | ||||||||||||||||||||||||||||
| 203 | on L<DateTime> which means they are using the system timezone. If you are working | ||||||||||||||||||||||||||||
| 204 | with dates that are stored in a database you should be aware that the timezone | ||||||||||||||||||||||||||||
| 205 | of the database and the timezone of the system running your code might not be the | ||||||||||||||||||||||||||||
| 206 | same. This can lead to unexpected results. I don't have a lot of test cases | ||||||||||||||||||||||||||||
| 207 | around this, please shout out your experiences if you run into issues. You can use | ||||||||||||||||||||||||||||
| 208 | the C<tz> attribute (described below) to set the timezone of the L<DateTime> object | ||||||||||||||||||||||||||||
| 209 | we create locally if needed | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This validator supports the following attributes: | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 tz | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Default is 'UTC'. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | If you are working with dates that are stored in a database you should be aware | ||||||||||||||||||||||||||||
| 220 | that the timezone of the database and the timezone of the system running your code | ||||||||||||||||||||||||||||
| 221 | might not be the same. This can lead to unexpected results. You can use the C<tz> | ||||||||||||||||||||||||||||
| 222 | attribute to set the timezone of the L<DateTime> object we create locally if needed. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head2 pattern | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | This is a string pattern that is used by L<DateTime::Format::Strptime> that your | ||||||||||||||||||||||||||||
| 227 | date value must conform to (that is it must parse into a L<DateTime> object or the | ||||||||||||||||||||||||||||
| 228 | validation fails). The default is '%Y-%m-%d'. This is a common database format and | ||||||||||||||||||||||||||||
| 229 | is also used by HTML5 input date type fields. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 min | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | If provided set a bottom limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 234 | format or a L<DateTime> object. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 max | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | If provided set an upper limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 241 | format or a L<DateTime> object. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 min_eq | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | If provided set a bottom limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 248 | format or a L<DateTime> object. The date must be greater than or equal to this value. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head2 max_eq | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | If provided set an upper limit on the allowed date. Either a string in YYYY-MM-DD | ||||||||||||||||||||||||||||
| 255 | format or a L<DateTime> object. The date must be less than or equal to this value. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Value may also be a coderef so that you can set dynamic dates (such as always today) | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | The coderef receives the object being validated as its first argument (and the | ||||||||||||||||||||||||||||
| 260 | validator instance as its second, giving access to the date L</HELPERS>), so you | ||||||||||||||||||||||||||||
| 261 | can also compare against another attribute: | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | validates end_date => ( | ||||||||||||||||||||||||||||
| 264 | date => { min => sub { shift->start_date } }, | ||||||||||||||||||||||||||||
| 265 | ); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head2 cb | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | A code reference that lets you create custom validation logic. This is basically the | ||||||||||||||||||||||||||||
| 270 | same as the 'With' validator expect its only called IF the value is in valid date | ||||||||||||||||||||||||||||
| 271 | format and you get that date inflated into a L<DateTime> object instead of the raw | ||||||||||||||||||||||||||||
| 272 | string value. This makes it a little less work for you since you can skip those extra | ||||||||||||||||||||||||||||
| 273 | checks. Also the coderef will receive the validator type instance as the third argument | ||||||||||||||||||||||||||||
| 274 | so that you can take advantage of the type helpers (see below L<\HELPERS>). | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | package MyRecord | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | use Moo; | ||||||||||||||||||||||||||||
| 279 | use Valiant::Validations; | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | has attribute => (is=>'ro'); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | validates attribute => ( | ||||||||||||||||||||||||||||
| 284 | date => +{ | ||||||||||||||||||||||||||||
| 285 | min => sub { pop->years_ago(10) }, | ||||||||||||||||||||||||||||
| 286 | max => sub { pop->now }, | ||||||||||||||||||||||||||||
| 287 | cb => \&my_special_method, | ||||||||||||||||||||||||||||
| 288 | }, | ||||||||||||||||||||||||||||
| 289 | ); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | sub my_special_method { | ||||||||||||||||||||||||||||
| 292 | my ($self, $dt, $type) = @_; | ||||||||||||||||||||||||||||
| 293 | # In this case $dt is a DateTime object inflated from the value | ||||||||||||||||||||||||||||
| 294 | # of 'attribute'. This method won't get called if we previously | ||||||||||||||||||||||||||||
| 295 | # determine that the value isn't in proper YYY-MM-DD format. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # Custom validation stuff... | ||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 below_min_msg | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head2 above_max_msg | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head2 below_min_eq_msg | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =head2 above_max_eq_msg | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head2 invalid_date_msg | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | The error message / tag associated with the given validation failures. Default messages | ||||||||||||||||||||||||||||
| 311 | are provided. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head1 HELPERS | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | This validator provides the following helpers. These basically just wrap L<DateTime> | ||||||||||||||||||||||||||||
| 316 | and L<DateTime::Format::Strptime> so you can avoid having to create your own in your | ||||||||||||||||||||||||||||
| 317 | record / object classes. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head2 datetime | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Returns a raw blessed L<DateTime> object. If you pass a hash of arguments, those will | ||||||||||||||||||||||||||||
| 322 | be passed to C<new>. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 now | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | returns L<DateTime> now. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Please note that C<now> returns a L<DateTime> object that is both the current | ||||||||||||||||||||||||||||
| 329 | date AND current time. In the context of a date validator this might be less | ||||||||||||||||||||||||||||
| 330 | useful especially for comparisons since a date come out of a storage like a | ||||||||||||||||||||||||||||
| 331 | DB will be at hour zero, where as C<now> will likely be after that. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | If you want just the current date you should use C<today>. In fact I'd say that | ||||||||||||||||||||||||||||
| 334 | C<today> is the more useful of the two in the context of a date validator. I'm | ||||||||||||||||||||||||||||
| 335 | leaving C<now> for back compatibility. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =head2 today | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | returns L<DateTime> today. This is the current date at hour zero. If you | ||||||||||||||||||||||||||||
| 340 | are writing constraints like 'must be in the past' or 'must be in the future' | ||||||||||||||||||||||||||||
| 341 | you probably want to use this method instead of C<now>. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =head2 years_ago | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =head2 years_from_now | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Return a L<DateTime> object that is now plus or minus a given number of years. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head2 is_future | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =head2 is_past | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Given a L<DateTime> object (such as the value you are trying to validate), return true | ||||||||||||||||||||||||||||
| 354 | or false if it is either in the future or in the past. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | validates attribute => ( date => 1, ... ); | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Which is the same as: | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | validates attribute => ( | ||||||||||||||||||||||||||||
| 365 | date => +{ }, | ||||||||||||||||||||||||||||
| 366 | ); | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Not many saved characters but makes usage syntactically regular across validators. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | You can also invoke a custom callback with a shortcut | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | validates attribute => ( date => \&my_special_method, ... ); | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | sub my_special_method { | ||||||||||||||||||||||||||||
| 375 | my ($self, $dt, $type) = @_; | ||||||||||||||||||||||||||||
| 376 | # Custom validation stuff | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | Which is the same as: | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | validates attribute => ( | ||||||||||||||||||||||||||||
| 382 | date => +{ | ||||||||||||||||||||||||||||
| 383 | cb => \&my_special_method, | ||||||||||||||||||||||||||||
| 384 | }, | ||||||||||||||||||||||||||||
| 385 | ); | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Lastly you can specify that the date must be either future or past with a shortcut: | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | validates attribute => ( date => 'is_future', ... ); | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | Which is the same as: | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | validates attribute => ( | ||||||||||||||||||||||||||||
| 394 | date => +{ | ||||||||||||||||||||||||||||
| 395 | min => sub { pop->now }, | ||||||||||||||||||||||||||||
| 396 | }, | ||||||||||||||||||||||||||||
| 397 | ); | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | And: | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | validates attribute => ( date => 'is_past', ... ); | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | Which is the same as: | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | validates attribute => ( | ||||||||||||||||||||||||||||
| 406 | date => +{ | ||||||||||||||||||||||||||||
| 407 | max => sub { pop->now }, | ||||||||||||||||||||||||||||
| 408 | }, | ||||||||||||||||||||||||||||
| 409 | ); | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 414 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | See L<Valiant> | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | See L<Valiant> | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||