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