File Coverage

blib/lib/Articulate/Validation.pm
Criterion Covered Total %
statement 18 20 90.0
branch 0 2 0.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 24 28 85.7


line stmt bran cond sub pod time code
1             package Articulate::Validation;
2 4     4   3152 use strict;
  4         7  
  4         135  
3 4     4   15 use warnings;
  4         5  
  4         92  
4              
5 4     4   13 use Moo;
  4         5  
  4         20  
6              
7             with 'Articulate::Role::Component';
8 4     4   1203 use Articulate::Syntax qw(instantiate_array);
  4         7  
  4         32  
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($meta, $content) 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             sub validate {
43 2     2 1 181 my $self = shift;
44 2         8 my $meta = shift;
45 2         4 my $content = shift;
46 2         3 foreach my $validator (@{ $self->validators }) {
  2         32  
47 0         0 my $result = $validator->validate($meta, $content);
48 0 0       0 return $result unless $result;
49             }
50 2         682 return 1;
51             }
52              
53             1;