File Coverage

blib/lib/Syccess/Validator/Regex.pm
Criterion Covered Total %
statement 9 9 100.0
branch 4 4 100.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 16 17 94.1


line stmt bran cond sub pod time code
1             package Syccess::Validator::Regex;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: A validator to check with a regex
4             $Syccess::Validator::Regex::VERSION = '0.104';
5 2     2   2049 use Moo;
  2         2  
  2         8  
6              
7             with qw(
8             Syccess::ValidatorSimple
9             );
10              
11             has message => (
12             is => 'lazy',
13             );
14              
15             sub _build_message {
16 1     1   7 return 'Your value for %s is not valid.';
17             }
18              
19             sub validator {
20 4     4 0 4 my ( $self, $value ) = @_;
21 4         5 my $regex = $self->arg;
22 4 100       29 my $r = ref $regex eq 'Regexp' ? $regex : qr{$regex};
23 4 100       44 return $self->message unless $value =~ m/$r/;
24 2         8 return;
25             }
26              
27             1;
28              
29             __END__
30              
31             =pod
32              
33             =head1 NAME
34              
35             Syccess::Validator::Regex - A validator to check with a regex
36              
37             =head1 VERSION
38              
39             version 0.104
40              
41             =head1 SYNOPSIS
42              
43             Syccess->new(
44             fields => [
45             foo => [ regex => qr/^\w+$/ ],
46             bar => [ regex => {
47             arg => '^[a-z]+$', # will be converted to regexp
48             message => 'We only allow lowercase letters on this field.',
49             } ],
50             ],
51             );
52              
53             =head1 DESCRIPTION
54              
55             This validator allows checking against a regular expression. The regular
56             expression can be given as Regex or plain scalar, which will be converted
57             to a Regex.
58              
59             =head1 ATTRIBUTES
60              
61             =head2 message
62              
63             This contains the error message or the format for the error message
64             generation. See L<Syccess::Error/validator_message>.
65              
66             =encoding utf8
67              
68             =head1 SUPPORT
69              
70             IRC
71              
72             Join irc.perl.org and msg Getty
73              
74             Repository
75              
76             http://github.com/Getty/p5-syccess
77             Pull request and additional contributors are welcome
78              
79             Issue Tracker
80              
81             http://github.com/Getty/p5-syccess/issues
82              
83             =head1 AUTHOR
84              
85             Torsten Raudssus <torsten@raudss.us>
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2017 by Torsten Raudssus.
90              
91             This is free software; you can redistribute it and/or modify it under
92             the same terms as the Perl 5 programming language system itself.
93              
94             =cut