| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Poz::Types; |
|
2
|
11
|
|
|
11
|
|
4967
|
use strict; |
|
|
11
|
|
|
|
|
24
|
|
|
|
11
|
|
|
|
|
406
|
|
|
3
|
11
|
|
|
11
|
|
52
|
use warnings; |
|
|
11
|
|
|
|
|
36
|
|
|
|
11
|
|
|
|
|
524
|
|
|
4
|
11
|
|
|
11
|
|
57
|
use Carp (); |
|
|
11
|
|
|
|
|
13
|
|
|
|
11
|
|
|
|
|
4991
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
|
7
|
138
|
|
|
138
|
1
|
296
|
my ($class, $opts) = @_; |
|
8
|
138
|
|
50
|
|
|
328
|
$opts = $opts || {}; |
|
9
|
138
|
|
|
|
|
250
|
my $rulecode = $class . "::rule"; |
|
10
|
|
|
|
|
|
|
my $self = bless { |
|
11
|
138
|
|
|
|
|
194
|
%{$opts}, |
|
|
138
|
|
|
|
|
1094
|
|
|
12
|
|
|
|
|
|
|
rules => [\&$rulecode], |
|
13
|
|
|
|
|
|
|
transform => [], |
|
14
|
|
|
|
|
|
|
caller => [ caller() ], |
|
15
|
|
|
|
|
|
|
}, $class; |
|
16
|
138
|
|
50
|
|
|
3420
|
$self->{required_error} //= 'required'; |
|
17
|
138
|
|
50
|
|
|
296
|
$self->{invalid_type_error} //= 'invalid type'; |
|
18
|
138
|
|
|
|
|
351
|
return $self; |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub rule { |
|
22
|
0
|
|
|
0
|
1
|
0
|
Carp::croak("Not implemented"); |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub parse { |
|
26
|
406
|
|
|
406
|
1
|
7349
|
my $self = shift; |
|
27
|
406
|
|
|
|
|
1110
|
my ($ret, $err) = $self->safe_parse(@_); |
|
28
|
406
|
100
|
|
|
|
2369
|
Carp::croak $err if defined $err; |
|
29
|
267
|
|
|
|
|
1075
|
$ret; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub safe_parse { |
|
33
|
450
|
100
|
|
450
|
1
|
4642
|
Carp::croak "Must handle error" unless wantarray; |
|
34
|
|
|
|
|
|
|
|
|
35
|
447
|
|
|
|
|
889
|
my ($self, $value) = @_; |
|
36
|
447
|
50
|
|
|
|
1599
|
if (length($self->{transform}) > 0) { |
|
37
|
447
|
|
|
|
|
697
|
for my $transformer (@{$self->{transform}}) { |
|
|
447
|
|
|
|
|
1094
|
|
|
38
|
49
|
|
|
|
|
123
|
$value = $transformer->($self, $value); |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
} |
|
41
|
447
|
|
|
|
|
709
|
for my $rule (@{$self->{rules}}) { |
|
|
447
|
|
|
|
|
782
|
|
|
42
|
713
|
|
|
|
|
2033
|
my $err = $rule->($self, $value); |
|
43
|
713
|
100
|
100
|
|
|
50134
|
if ( (ref($err)||'') eq 'Poz::Result::ShortCircuit') { |
|
44
|
5
|
|
|
|
|
53
|
return; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
708
|
100
|
|
|
|
1931
|
return (undef, $err) if defined $err; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
281
|
100
|
|
|
|
656
|
if ($self->{need_coerce}) { |
|
49
|
9
|
|
|
|
|
28
|
$value = $self->coerce($value); |
|
50
|
|
|
|
|
|
|
} |
|
51
|
281
|
|
|
|
|
740
|
return ($value, undef); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 NAME |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Poz::Types - A module for handling type validation and transformation |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
use Poz::Types; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $type = Poz::Types->new({ |
|
65
|
|
|
|
|
|
|
required_error => 'This field is required', |
|
66
|
|
|
|
|
|
|
invalid_type_error => 'Invalid type provided', |
|
67
|
|
|
|
|
|
|
}); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $result = $type->parse($value); |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Poz::Types is a module designed to handle type validation and transformation. |
|
74
|
|
|
|
|
|
|
It provides a flexible way to define rules and transformations for different types. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 METHODS |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 new |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $type = Poz::Types->new(\%options); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Creates a new Poz::Types object. The optional hash reference can contain the following keys: |
|
83
|
|
|
|
|
|
|
- required_error: Custom error message for required fields. |
|
84
|
|
|
|
|
|
|
- invalid_type_error: Custom error message for invalid types. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 rule |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$type->rule(); |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This method should be overridden in subclasses to provide specific validation rules. |
|
91
|
|
|
|
|
|
|
By default, it throws a "Not implemented" error. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 parse |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $result = $type->parse($value); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Parses the given value according to the defined rules and transformations. |
|
98
|
|
|
|
|
|
|
Returns the transformed value or an error if validation fails. |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 safe_parse |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my ($result, $error) = $type->safe_parse($value); |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Parses the given value and returns the transformed value. |
|
105
|
|
|
|
|
|
|
If succeeds, returns a tuple of the transformed value and undef. |
|
106
|
|
|
|
|
|
|
If fails, returns a tuple of undef and an error message. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 LICENSE |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Copyright (C) ytnobody. |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
113
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 AUTHOR |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
ytnobody E<lt>ytnobody@gmail.comE<gt> |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |