File Coverage

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


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