| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Dancer::Plugin::ValidateTiny; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 21837 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 4 | 1 |  |  | 1 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 573 | use Dancer ':syntax'; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use Dancer::Plugin; | 
| 8 |  |  |  |  |  |  | use Validate::Tiny ':all'; | 
| 9 |  |  |  |  |  |  | use Email::Valid; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | my $settings = plugin_setting; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | register validator => sub | 
| 18 |  |  |  |  |  |  | { | 
| 19 |  |  |  |  |  |  | my ($params, $rules_file) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | my $result = {}; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | # Loading rules from file | 
| 24 |  |  |  |  |  |  | my $rules = _load_rules($rules_file); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Validating | 
| 27 |  |  |  |  |  |  | my $validator = Validate::Tiny->new($params, $rules); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # If you need a full Validate::Tiny object | 
| 30 |  |  |  |  |  |  | if($settings->{is_full} eq 1) | 
| 31 |  |  |  |  |  |  | { | 
| 32 |  |  |  |  |  |  | return $validator; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | if($validator->success) | 
| 36 |  |  |  |  |  |  | { | 
| 37 |  |  |  |  |  |  | # All ok | 
| 38 |  |  |  |  |  |  | $result = { | 
| 39 |  |  |  |  |  |  | result => $validator->data, | 
| 40 |  |  |  |  |  |  | valid => $validator->success | 
| 41 |  |  |  |  |  |  | }; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | else | 
| 44 |  |  |  |  |  |  | { | 
| 45 |  |  |  |  |  |  | # Returning errors | 
| 46 |  |  |  |  |  |  | if(exists $settings->{error_prefix}) | 
| 47 |  |  |  |  |  |  | { | 
| 48 |  |  |  |  |  |  | # With error prefixes from config | 
| 49 |  |  |  |  |  |  | $result = { | 
| 50 |  |  |  |  |  |  | result => _set_error_prefixes($validator->error), | 
| 51 |  |  |  |  |  |  | valid => $validator->success | 
| 52 |  |  |  |  |  |  | }; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | else | 
| 55 |  |  |  |  |  |  | { | 
| 56 |  |  |  |  |  |  | # Without error prefixes | 
| 57 |  |  |  |  |  |  | $result = { | 
| 58 |  |  |  |  |  |  | result => $validator->error, | 
| 59 |  |  |  |  |  |  | valid => $validator->success | 
| 60 |  |  |  |  |  |  | }; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # Combining filtered params and validation results | 
| 65 |  |  |  |  |  |  | %{$result->{result}} = (%{$result->{result}}, %{$validator->data}); | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # Returning validated data | 
| 68 |  |  |  |  |  |  | return $result; | 
| 69 |  |  |  |  |  |  | }; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _set_error_prefixes | 
| 72 |  |  |  |  |  |  | { | 
| 73 |  |  |  |  |  |  | my $errors = shift; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | foreach my $error (keys %{$errors}) | 
| 76 |  |  |  |  |  |  | { | 
| 77 |  |  |  |  |  |  | # Replacing keys with prefix. O_o | 
| 78 |  |  |  |  |  |  | $errors->{$settings->{error_prefix} . $error} = delete $errors->{$error}; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | return $errors; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub _load_rules | 
| 85 |  |  |  |  |  |  | { | 
| 86 |  |  |  |  |  |  | my $rules_file = shift; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | # Checking plugin settings and rules file for existing | 
| 89 |  |  |  |  |  |  | die "Rules directory not specified in plugin settings!" if !$settings->{rules_dir}; | 
| 90 |  |  |  |  |  |  | die "Rules file not specified!" if !$rules_file; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # Making full path to rules file | 
| 93 |  |  |  |  |  |  | $rules_file = setting('appdir') . '/' . $settings->{rules_dir} . "/" . $rules_file; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # Putting rules from file to $rules | 
| 96 |  |  |  |  |  |  | my $rules = do $rules_file || die $! . "\n" . $@; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | return $rules; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub check_email | 
| 103 |  |  |  |  |  |  | { | 
| 104 |  |  |  |  |  |  | my ($email, $message) = @_; | 
| 105 |  |  |  |  |  |  | Email::Valid->address($email) ? undef : $message; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | register_plugin; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | 1; | 
| 113 |  |  |  |  |  |  | __END__ |