| 1 | package Validator::Lazy; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Validator::Lazy | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Version 0.03 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Validator::Lazy; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $form = { | ||||||||||||||||||||||||||||
| 20 | first_name => 'John', | ||||||||||||||||||||||||||||
| 21 | last_name => 'John', | ||||||||||||||||||||||||||||
| 22 | phone => '+123456789', | ||||||||||||||||||||||||||||
| 23 | fax => '', | ||||||||||||||||||||||||||||
| 24 | email => 'john@site.com', | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my $config = q~ | ||||||||||||||||||||||||||||
| 28 | '/^(first|last)_name$/': | ||||||||||||||||||||||||||||
| 29 | - Trim: 'all' | ||||||||||||||||||||||||||||
| 30 | - Required | ||||||||||||||||||||||||||||
| 31 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | '[phone,fax]': Phone | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | phone: Required | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | email: [ 'Required', 'Email' ] | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | myform: | ||||||||||||||||||||||||||||
| 40 | - Form: [ 'first_name', 'last_name', 'phone', 'fax', 'email' ] | ||||||||||||||||||||||||||||
| 41 | ~; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $validator = Validator::Lazy->new( $config ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $ok = $v->check( myform => $form ); # true / false | ||||||||||||||||||||||||||||
| 46 | OR | ||||||||||||||||||||||||||||
| 47 | my ( $ok, $data ) = $validator->check( myform => $form ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | use Data::Dumper; | ||||||||||||||||||||||||||||
| 50 | say Dumper $v->errors; # [ { code => any_error_code, field => field_with_error, data => { variable data for more accurate error definition } } ] | ||||||||||||||||||||||||||||
| 51 | say Dumper $v->warnings; # [ { code => any_warn_code, field => field_with_warn, data => { variable data for more accurate warn definition } } ] | ||||||||||||||||||||||||||||
| 52 | say Dumper $v->data; # Fixed data. For example trimmed strings, corrected char case, etc... | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | For more details please see test files. For example t/14-int_roles-form.t can be the no.1 to see. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Validator for different sets of data with easy and simple sets of rules. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Features: | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =over 4 | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =item * Very simple and relatively short configuration | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item * Allows to check almost everything: small web forms as well as deep multilevel structures with intersected params. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item * Some predefined check rules/roles ( Case, CountryCode, Email, Form, IP, IsIn, MinMax, Phone, RegExp, Required, Trim ) | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item * Easy way to create your own simple rules/roles and use them as native as predefined | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item * No need to make and pass extra code to validator from caller during validation. Just a sets of rules. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item * No intersections and relations between rules (when you fix a one field validation, your other fields are be safe) | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | So, how it works... | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 Configuration/Init | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my $v = Validator::Lazy->new( $config ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | $config can be a string, or text, or hashref | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =over 4 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item * when config is a string, which looks like a filename,- validator will try to read configuration from this file | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =item * when config is a text, validator will think, that it's a pure YAML or JSON and will try to parse it | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item * when config is a hashref, validator just will apply it without further ado | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =back | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | In all cases, finnaly we have a hashref: | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | field_definition1: | ||||||||||||||||||||||||||||
| 99 | - rule1: param1 | ||||||||||||||||||||||||||||
| 100 | - rule2: param2 | ||||||||||||||||||||||||||||
| 101 | ... | ||||||||||||||||||||||||||||
| 102 | field_definition2: | ||||||||||||||||||||||||||||
| 103 | ... | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | in case, when a field have 1 rule without params, we can give a scalar instead of Arrayref[HashRef]: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | field_definition1: rule1 | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | B<each rule can be an internal validator role, or external role, or a key from configuration> | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | internal role example: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | user_phone: Phone | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | external/your role example: | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | part_number: Where::Your::Role::IS::YourFieldRole | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | predefined role example: | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Here, we predefine/declare the new field: "any_phone" | ||||||||||||||||||||||||||||
| 123 | any_phone: Phone | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # And now we can use it within other field rules: | ||||||||||||||||||||||||||||
| 126 | user_phone: | ||||||||||||||||||||||||||||
| 127 | - Required | ||||||||||||||||||||||||||||
| 128 | - any_phone | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # And here we have alias or clone for any_phone | ||||||||||||||||||||||||||||
| 131 | user_fax: any_phone | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | B<each config key allows you to match more than one field of your data for checking:> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | for example we need to check a web form like this: | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | firstname: 'John' | ||||||||||||||||||||||||||||
| 140 | lastname: 'John' | ||||||||||||||||||||||||||||
| 141 | phone: '+123456789' | ||||||||||||||||||||||||||||
| 142 | fax: '' | ||||||||||||||||||||||||||||
| 143 | email: 'john@site.com' | ||||||||||||||||||||||||||||
| 144 | secretkey: 'x1x2x3x4' | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | at first we make a config: | ||||||||||||||||||||||||||||
| 147 | firstname and lastname has equal requirements, so we put them in one key: | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | '[firstname,lastname]': | ||||||||||||||||||||||||||||
| 150 | - Required | ||||||||||||||||||||||||||||
| 151 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | or like this | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | '/^(first|last)_name$/': | ||||||||||||||||||||||||||||
| 156 | - Required | ||||||||||||||||||||||||||||
| 157 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | phone and fax has similar requirements, but phone is required and fax is optional: | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | [phone,fax]: Phone | ||||||||||||||||||||||||||||
| 162 | phone: Required | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | and email: | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | email: | ||||||||||||||||||||||||||||
| 167 | - Required | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | let's assume, that secretkey has some tricky checks, so it should be checked with external code. | ||||||||||||||||||||||||||||
| 171 | We should write a role (how to do this - see far below), and now just use it: | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | secretkey: | ||||||||||||||||||||||||||||
| 174 | - Required | ||||||||||||||||||||||||||||
| 175 | - Path::To::Your::Roles::SecretKey : { secretkey: param } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | B<Combining all together, and we have:> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # Config | ||||||||||||||||||||||||||||
| 181 | my $config = q~ | ||||||||||||||||||||||||||||
| 182 | '/(first|last)_name/': | ||||||||||||||||||||||||||||
| 183 | - Trim: 'all' | ||||||||||||||||||||||||||||
| 184 | - Required | ||||||||||||||||||||||||||||
| 185 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | '[phone,fax]': Phone | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | phone: Required | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | email: | ||||||||||||||||||||||||||||
| 192 | - Required | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | secretkey: | ||||||||||||||||||||||||||||
| 196 | - Required | ||||||||||||||||||||||||||||
| 197 | - 'Path::To::Your::Roles::SecretKey': { secretkey: 'param' } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | myform: | ||||||||||||||||||||||||||||
| 200 | - Form: | ||||||||||||||||||||||||||||
| 201 | - first_name | ||||||||||||||||||||||||||||
| 202 | - last_name | ||||||||||||||||||||||||||||
| 203 | - phone | ||||||||||||||||||||||||||||
| 204 | - fax | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | - secretkey | ||||||||||||||||||||||||||||
| 207 | ~; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # Form to check: | ||||||||||||||||||||||||||||
| 210 | my $form = { | ||||||||||||||||||||||||||||
| 211 | first_name => 'John', | ||||||||||||||||||||||||||||
| 212 | last_name => 'John', | ||||||||||||||||||||||||||||
| 213 | phone => '+123456789', | ||||||||||||||||||||||||||||
| 214 | fax => '', | ||||||||||||||||||||||||||||
| 215 | email => 'john@site.com', | ||||||||||||||||||||||||||||
| 216 | secretkey => 'x1x2x3x4', | ||||||||||||||||||||||||||||
| 217 | }; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $validator = Validator::Lazy->new( $config ); | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | my $ok = $validator->check( $form ); # true | ||||||||||||||||||||||||||||
| 222 | or | ||||||||||||||||||||||||||||
| 223 | my ( $ok, $data ) = $validator->check( myform => $form ); | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 Writing a roles | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Let's write a role, that is required for example above: | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | package Path::To::Your::Roles::SecretKey; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | use Modern::Perl; | ||||||||||||||||||||||||||||
| 233 | use Moose::Role; | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | sub check { | ||||||||||||||||||||||||||||
| 236 | my ( $self, $value, $param ) = @_; | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | $self; # is a validator object | ||||||||||||||||||||||||||||
| 239 | $value; # is a value to check from form | ||||||||||||||||||||||||||||
| 240 | $param; # is a param = {secretkey:param} | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | $self->add_error( ); # This will add to validator dafault error | ||||||||||||||||||||||||||||
| 243 | $self->add_error( 'CUSTOM_ERROR_CODE' ); # Custom error code will be plased instead of default | ||||||||||||||||||||||||||||
| 244 | $self->add_error( 'CUSTOM_ERROR_CODE', { some useful data } ); # Also, we can pass to error some data, and use it somwhere outside | ||||||||||||||||||||||||||||
| 245 | $self->add_error( { some useful data } ); # Default code, but some useful data | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | $self->add_warning(); # All the same as with errors; | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | return $value; # You should do it! Othervise you just lost your value. | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # Also, you can use in check roles: | ||||||||||||||||||||||||||||
| 253 | sub before_check { similar to check } | ||||||||||||||||||||||||||||
| 254 | sub after_check { similar to check } | ||||||||||||||||||||||||||||
| 255 | # They are working exactly as check, but allow to do some code separation | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 1; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 Forms | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Validator has a predefined "Form" role, let's, use it: | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # YAML sample: | ||||||||||||||||||||||||||||
| 264 | '/(first|last)_name/': | ||||||||||||||||||||||||||||
| 265 | - Trim: 'all' | ||||||||||||||||||||||||||||
| 266 | - Required | ||||||||||||||||||||||||||||
| 267 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | full_name: | ||||||||||||||||||||||||||||
| 270 | Form: [first_name, last_name ] | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Corrected example from above: | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | my $form = { | ||||||||||||||||||||||||||||
| 275 | full_name => { | ||||||||||||||||||||||||||||
| 276 | first_name => 'John', | ||||||||||||||||||||||||||||
| 277 | last_name => 'Smith', | ||||||||||||||||||||||||||||
| 278 | }, | ||||||||||||||||||||||||||||
| 279 | phone => '+123456789', | ||||||||||||||||||||||||||||
| 280 | fax => '', | ||||||||||||||||||||||||||||
| 281 | email => 'john@site.com', | ||||||||||||||||||||||||||||
| 282 | secretkey => 'x1x2x3x4', | ||||||||||||||||||||||||||||
| 283 | }; | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | my $config = q~ | ||||||||||||||||||||||||||||
| 286 | '/(first|last)_name/': | ||||||||||||||||||||||||||||
| 287 | - Trim: 'all' | ||||||||||||||||||||||||||||
| 288 | - Required | ||||||||||||||||||||||||||||
| 289 | - RegExp: '/^[A-Zaz]{3,64}$/' | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | full_name: | ||||||||||||||||||||||||||||
| 292 | - Form: [first_name, last_name ] | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | '[phone,fax]': Phone | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | phone: Required | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | email: Email | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | secretkey: | ||||||||||||||||||||||||||||
| 301 | - Required | ||||||||||||||||||||||||||||
| 302 | - 'Path::To::Your::Roles::SecretKey': { secretkey: param } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | myform: | ||||||||||||||||||||||||||||
| 305 | - Form: | ||||||||||||||||||||||||||||
| 306 | - full_name | ||||||||||||||||||||||||||||
| 307 | - phone | ||||||||||||||||||||||||||||
| 308 | - fax | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | - secretkey | ||||||||||||||||||||||||||||
| 311 | ~; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | my $validator = Validator::Lazy->new( $config ); | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | my $ok = $validator->check( $form ); # true | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | or | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | my ( $ok, $data ) = $validator->check( myform => $form ); | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =head2 warnings/errors/results | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | $validator->check( $data ); can return 1 or 2 params; | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | the 1st - can be true or false - is a result of check. | ||||||||||||||||||||||||||||
| 326 | the 2nd - form data, but corrected. For example, if Trim used, some strings will be trimmed from spaces. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | after $validator->check() validators has: | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | $validator->errors - list = [ { code => 'SOME_ERROR_CODE', field => 'some_field', data => 'some data' }, ... ] | ||||||||||||||||||||||||||||
| 331 | $validator->error_codes - list of codes = [ 'SOME_ERROR_CODE', ... ] | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | And all the same with warnings... | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | when an external role adds an error, then, by default it has a error/warning code: | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | role is "Path::To::Your::Roles::SecretKey" | ||||||||||||||||||||||||||||
| 338 | default error code is PATH_TO_YOUR_ROLES_SECRETKEY_ERROR | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | when a subform adds an error, then default field name is generated as form_name + '_' + field_name: | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | myform: | ||||||||||||||||||||||||||||
| 343 | - Form: | ||||||||||||||||||||||||||||
| 344 | - full_name | ||||||||||||||||||||||||||||
| 345 | - Required | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | when full_name is absent, we have an error: | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | { code => 'REQUIRED_ERROR', field => 'myform_full_name', data => {} } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 Contestants | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | There are huge amount of other validators on CPAN. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | All of them have their pros and cons, but all are very different. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | I do not wish to write here detailed review of each, so let's just list them all: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | https://metacpan.org/pod/Data::Validator | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | https://metacpan.org/pod/Validator::LIVR | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | https://metacpan.org/pod/QBit::Validator | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | https://metacpan.org/pod/Input::Validator | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | https://metacpan.org/pod/MojoX::Validator | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | https://metacpan.org/pod/Kossy::Validator | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | https://metacpan.org/pod/FormValidator::Tiny | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | https://metacpan.org/pod/FormValidator::LazyWay | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =head1 METHODS | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =head2 C<check> | ||||||||||||||||||||||||||||
| 380 | $validator->check( $data ); | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 SUPPORT AND DOCUMENTATION | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | After installing, you can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | perldoc Validator::Lazy | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | You can also look for information at: | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | RT, CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 391 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | AnnoCPAN, Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 394 | http://annocpan.org/dist/Validator-Lazy | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | CPAN Ratings | ||||||||||||||||||||||||||||
| 397 | http://cpanratings.perl.org/d/Validator-Lazy | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | Search CPAN | ||||||||||||||||||||||||||||
| 400 | http://search.cpan.org/dist/Validator-Lazy/ | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | ANTONC <antonc@cpan.org> | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 410 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 411 | copy of the full license at: | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =cut | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 16 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 418 | 16 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 419 | 16 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 16 | ▶ | use ExtUtils::Installed; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 422 | 16 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 16 | with 'Validator::Lazy::Role::Composer'; # get_field_roles | |||||||||||||||||||||||||||
| 427 | 16 | with 'Validator::Lazy::Role::Check'; | |||||||||||||||||||||||||||
| 428 | 16 | with 'Validator::Lazy::Role::Notifications'; | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # my $validator = Validator::Lazy->new( config => $config ); | ||||||||||||||||||||||||||||
| 431 | # $validator->check( %data ); | ||||||||||||||||||||||||||||
| 432 | 101 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.2
| |||||||||||||||||||||||||||||
| 433 | 101 | my ( $orig, $class, @param ) = @_; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 101 | my %param; | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | # We are too lazy to write word "config" | ||||||||||||||||||||||||||||
| 438 | # But it's ok! :) | ||||||||||||||||||||||||||||
| 439 | 101 | ▶ | if ( scalar @param == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 86 | my $p = $param[0]; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 86 | ▶ | confess 'param should contain something' unless $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 86 | $param{config} = $p; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | else { | ||||||||||||||||||||||||||||
| 448 | 15 | %param = @param; | |||||||||||||||||||||||||||
| 449 | }; | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 101 | my $cfg = $param{config}; | |||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | # Oh! we are more lazy than we not lazy to imagine... | ||||||||||||||||||||||||||||
| 454 | 101 | ▶ | if ( $cfg && !ref $cfg ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cfg and not ref $cfg
MC/DC100%2/2
$cfg and not ref $cfg
$cfg
not ref $cfg
Truth table100%3/3
A and not(B)
A= $cfgB= not ref $cfg
| |||||||||||||||||||||||||||||
| 455 | # File detected! | ||||||||||||||||||||||||||||
| 456 | 5 | ▶ | if ( $cfg !~ /\n/ && $cfg =~ /\.(yaml|yml|json)$/ai ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $cfg =~ /\n/u and $cfg =~ /\.(yaml|yml|json)$/ai
MC/DC0%0/2
not $cfg =~ /\n/u and $cfg =~ /\.(yaml|yml|json)$/ai
not $cfg =~ /\n/u
$cfg =~ /\.(yaml|yml|json)$/ai
Truth table33%1/3
not(A) and B
A= not $cfg =~ /\n/uB= $cfg =~ /\.(yaml|yml|json)$/ai
| |||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | ▶ | confess "File not found: $cfg" unless -f $cfg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 0 | $param{config} = LoadFile( $cfg ); | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | # Try to parse as YAML | ||||||||||||||||||||||||||||
| 463 | else { | ||||||||||||||||||||||||||||
| 464 | 5 | $param{config} = Load( $cfg ); | |||||||||||||||||||||||||||
| 465 | }; | ||||||||||||||||||||||||||||
| 466 | }; | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 101 | return $class->$orig( %param ); | |||||||||||||||||||||||||||
| 469 | 16 | }; | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | has config => ( | ||||||||||||||||||||||||||||
| 472 | is => 'rw', | ||||||||||||||||||||||||||||
| 473 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 474 | required => 1, | ||||||||||||||||||||||||||||
| 475 | 8 | ▶ | default => sub {{}}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 476 | 16 | ); | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | # scalar, that contains current field name which is now under a check | ||||||||||||||||||||||||||||
| 480 | 16 | has current_field => ( | |||||||||||||||||||||||||||
| 481 | is => 'rw', | ||||||||||||||||||||||||||||
| 482 | isa => 'Str', | ||||||||||||||||||||||||||||
| 483 | init_arg => undef, | ||||||||||||||||||||||||||||
| 484 | ); | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | # list of all nested forms above current, exept the very first form, from which all check began | ||||||||||||||||||||||||||||
| 488 | has form_stack => ( | ||||||||||||||||||||||||||||
| 489 | is => 'rw', | ||||||||||||||||||||||||||||
| 490 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 491 | 101 | ▶ | default => sub{ [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 492 | 16 | ); | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | # list of all nested forms above current, exept the very first form, from which all check began | ||||||||||||||||||||||||||||
| 496 | 16 | has data => ( | |||||||||||||||||||||||||||
| 497 | is => 'rw', | ||||||||||||||||||||||||||||
| 498 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 499 | ); | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # full name of current field which is now under checking with all form prefixes attached | ||||||||||||||||||||||||||||
| 502 | # Using for generating field names for errors and notifications, also for uniqueness for all checking process | ||||||||||||||||||||||||||||
| 503 | 393 | ▶ | sub get_full_current_field_name { | ||||||||||||||||||||||||||
|
Subroutine
get_full_current_field_name: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 504 | 393 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 393 | ▶ | my @stack = @{ $self->form_stack // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->form_stack // []
MC/DC0%0/1
$self->form_stack // []
$self->form_stack
Truth table50%1/2
A
A= $self->form_stack
| |||||||||||||||||||||||||||||
| 507 | 393 | shift @stack; | |||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 393 | return join '_', ( @stack, $self->current_field ); | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||