-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
175 lines (137 loc) · 5.16 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
NAME
Dancer::Plugin::DataTransposeValidator - Data::Transpose::Validator
plugin for Dancer
VERSION
Version 0.009
SYNOPSIS
use Dancer::Plugin::DataTransposeValidator;
post '/' => sub {
my $params = params;
my $data = validator($params, 'rules-file');
if ( $data->{valid} ) { ... }
}
DESCRIPTION
Dancer plugin for for Data::Transpose::Validator
FUNCTIONS
This module exports the single function "validator".
validator( $params, $rules_file, @additional_args )
Arguments should be a hash reference of parameters to be validated and
the name of the rules file to use. Any @additional_args are passed as
arguments to the rules file only if it is a code reference. See "RULES
FILE".
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".
RULES FILE
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.
CONFIGURATION
The following configuration settings are available (defaults are shown
here):
plugins:
DataTransposeValidator:
css_error_class: has-error
errors_hash: 0
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_dir
Subdirectory of "appdir" in Dancer::Config in which rules files are
stored.
ACKNOWLEDGEMENTS
Alexey Kolganov for Dancer::Plugin::ValidateTiny which inspired a number
of aspects of this plugin.
SEE ALSO
Dancer::Plugin::ValidateTiny Dancer::Plugin::FormValidator
Dancer::Plugin::DataFu
AUTHOR
Peter Mottram (SysPete), "<[email protected]>"
CONTRIBUTORS
Slaven Rezić (SREZIC), "<[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.