File Coverage

blib/lib/Poz/Types/null.pm
Criterion Covered Total %
statement 21 21 100.0
branch 3 4 75.0
condition 3 6 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 35 39 89.7


line stmt bran cond sub pod time code
1             package Poz::Types::null;
2 11     11   165 use 5.032;
  11         48  
3 11     11   69 use strict;
  11         24  
  11         274  
4 11     11   45 use warnings;
  11         13  
  11         580  
5 11     11   3980 use parent 'Poz::Types::scalar';
  11         3039  
  11         61  
6              
7             sub new {
8 2     2 1 7 my ($class, $opts) = @_;
9 2   50     7 $opts = $opts || {};
10 2   50     12 $opts->{required_error} //= "Not a null";
11 2   50     11 $opts->{invalid_type_error} //= "Not a null";
12 2         15 my $self = $class->SUPER::new($opts);
13 2         12 return $self;
14             }
15              
16             sub rule {
17 3     3 1 6 my ($self, $value) = @_;
18 3 100       11 return $self->{required_error} if defined $value;
19 1 50       5 return $self->{invalid_type_error} if $value ;
20 1         4 return;
21             };
22              
23             1;
24              
25             =head1 NAME
26              
27             Poz::Types::null - Null type validation for Poz::Types
28              
29             =head1 SYNOPSIS
30              
31             use Poz qw/z/;
32              
33             my $null_validator = z->null;
34              
35             $null_validator->parse(undef); # returns undef
36             $null_validator->parse(1); # returns error message
37              
38             my $array_with_null_or_number = z->array(z->union(z->null, z->number));
39             $array_with_null_or_number->parse([undef, 1]); # returns [undef, 1]
40             $array_with_null_or_number->parse([1, 2]); # returns [1, 2]
41             $array_with_null_or_number->parse([undef, "a"]); # returns error message
42              
43             =head1 DESCRIPTION
44              
45             This module provides a null type validator for Poz::Types. It can be used to validate that a value is C<undef>.
46              
47             =head1 METHODS
48              
49             =head2 rule
50              
51             my $error = $null_validator->rule($value);
52              
53             Validates the given value. Returns an error message if the value is not C<undef>, otherwise returns C<undef>.
54              
55             =head1 LICENSE
56              
57             Copyright (C) ytnobody.
58              
59             This library is free software; you can redistribute it and/or modify
60             it under the same terms as Perl itself.
61              
62             =head1 AUTHOR
63              
64             ytnobody E<lt>ytnobody@gmail.comE<gt>
65              
66             =cut