Valiant/Validator.pm

filter: Statementstmt 100% 5 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 6 / 6 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« Valiant/Util/Exception/Strict.pm ↑ Summary Valiant/Validator/locale/errors.pl »
1 package Valiant::Validator;
2  
3 38 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 38 requires 'validate';
6  
7 38 1;
8  
9 =head1 NAME
10  
11 Valiant::Validator - A role to define the validator interface.
12  
13 =head1 SYNOPSIS
14  
15 package MySpecialValidator;
16  
17 use Moo;
18 with 'Valiant::Validator';
19  
20 sub validate {
21 my ($self, $object, $options) = @_;
22 # DO your custom validation here. Remember if you want to support
23 # strict and message you should pass $options to any errors:
24 # $object->errors->add('_base', 'Invalid', $options);
25 # This method doesn't have to return anything in particular.
26 }
27  
28 =head1 DESCRIPTION
29  
30 This is a base role for defining a validator. This should be a class that
31 defines a C<validate> method. Here's a more detailed example that shows
32 using a custom validator with a validatable object:
33  
34 package Local::Test::Validator::Box;
35  
36 use Moo;
37 with 'Valiant::Validator';
38  
39 has max_size => (is=>'ro', required=>1);
40  
41 sub validate {
42 my ($self, $record, $opts) = @_;
43 my $size = $record->height + $record->width + $record->length;
44 if($size > $self->max_size) {
45 $record->errors->add(_base=>"Total of all size cannot exceed ${\$self->max_size}", $opts),
46 }
47 }
48  
49 package Local::Test::Box;
50  
51 use Moo;
52 use Valiant::Validations;
53  
54 has [qw(height width length)] => (is=>'ro', required=>1);
55  
56 validates [qw(height width length)] => (numericality=>+{});
57  
58 validates_with 'Box', max_size=>25;
59 validates_with 'Box', max_size=>50, on=>'big', message=>'Big for Big!!';
60 validates_with 'Box', max_size=>30, on=>'big', if=>'is_very_tall';
61  
62 sub is_very_tall {
63 my ($self) = @_;
64 return $self->height > 30 ? 1:0;
65 }
66  
67 When used with C<validates_with> we filter any extra arguments outside the globals
68 (C<on>, C<if/unless>, C<message>, C<strict>) and pass them as init args when creating
69 the validator.
70  
71 A Validator is created once when the class uses it and exists for the full life cycle
72 of the validatable object.
73  
74 Generally you would write a validator class like this when the validation is very complex
75 and cannot be tied to a specific attribute. If it can be tied to an attribute then you
76 might prefer to use L<Valiant::Validator::Each>.
77  
78 =head1 PREPACKAGED VALIDATOR CLASSES
79  
80 The following attribute validator classes are shipped with L<Valiant>. Please see the package POD for
81 usage details (this is only a sparse summary)
82  
83 =head2 Absence
84  
85 Checks that a value is absent (undefinef or empty).
86  
87 See L<Valiant::Validator::Absence> for details.
88  
89 =head2 Acceptance
90  
91 Checks that a value was accepted (for example a "terms of service" checkbox).
92  
93 See L<Valiant::Validator::Acceptance> for details.
94  
95 =head2 Array
96  
97 Validations on an array value. Has options for nested errors when the array contains objects that
98 themselves are validatible.
99  
100 See L<Valiant::Validator::Array> for details.
101  
102 =head2 Boolean
103  
104 Returns errors messages based on the boolean state of an attribute.
105  
106 See L<Valiant::Validator::Boolean> for details.
107  
108 =head2 Check
109  
110 Use your existing L<Type::Tiny> constraints with L<Valiant>
111  
112 See L<Valiant::Validator::Check> for details.
113  
114 =head2 Confirmation
115  
116 Add a confirmation error check. Used for when you want to verify that a given field is correct
117 (such as when a user submits a new password or an email address).
118  
119 See L<Valiant::Validator::Confirmation> for details.
120  
121 =head2 Date
122  
123 Value must conform to standard date format (default is YYYY-MM-DD or eg 2000-01-01) and be a valid date.
124  
125 See L<Valiant::Validator::Date> for details.
126  
127 =head2 Exclusion
128  
129 Value cannot match a fixed list.
130  
131 See L<Valiant::Validator::Exclusion> for details.
132  
133  
134 =head2 Format
135  
136 Value must be a string tht matched a given format or regular expression.
137  
138 See L<Valiant::Validator::Format> for details.
139  
140 =head2 Hash
141  
142 Value is a hashref whose values pass a set of validation rules.
143  
144 See L<Valiant::Validator::Hash> for details.
145  
146 =head2 Inclusion
147  
148 Value must be one of a fixed list
149  
150 See L<Valiant::Validator::Inclusion> for details.
151  
152 =head2 Length
153  
154 Value must be a string with given minimum and maximum lengths.
155  
156 See L<Valiant::Validator::Length> for details.
157  
158 =head2 Numericality
159  
160 Validate various types of numbers.
161  
162 See L<Valiant::Validator::Numericality> for details.
163  
164 =head2 Object
165  
166 Value is an object. Allows one to have nested validations when the object itself can be validated.
167  
168 See L<Valiant::Validator::Object> for details.
169  
170 =head2 OnlyOf
171  
172 Validates that only one or more of a group of attributes is defined.
173  
174 See L<Valiant::Validator::OnlyOf> for details.
175  
176 =head2 Presence
177  
178 That the value is defined and not empty
179  
180 See L<Valiant::Validator::Presence> for details.
181  
182 =head2 Unique
183  
184 That the value is unique based on some custom logic that your class must provide.
185  
186 See L<Valiant::Validator::Unique> for details.
187  
188 =head2 With
189  
190 Use a subroutine reference or the name of a method on your class to provide validation.
191  
192 See L<Valiant::Validator::With> for details.
193  
194 =head2 Scalar
195  
196 Verifies that the value is a string or number, basically a Perl scalar.
197  
198 See L<Valiant::Validator::Scalar> for details.
199  
200 =head2 Special Validators
201  
202 The following validators are not considered for end users but have documentation you might
203 find useful in furthering your knowledge of L<Valiant>: L<Valiant::Validator::Collection>,
204 L<Valiant::Validator::Each>.
205  
206 =head1 SEE ALSO
207
208 L<Valiant>, L<Valiant::Validator::Each>.
209 =head1 AUTHOR
210
211 See L<Valiant>
212  
213 =head1 COPYRIGHT & LICENSE
214
215 See L<Valiant>
216  
217 =cut
« Valiant/Util/Exception/Strict.pm ↑ Summary Valiant/Validator/locale/errors.pl »