File Coverage

blib/lib/Articulate/Validation.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Articulate::Validation;
2 6     6   4392 use strict;
  6         11  
  6         204  
3 6     6   27 use warnings;
  6         8  
  6         134  
4              
5 6     6   22 use Moo;
  6         9  
  6         31  
6              
7             with 'Articulate::Role::Component';
8 6     6   1639 use Articulate::Syntax qw(instantiate_array);
  6         11  
  6         46  
9              
10             =head1 NAME
11              
12             Articulate::Validation - ensure content is valid before accepting it.
13              
14             =head1 DESCRIPTION
15              
16             use Articulate::Validation;
17             $validation->validate($item) or throw_error;
18              
19             Validators should return a true argument if either a) The item is valid, or b) The validator has no opinion on the item.
20              
21             =head1 METHODS
22              
23             =head3 validate
24              
25             Iterates through the validators. Returns false if any has a false result. Returns true otherwise.
26              
27             =cut
28              
29             =head1 ATTTRIBUTES
30              
31             =head3 validators
32              
33             An arrayref of the classes which provide a validate function, in the order in which they will be asked to validate items.
34              
35             =cut
36              
37             has validators => (
38             is => 'rw',
39             default => sub { [] },
40             coerce => sub { instantiate_array @_ }
41             );
42              
43             sub validate {
44 5     5 1 184 my $self = shift;
45 5         9 my $item = shift;
46 5         9 foreach my $validator ( @{ $self->validators } ) {
  5         66  
47 2         16 my $result = $validator->validate($item);
48 2 100       9 return $result unless $result;
49             }
50 4         1207 return 1;
51             }
52              
53             1;