/usr/share/perl5/Mojolicious/Validator.pm is in libmojolicious-perl 5.54+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package Mojolicious::Validator;
use Mojo::Base -base;
use Mojolicious::Validator::Validation;
has checks => sub {
{equal_to => \&_equal_to, in => \&_in, like => \&_like, size => \&_size};
};
sub add_check {
my ($self, $name, $cb) = @_;
$self->checks->{$name} = $cb;
return $self;
}
sub validation {
Mojolicious::Validator::Validation->new(validator => shift);
}
sub _equal_to {
my ($validation, $name, $value, $to) = @_;
return 1 unless defined(my $other = $validation->input->{$to});
return $value ne $other;
}
sub _in {
my ($validation, $name, $value) = (shift, shift, shift);
$value eq $_ && return undef for @_;
return 1;
}
sub _like { $_[2] !~ $_[3] }
sub _size {
my ($validation, $name, $value, $min, $max) = @_;
my $len = length $value;
return $len < $min || $len > $max;
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Validator - Validate parameter
=head1 SYNOPSIS
use Mojolicious::Validator;
my $validator = Mojolicious::Validator->new;
my $validation = $validator->validation;
$validation->input({foo => 'bar'});
$validation->required('foo')->like(qr/ar$/);
say $validation->param('foo');
=head1 DESCRIPTION
L<Mojolicious::Validator> validates parameters for L<Mojolicious>.
=head1 CHECKS
These validation checks are available by default.
=head2 equal_to
$validation->equal_to('foo');
Value needs to be equal to the value of another field.
=head2 in
$validation->in(qw(foo bar baz));
Value needs to match one of the values in the list.
=head2 like
$validation->like(qr/^[A-Z]/);
Value needs to match the regular expression.
=head2 size
$validation->size(2, 5);
Value length in characters needs to be between these two values.
=head1 ATTRIBUTES
L<Mojolicious::Validator> implements the following attributes.
=head2 checks
my $checks = $validator->checks;
$validator = $validator->checks({size => sub {...}});
Registered validation checks, by default only L</"equal_to">, L</"in">,
L</"like"> and L</"size"> are already defined.
=head1 METHODS
L<Mojolicious::Validator> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 add_check
$validator = $validator->add_check(size => sub {...});
Register a new validation check.
=head2 validation
my $validation = $validator->validation;
Build L<Mojolicious::Validator::Validation> object to perform validations.
my $validation = $validator->validation;
$validation->input({foo => 'bar'});
$validation->required('foo')->size(1, 5);
say $validation->param('foo');
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|