File Coverage

blib/lib/Syccess/Validator.pm
Criterion Covered Total %
statement 4 4 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 6 100.0


line stmt bran cond sub pod time code
1             package Syccess::Validator;
2             BEGIN {
3 9     9   4331 $Syccess::Validator::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Syccess validator
6             $Syccess::Validator::VERSION = '0.103';
7 9     9   47 use Moo::Role;
  9         36  
  9         51  
8              
9             requires qw(
10             validate
11             );
12              
13             has syccess_field => (
14             is => 'ro',
15             required => 1,
16             handles => [qw(
17             syccess
18             )],
19             );
20              
21             has arg => (
22             is => 'ro',
23             predicate => 1,
24             );
25              
26             1;
27              
28             __END__
29              
30             =pod
31              
32             =head1 NAME
33              
34             Syccess::Validator - Syccess validator
35              
36             =head1 VERSION
37              
38             version 0.103
39              
40             =head1 SYNOPSIS
41              
42             package MyValidators::Custom;
43              
44             use Moo;
45              
46             with qw(
47             Syccess::Validator
48             );
49              
50             sub validate {
51             my ( $self, %params ) = @_;
52             my $name = $self->syccess_field->name;
53             # No error if there is no value
54             return if !exists($params{$name});
55             my $value = $params{$name};
56             return if $value eq 'ok';
57             return 'Your value for %s is not ok.';
58             }
59              
60             1;
61              
62             =head1 DESCRIPTION
63              
64             A custom validator requires a B<validate> function, which will be given the
65             complete list of parameters that was given to the B<validate> function on the
66             L<Syccess> object. If there is no error, then the function must return also
67             nothing, as in, an empty list. Anything else given back will be converted into
68             an error message. Most simple is giving a string, that may contain a B<%s>,
69             which will be filled with the label of the field.
70              
71             Normally you don't need this role, most validation requirements will be
72             fulfilled with the B<Syccess::ValidatorSimple>. The case for this role is only
73             given, if you need also access to values of other fields to decide the
74             success.
75              
76             By default, the argument for the validator will be stored in L</arg>, except
77             if its a HashRef, in this case, it will be dereferenced and be used as
78             arguments for the creation of the validator. Which means:
79              
80             Syccess->new( fields => [ myfield => [ length => 4 ] ] );
81              
82             is the same as doing:
83              
84             Syccess->new( fields => [ myfield => [ length => { arg => 4 } ] ] );
85              
86             This way allows to mix complex and straight forward usages. The core validator
87             L<Syccess::Validator::Length> is a very good example for this.
88              
89             =head1 ATTRIBUTES
90              
91             =head2 syccess_field
92              
93             This attribute will be set automatically by Syccess, when it instantiate an
94             object of the validator. There the validator can get the B<name> to find its
95             value in the parameters given on B<validate>.
96              
97             =encoding utf8
98              
99             =head1 SUPPORT
100              
101             IRC
102              
103             Join #sycontent on irc.perl.org. Highlight Getty for fast reaction :).
104              
105             Repository
106              
107             http://github.com/SyContent/Syccess
108             Pull request and additional contributors are welcome
109              
110             Issue Tracker
111              
112             http://github.com/SyContent/Syccess/issues
113              
114             =cut
115              
116             =head1 AUTHOR
117              
118             Torsten Raudssus <torsten@raudss.us>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2014 by Torsten Raudssus.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut