Valiant/Validator/Scalar.pm

filter: Statementstmt 100% 15 / 15 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 88.4% 23 / 26 CC 3 SCAR 4.6
CC
3
Cov
95%
normalize_shortcut
2.1
validate_each
2.0
BEGIN
1.0
CRAP 1.6
« Valiant/Proxy/Hash.pm ↑ Summary Valiant/Filter/Lower.pm »
1 package Valiant::Validator::Scalar;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 with 'Valiant::Validator::Each';
7  
8 1 ▶has is_not_scalar => (is=>'ro', required=>1, default=>sub {_t 'is_not_a_scalar'});
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 1 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=2 SCAR=7.2
Pod undocumented
11 1 my ($class, $arg) = @_;
12 1 ▶ return +{} if $arg eq '1' ;
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq 1 1 0 1
13 }
14  
15 2 ▶sub validate_each {
Subroutine validate_each: called CC=2 SCAR=6.9
Pod undocumented
16 2 my ($self, $record, $attribute, $value, $opts) = @_;
17 2 ▶ return if ref( \$value ) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref \$value eq "SCALAR" 1 1 2
18  
19 1 $record->errors->add($attribute, $self->is_not_scalar, $opts)
20 }
21  
22 1 1;
23  
24 =head1 NAME
25  
26 Valiant::Validator::Scalar - Validate that a value is a scalar (like a string or number)
27  
28 =head1 SYNOPSIS
29  
30 package Local::Test::Scalar;
31  
32 use Moo;
33 use Valiant::Validations;
34  
35 has name => (is=>'ro');
36  
37 validates name => ( scalar => 1 );
38  
39 ok my $object = Local::Test::Scalar->new(name=>[111,'John']);
40 ok $object->validate->invalid;
41  
42 is_deeply +{ $object->errors->to_hash(full_messages=>1) },
43 {
44 'name' => [
45 'Name must be a string or number',
46 ]
47 };
48  
49 =head1 DESCRIPTION
50  
51 Validates that the value in question is a scalar.
52  
53 =head1 ATTRIBUTES
54  
55 This validator supports the following attributes:
56  
57 =head2 is_not_scalar
58  
59 The error message used when the value is not a scalar. Default is translation
60 tag 'is_not_a_scalar'.
61  
62 =head1 SHORTCUT FORM
63  
64 This validator supports the follow shortcut forms:
65  
66 validates attribute => ( scalar => 1, ... );
67  
68 Which is the same as:
69  
70 validates attribute => (
71 scalar => { },
72 );
73
74 =head1 GLOBAL PARAMETERS
75  
76 This validator supports all the standard shared parameters: C<if>, C<unless>,
77 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
78  
79 =head1 AUTHOR
80
81 John Napiorkowski L<email:jjnapiork@cpan.org>
82
83 =head1 SEE ALSO
84
85 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
86
87 =head1 COPYRIGHT & LICENSE
88
89 Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org>
90
91 This library is free software; you can redistribute it and/or modify it under
92 the same terms as Perl itself.
93  
94 =cut
« Valiant/Proxy/Hash.pm ↑ Summary Valiant/Filter/Lower.pm »