-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
277 lines (225 loc) · 8.56 KB
/
README
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
NAME
Dancer2::Plugin::DataTransposeValidator - Data::Transpose::Validator
plugin for Dancer2
VERSION
Version 0.100
SYNOPSIS
use Dancer2::Plugin::DataTransposeValidator;
post '/' => sub {
my $params = params;
my $data = validator($params, 'rules-file');
if ( $data->{valid} ) { ... }
}
DESCRIPTION
Dancer2 plugin for for Data::Transpose::Validator
FUNCTIONS
This module exports the single function "validator".
validator( $params, $rules, @additional_args? )
Where:
$params is a hash reference of parameters to be validated.
$rules is one of:
* the name of a rule sub if you are using "rules_class"
* the name of a rule file if you are using "rules_dir"
* a hash reference of rules
* a code reference that will return a hashref of rules
Any optional @additional_args are passed as arguments to code
references/subs.
A hash reference with the following keys is returned:
* valid
A boolean 1/0 showing whether the parameters validated correctly or
not.
* values
The transposed values as a hash reference.
* errors
A hash reference containing one key for each parameter which failed
validation. See "errors_hash" in "CONFIGURATION" for an explanation
of what the value of each parameter key will be.
* css
A hash reference containing one key for each parameter which failed
validation. The value for each parameter is a css class. See
"css_error_class" in "CONFIGURATION".
CONFIGURATION
The following configuration settings are available (defaults are shown
here):
plugins:
DataTransposeValidator:
css_error_class: has-error
errors_hash: 0
rules_class: MyApp::ValidationRules
# OR:
rules_dir: validation
css_error_class
The class returned as a value for parameters in the css key of the hash
reference returned by "validator".
errors_hash
This can has a number of different values:
* A false value (the default) means that only a single scalar error
string will be returned for each parameter error. This will be the
first error returned for the parameter by "errors_hash" in
Data::Transpose::Validator.
* joined
All errors for a parameter will be returned joined by a full stop
and a space.
* arrayref
All errors for a parameter will be returned as an array reference.
rules_class
This is much preferred over "rules_dir" since it does not eval external
files.
This is a class (package) name such as "MyApp::Validator::Rules". There
should be one sub for each rule name inside that class which returns a
hash reference. See "RULES CLASS" for examples.
rules_dir
Subdirectory of "appdir" in Dancer2::Config in which rules files are
stored. NOTE: We recommend you do not use this approach since the rules
files are eval'ed with all the security risks that entails. Please use
"rules_class" instead. You have been warned. See "RULES DIR" for
examples.
RULES CLASS
The rules class allows the "validator" to be configured using all
options available in Data::Transpose::Validator. The rules class must
contain one sub for each rule name which will be passed any
@optional_args.
package MyApp::ValidationRules;
sub register {
# simple hashref
+{
options => {
stripwhite => 1,
collapse_whitespace => 1,
requireall => 1,
},
prepare => {
email => {
validator => "EmailValid",
},
email2 => {
validator => "EmailValid",
},
emails => {
validator => 'Group',
fields => [ "email", "email2" ],
},
},
};
}
sub change_password {
# args and hashref
my %args = @_;
+{
options => {
requireall => 1,
},
prepare => {
old_password => {
required => 1,
validator => sub {
if ( $args{logged_in_user}->check_password( $_[0] ) ) {
return 1;
}
else {
return ( undef, "Password incorrect" );
}
},
},
password => {
required => 1,
validator => {
class => 'PasswordPolicy',
options => {
username => $args{logged_in_user}->username,
minlength => 8,
maxlength => 70,
patternlength => 4,
mindiffchars => 5,
disabled => {
digits => 1,
mixed => 1,
specials => 1,
}
}
}
},
confirm_password => { required => 1 },
passwords => {
validator => 'Group',
fields => [ "password", "confirm_password" ],
},
},
};
}
1;
RULES DIR
The rules file format allows the "validator" to be configured using all
options available in Data::Transpose::Validator. The rules file must
contain a valid hash reference, e.g.:
{
options => {
stripwhite => 1,
collapse_whitespace => 1,
requireall => 0,
unknown => "fail",
missing => "undefine",
},
prepare => {
email => {
validator => "EmailValid",
required => 1,
},
email2 => {
validator => {
class => "MyValidator::EmailValid",
absolute => 1,
}
},
field4 => {
validator => {
sub {
my $field = shift;
if ( $field =~ /^\d+/ && $field > 0 ) {
return 1;
}
else {
return ( undef, "Not a positive integer" );
}
}
}
}
}
}
Note that the value of the "prepare" key must be a hash reference since
the array reference form of "prepare" in Data::Transpose::Validator is
not supported.
As an alternative the rules file can contain a code reference, e.g.:
sub {
my $username = shift;
return {
options => {
stripwhite => 1,
},
prepare => {
password => {
validator => {
class => 'PasswordPolicy',
options => {
username => $username,
minlength => 8,
}
}
}
}
};
}
The code reference receives the @additional_args passed to "validator".
The code reference must return a valid hash reference.
SEE ALSO
Dancer2, Data::Transpose
ACKNOWLEDGEMENTS
Alexey Kolganov for Dancer::Plugin::ValidateTiny which inspired a number
of aspects of the original version of this plugin.
Stefan Hornburg (Racke) for his valuable feedback.
AUTHOR
Peter Mottram (SysPete), "<[email protected]>"
COPYRIGHT AND LICENSE
Copyright 2015-2016 Peter Mottram (SysPete).
This program is free software; you can redistribute it and/or modify it
under the same terms as the Perl 5 programming language system itself.