source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Validator.pm@ 32591

Last change on this file since 32591 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 3.7 KB
Line 
1package Mojolicious::Validator;
2use Mojo::Base -base;
3
4use Mojo::Util 'trim';
5use Mojolicious::Validator::Validation;
6
7has checks => sub {
8 {
9 equal_to => \&_equal_to,
10 in => \&_in,
11 like => sub { $_[2] !~ $_[3] },
12 num => \&_num,
13 size => \&_size,
14 upload => sub { !ref $_[2] || !$_[2]->isa('Mojo::Upload') }
15 };
16};
17has filters => sub { {trim => \&_trim} };
18
19sub add_check { $_[0]->checks->{$_[1]} = $_[2] and return $_[0] }
20sub add_filter { $_[0]->filters->{$_[1]} = $_[2] and return $_[0] }
21
22sub validation {
23 Mojolicious::Validator::Validation->new(validator => shift);
24}
25
26sub _equal_to {
27 my ($v, $name, $value, $to) = @_;
28 return 1 unless defined(my $other = $v->input->{$to});
29 return $value ne $other;
30}
31
32sub _in {
33 my ($v, $name, $value) = (shift, shift, shift);
34 $value eq $_ && return undef for @_;
35 return 1;
36}
37
38sub _num {
39 my ($v, $name, $value, $min, $max) = @_;
40 return 1 if $value !~ /^[0-9]+$/;
41 return defined $min && $min > $value || defined $max && $max < $value;
42}
43
44sub _size {
45 my ($v, $name, $value, $min, $max) = @_;
46 my $len = ref $value ? $value->size : length $value;
47 return $len < $min || $len > $max;
48}
49
50sub _trim { trim $_[2] // '' }
51
521;
53
54=encoding utf8
55
56=head1 NAME
57
58Mojolicious::Validator - Validate values
59
60=head1 SYNOPSIS
61
62 use Mojolicious::Validator;
63
64 my $validator = Mojolicious::Validator->new;
65 my $v = $validator->validation;
66 $v->input({foo => 'bar'});
67 $v->required('foo')->like(qr/ar$/);
68 say $v->param('foo');
69
70=head1 DESCRIPTION
71
72L<Mojolicious::Validator> validates values for L<Mojolicious>.
73
74=head1 CHECKS
75
76These validation checks are available by default.
77
78=head2 equal_to
79
80 $v = $v->equal_to('foo');
81
82String value needs to be equal to the value of another field.
83
84=head2 in
85
86 $v = $v->in('foo', 'bar', 'baz');
87
88String value needs to match one of the values in the list.
89
90=head2 like
91
92 $v = $v->like(qr/^[A-Z]/);
93
94String value needs to match the regular expression.
95
96=head2 num
97
98 $v = $v->num;
99 $v = $v->num(2, 5);
100 $v = $v->num(2, undef);
101 $v = $v->num(undef, 5);
102
103String value needs to be a non-fractional number and if provided in the given
104range.
105
106=head2 size
107
108 $v = $v->size(2, 5);
109
110String value length or size of L<Mojo::Upload> object in bytes needs to be
111between these two values.
112
113=head2 upload
114
115 $v = $v->upload;
116
117Value needs to be a L<Mojo::Upload> object, representing a file upload.
118
119=head1 FILTERS
120
121These filters are available by default.
122
123=head2 trim
124
125 $v = $v->optional('foo', 'trim');
126
127Trim whitespace characters from both ends of string value with
128L<Mojo::Util/"trim">.
129
130=head1 ATTRIBUTES
131
132L<Mojolicious::Validator> implements the following attributes.
133
134=head2 checks
135
136 my $checks = $validator->checks;
137 $validator = $validator->checks({size => sub {...}});
138
139Registered validation checks, by default only L</"equal_to">, L</"in">,
140L</"like">, L</"num">, L</"size"> and L</"upload"> are already defined.
141
142=head1 METHODS
143
144L<Mojolicious::Validator> inherits all methods from L<Mojo::Base> and
145implements the following new ones.
146
147=head2 add_check
148
149 $validator = $validator->add_check(size => sub {...});
150
151Register a validation check.
152
153 $validator->add_check(foo => sub {
154 my ($v, $name, $value, @args) = @_;
155 ...
156 return undef;
157 });
158
159=head2 add_filter
160
161 $validator = $validator->add_filter(trim => sub {...});
162
163Register a new filter.
164
165 $validator->add_filter(foo => sub {
166 my ($v, $name, $value) = @_;
167 ...
168 return $value;
169 });
170
171=head2 validation
172
173 my $v = $validator->validation;
174
175Build L<Mojolicious::Validator::Validation> object to perform validations.
176
177 my $v = $validator->validation;
178 $v->input({foo => 'bar'});
179 $v->required('foo')->size(1, 5);
180 say $v->param('foo');
181
182=head1 SEE ALSO
183
184L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
185
186=cut
Note: See TracBrowser for help on using the repository browser.