| 1 | package Dancer2::Plugin::FormValidator::Validator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use Types::Standard qw(InstanceOf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 17 | has config => ( | |||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'], | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 17 | has registry => ( | |||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'], | ||||||||||||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Apply validators to each fields. | ||||||||||||||||||||||||||||
| 24 | # Collect valid and invalid fields. | ||||||||||||||||||||||||||||
| 25 | sub validate { | ||||||||||||||||||||||||||||
| 26 | 19 | ▶ | my ($self, $profile, $input) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 19 | ▶ | if (ref $profile ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | Carp::croak("Profile should be a HashRef\n") | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 19 | my $success = 0; | |||||||||||||||||||||||||||
| 33 | 19 | my %profile = %{ $profile }; | |||||||||||||||||||||||||||
| 34 | 19 | my $is_valid; | |||||||||||||||||||||||||||
| 35 | my @valid; | ||||||||||||||||||||||||||||
| 36 | 19 | my @invalid; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | for my $field (keys %profile) { | |||||||||||||||||||||||||||
| 39 | 41 | $is_valid = 1; | |||||||||||||||||||||||||||
| 40 | 41 | my @validators = @{ $profile{$field} }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 41 | for my $validator_declaration (@validators) { | |||||||||||||||||||||||||||
| 43 | 65 | ▶ | if (my ($name, $params) = $self->_split_validator_declaration($validator_declaration)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 65 | my $validator = $self->registry->get($name); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 65 | ▶ | if (not $validator->validate($field, $input, split(',', $params))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 23 | push @invalid, [ $field, $name, $params ]; | |||||||||||||||||||||||||||
| 48 | 23 | $is_valid = 0; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 65 | ▶ | if (!$is_valid && $validator->stop_on_fail) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $is_valid and $validator->stop_on_fail
MC/DC100%2/2
not $is_valid and $validator->stop_on_fail
not $is_valid
$validator->stop_on_fail
Truth table100%3/3
not(A) and B
A= not $is_validB= $validator->stop_on_fail
| |||||||||||||||||||||||||||||
| 52 | 9 | last; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 41 | ▶ | if ($is_valid == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 18 | push @valid, $field; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 19 | ▶ | if (not @invalid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 5 | $success = 1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 19 | return ($success, \@valid, \@invalid) | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Because validator signatures could be validator:params, we need to split it. | ||||||||||||||||||||||||||||
| 70 | sub _split_validator_declaration { | ||||||||||||||||||||||||||||
| 71 | 65 | ▶ | return ($_[1] =~ /([^:]+):?(.*)/); | ||||||||||||||||||||||||||
|
Subroutine
_split_validator_declaration: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 17 | 1; | |||||||||||||||||||||||||||