| 1 | package Term::Fabulous::Validator; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | class Term::Fabulous::Validator :strict(params) { | ||
| 13 | use Scalar::Util qw(blessed); | ||
| 14 | use Socket qw(inet_pton AF_INET AF_INET6); | ||
| 15 | use Term::Fabulous::Check qw(describe optional string); | ||
| 16 | |||
| 17 | # The named validators a widget parameter may ask for by name. | ||
| 18 | my @NAMES = qw(email integer number url hostname ip date time); | ||
| 19 | my %IS_NAMED = map { $_ => 1 } @NAMES; | ||
| 20 | |||
| 21 | my %DEFAULT_MESSAGE = ( | ||
| 22 | email => 'Please enter an e-mail address.', | ||
| 23 | url => 'Please enter a URL, such as https://example.com.', | ||
| 24 | hostname => 'Please enter a host name.', | ||
| 25 | ip => 'Please enter an IP address.', | ||
| 26 | date => 'Please enter a date as YYYY-MM-DD.', | ||
| 27 | time => 'Please enter a time as HH:MM.', | ||
| 28 | pattern => 'Please match the expected format.', | ||
| 29 | code => 'Invalid value.', | ||
| 30 | ); | ||
| 31 | |||
| 32 | my @DAYS_IN_MONTH = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); | ||
| 33 | |||
| 34 | field $name :param :reader; | ||
| 35 | field $message :param :reader; | ||
| 36 | field $accept :param :reader = undef; | ||
| 37 | |||
| 38 | # Called with the value; returns the error message, or false for a | ||
| 39 | # valid value. | ||
| 40 | field $check :param; | ||
| 41 | |||
| 42 | ADJUST { | ||
| 43 | $name = string( $self, name => $name ); | ||
| 44 | $message = string( $self, message => $message ); | ||
| 45 | die 'Term::Fabulous::Validator: check must be a code reference, got ' . describe($check) unless ref $check eq 'CODE'; | ||
| 46 | } | ||
| 47 | |||
| 48 | method check ($value) { | ||
| 49 | my $error = $check->($value); | ||
| 50 | return $error ? "$error" : undef; | ||
| 51 | } | ||
| 52 | |||
| 53 | # --------------------------------------------------------------------- | ||
| 54 | # Coercion: what a widget's validator parameter takes | ||
| 55 | # --------------------------------------------------------------------- | ||
| 56 | |||
| 57 | method coerce :common ($spec) { | ||
| 58 | return undef unless defined $spec; | ||
| 59 | return $spec if blessed($spec) && $spec->isa(__PACKAGE__); | ||
| 60 | return $class->pattern($spec) if ref $spec eq 'Regexp'; | ||
| 61 | return $class->code($spec) if ref $spec eq 'CODE'; | ||
| 62 | return $class->all(@$spec) if ref $spec eq 'ARRAY'; | ||
| 63 | die 'Term::Fabulous::Validator: a validator is a name, a regular expression, a code reference, a list of those or a Term::Fabulous::Validator, got ' . describe($spec) if ref $spec; | ||
| 64 | die "Term::Fabulous::Validator: unknown validator '$spec'; the names are " . join( ', ', @NAMES ) unless $IS_NAMED{$spec}; | ||
| 65 | return $class->$spec; | ||
| 66 | } | ||
| 67 | |||
| 68 | method names :common () { | ||
| 69 | return @NAMES; | ||
| 70 | } | ||
| 71 | |||
| 72 | # --------------------------------------------------------------------- | ||
| 73 | # Named validators | ||
| 74 | # --------------------------------------------------------------------- | ||
| 75 | |||
| 76 | # A validator from a predicate (true for a valid value) and the message | ||
| 77 | # of %options, or the default of $name. | ||
| 78 | method _from_test :common ( $name, $test, $accept, %options ) { | ||
| 79 | my $message = delete $options{message} // $DEFAULT_MESSAGE{$name}; | ||
| 80 | my $unknown = join ', ', sort keys %options; | ||
| 81 | die "Term::Fabulous::Validator: $name does not take $unknown" if length $unknown; | ||
| 82 | return $class->new( name => $name, message => $message, accept => $accept, check => sub ($value) { $test->($value) ? undef : $message } ); | ||
| 83 | } | ||
| 84 | |||
| 85 | # "Please enter a whole number between 1 and 10." and the like. | ||
| 86 | sub _range_message ( $noun, $min, $max ) { | ||
| 87 | my $range | ||
| 88 | = defined $min && defined $max ? " between $min and $max" | ||
| 89 | : defined $min ? " at least $min" | ||
| 90 | : defined $max ? " at most $max" | ||
| 91 | : ''; | ||
| 92 | return "Please enter a $noun$range."; | ||
| 93 | } | ||
| 94 | |||
| 95 | # A validator for numbers of a kind (matching $shape) within the min and | ||
| 96 | # max options, which are removed from %options. | ||
| 97 | method _within :common ( $name, $noun, $shape, $accept, %options ) { | ||
| 98 | my $min = optional( \&Term::Fabulous::Check::number, $class, "$name min", delete $options{min} ); | ||
| 99 | my $max = optional( \&Term::Fabulous::Check::number, $class, "$name max", delete $options{max} ); | ||
| 100 | die "Term::Fabulous::Validator: $name min $min is above max $max" if defined $min && defined $max && $min > $max; | ||
| 101 | $options{message} //= _range_message( $noun, $min, $max ); | ||
| 102 | my $test = sub ($value) { $value =~ $shape && ( !defined $min || $value >= $min ) && ( !defined $max || $value <= $max ) }; | ||
| 103 | return $class->_from_test( $name, $test, $accept, %options ); | ||
| 104 | } | ||
| 105 | |||
| 106 | method email :common (%options) { | ||
| 107 | return $class->_from_test( email => sub ($value) { $value =~ /\A[^@\s]+@[^@\s]+\.[^@\s]+\z/ }, undef, %options ); | ||
| 108 | } | ||
| 109 | |||
| 110 | method integer :common (%options) { | ||
| 111 | return $class->_within( integer => 'whole number', qr/\A-?[0-9]+\z/, '0-9-', %options ); | ||
| 112 | } | ||
| 113 | |||
| 114 | method number :common (%options) { | ||
| 115 | return $class->_within( number => 'number', qr/\A-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/, '0-9.-', %options ); | ||
| 116 | } | ||
| 117 | |||
| 118 | method url :common (%options) { | ||
| 119 | return $class->_from_test( url => sub ($value) { $value =~ m{\A[a-z][a-z0-9+.-]*://\S+\z}i }, undef, %options ); | ||
| 120 | } | ||
| 121 | |||
| 122 | method hostname :common (%options) { | ||
| 123 | my $label = qr/[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?/i; | ||
| 124 | return $class->_from_test( hostname => sub ($value) { length $value <= 253 && $value =~ /\A$label(?:\.$label)*\.?\z/ }, undef, %options ); | ||
| 125 | } | ||
| 126 | |||
| 127 | method ip :common (%options) { | ||
| 128 | return $class->_from_test( ip => \&_is_ip_address, undef, %options ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _is_ip_address ($value) { | ||
| 132 | return 0 unless $value =~ /\A[0-9a-f.:]+\z/i; | ||
| 133 | return defined inet_pton( AF_INET, $value ) || defined inet_pton( AF_INET6, $value ) ? 1 : 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | method date :common (%options) { | ||
| 137 | return $class->_from_test( date => \&_is_calendar_day, '0-9-', %options ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _is_calendar_day ($value) { | ||
| 141 | my ( $year, $month, $day ) = $value =~ /\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/ or return 0; | ||
| 142 | return 0 if $month < 1 || $month > 12 || $day < 1; | ||
| 143 | my $is_leap_year = $year % 4 == 0 && ( $year % 100 != 0 || $year % 400 == 0 ); | ||
| 144 | my $days = $DAYS_IN_MONTH[ $month - 1 ] + ( $month == 2 && $is_leap_year ? 1 : 0 ); | ||
| 145 | return $day <= $days ? 1 : 0; | ||
| 146 | } | ||
| 147 | |||
| 148 | method time :common (%options) { | ||
| 149 | return $class->_from_test( time => sub ($value) { $value =~ /\A(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?\z/ }, '0-9:', %options ); | ||
| 150 | } | ||
| 151 | |||
| 152 | # --------------------------------------------------------------------- | ||
| 153 | # Validators from a pattern, from code, and of several at once | ||
| 154 | # --------------------------------------------------------------------- | ||
| 155 | |||
| 156 | method pattern :common ( $regexp, %options ) { | ||
| 157 | die 'Term::Fabulous::Validator: pattern takes a regular expression, got ' . describe($regexp) unless ref $regexp eq 'Regexp'; | ||
| 158 | return $class->_from_test( pattern => sub ($value) { $value =~ $regexp }, undef, %options ); | ||
| 159 | } | ||
| 160 | |||
| 161 | # $code returns what is wrong with the value (a message, or any true | ||
| 162 | # value for the validator's message) and false when it is fine. | ||
| 163 | method code :common ( $code, %options ) { | ||
| 164 | die 'Term::Fabulous::Validator: code takes a code reference, got ' . describe($code) unless ref $code eq 'CODE'; | ||
| 165 | my $message = string( $class, message => delete $options{message} // $DEFAULT_MESSAGE{code} ); | ||
| 166 | my $unknown = join ', ', sort keys %options; | ||
| 167 | die "Term::Fabulous::Validator: code does not take $unknown" if length $unknown; | ||
| 168 | my $check = sub ($value) { | ||
| 169 | my $error = $code->($value); | ||
| 170 | return undef unless $error; | ||
| 171 | return ref $error || $error eq '1' ? $message : $error; | ||
| 172 | }; | ||
| 173 | return $class->new( name => 'code', message => $message, check => $check ); | ||
| 174 | } | ||
| 175 | |||
| 176 | # Every validator must pass; the first message wins. The suggested | ||
| 177 | # accept is the first one any of them has. | ||
| 178 | method all :common (@specs) { | ||
| 179 | my @validators = map { $class->coerce($_) } @specs; | ||
| 180 | die 'Term::Fabulous::Validator: all needs at least one validator' unless @validators; | ||
| 181 | return $validators[0] if @validators == 1; | ||
| 182 | |||
| 183 | my ($accept) = grep { defined } map { $_->accept } @validators; | ||
| 184 | my $check = sub ($value) { | ||
| 185 | foreach my $validator (@validators) { | ||
| 186 | my $error = $validator->check($value); | ||
| 187 | return $error if defined $error; | ||
| 188 | } | ||
| 189 | return undef; | ||
| 190 | }; | ||
| 191 | return $class->new( name => 'all', message => $validators[0]->message, accept => $accept, check => $check ); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; | ||
| 196 | |||
| 197 | __END__ |