line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Workflow::Validator; |
2
|
|
|
|
|
|
|
|
3
|
24
|
|
|
24
|
|
819
|
use warnings; |
|
24
|
|
|
|
|
46
|
|
|
24
|
|
|
|
|
824
|
|
4
|
24
|
|
|
24
|
|
176
|
use strict; |
|
24
|
|
|
|
|
42
|
|
|
24
|
|
|
|
|
717
|
|
5
|
24
|
|
|
24
|
|
160
|
use base qw( Workflow::Base ); |
|
24
|
|
|
|
|
92
|
|
|
24
|
|
|
|
|
3718
|
|
6
|
24
|
|
|
24
|
|
222
|
use Carp qw(croak); |
|
24
|
|
|
|
|
53
|
|
|
24
|
|
|
|
|
6425
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Workflow::Validator::VERSION = '1.62'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my @FIELDS = qw( name class ); |
11
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(@FIELDS); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub init { |
14
|
79
|
|
|
79
|
1
|
205
|
my ( $self, $params ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
79
|
100
|
|
|
|
256
|
$params->{class} = ref $self unless ( $params->{class} ); |
17
|
|
|
|
|
|
|
|
18
|
79
|
100
|
|
|
|
207
|
if ( $params->{name} ) { |
19
|
73
|
|
|
|
|
290
|
$self->name( $params->{name} ); |
20
|
|
|
|
|
|
|
} else { |
21
|
6
|
50
|
|
|
|
31
|
$self->name((ref $self ? ref $self : $self) . " (init in Action)"); |
22
|
|
|
|
|
|
|
} |
23
|
79
|
|
|
|
|
1351
|
$self->class( $params->{class} ); |
24
|
79
|
|
|
|
|
884
|
$self->_init($params); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
60
|
|
|
60
|
|
111
|
sub _init {return} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub validate { |
30
|
1
|
|
|
1
|
1
|
615
|
my ($self) = @_; |
31
|
1
|
|
|
|
|
16
|
croak "Class ", ref($self), " must implement 'validate()'!\n"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
1; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
__END__ |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=pod |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 NAME |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Workflow::Validator - Ensure data are valid |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 VERSION |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This documentation describes version 1.62 of this package |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 SYNOPSIS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# First declare the validator... |
51
|
|
|
|
|
|
|
<validator name="DateValidator" |
52
|
|
|
|
|
|
|
class="MyApp::Validator::Date"> |
53
|
|
|
|
|
|
|
<param name="date_format" value="%Y-%m-%d %h:%m"/> |
54
|
|
|
|
|
|
|
</validator> |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Then associate the validator with runtime data from the context... |
57
|
|
|
|
|
|
|
<action name="MyAction"> |
58
|
|
|
|
|
|
|
<validator name="DateValidator"> |
59
|
|
|
|
|
|
|
<arg>$due_date</arg> |
60
|
|
|
|
|
|
|
</validator> |
61
|
|
|
|
|
|
|
</action> |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# TODO: You can also inintialize and instantiate in one step if you |
64
|
|
|
|
|
|
|
# don't need to centralize or reuse (does this work?) |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
<action name="MyAction"> |
67
|
|
|
|
|
|
|
<validator class="MyApp::Validator::Date"> |
68
|
|
|
|
|
|
|
<param name="date_format" value="%Y-%m-%d %h:%m"/> |
69
|
|
|
|
|
|
|
<arg>$due_date</arg> |
70
|
|
|
|
|
|
|
</validator> |
71
|
|
|
|
|
|
|
</action> |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Then implement the logic |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
package MyApp::Validator::Date; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
use strict; |
78
|
|
|
|
|
|
|
use base qw( Workflow::Validator ); |
79
|
|
|
|
|
|
|
use DateTime::Format::Strptime; |
80
|
|
|
|
|
|
|
use Workflow::Exception qw( configuration_error ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( 'formatter' ); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _init { |
85
|
|
|
|
|
|
|
my ( $self, $params ) = @_; |
86
|
|
|
|
|
|
|
unless ( $params->{date_format} ) { |
87
|
|
|
|
|
|
|
configuration_error |
88
|
|
|
|
|
|
|
"You must define a value for 'date_format' in ", |
89
|
|
|
|
|
|
|
"declaration of validator ", $self->name; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
if ( ref $params->{date_format} ) { |
92
|
|
|
|
|
|
|
configuration_error |
93
|
|
|
|
|
|
|
"The value for 'date_format' must be a simple scalar in ", |
94
|
|
|
|
|
|
|
"declaration of validator ", $self->name; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
my $formatter = DateTime::Format::Strptime->new( |
97
|
|
|
|
|
|
|
pattern => $params->{date_format}, |
98
|
|
|
|
|
|
|
on_error => 'undef' ); |
99
|
|
|
|
|
|
|
$self->formatter( $formatter ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub validate { |
103
|
|
|
|
|
|
|
my ( $self, $wf, $date_string ) = @_; |
104
|
|
|
|
|
|
|
my $fmt = $self->formatter; |
105
|
|
|
|
|
|
|
my $date_object = $fmt->parse_datetime( $date_string ); |
106
|
|
|
|
|
|
|
unless ( $date_object ) { |
107
|
|
|
|
|
|
|
validation_error |
108
|
|
|
|
|
|
|
"Date '$date_string' does not match pattern '", $fmt->pattern, "' ", |
109
|
|
|
|
|
|
|
"due to error '", $fmt->errstr, "'"; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 DESCRIPTION |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Validators specified by 'validator_name' are looked up in the |
116
|
|
|
|
|
|
|
L<Workflow::Factory> which reads a separate configuration and |
117
|
|
|
|
|
|
|
generates validators. (Generally all validators should be declared, |
118
|
|
|
|
|
|
|
but it is not required.) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Validators are objects with a single public method, 'validate()' that |
121
|
|
|
|
|
|
|
take as arguments a workflow object and a list of parameters. The |
122
|
|
|
|
|
|
|
parameters are filled in by the workflow engine according to the |
123
|
|
|
|
|
|
|
instantiation declaration in the Action. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The idea behind a validator is that it validates data but does not |
126
|
|
|
|
|
|
|
care where it comes from. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SUBCLASSING |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 Strategy |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 Methods |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head3 init( \%params ) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Called when the validator is first initialized. If you do not have |
137
|
|
|
|
|
|
|
sufficient information in C<\%params> you should throw an exception. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head3 _init |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
This is a I<dummy> method, please see L</init>. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head3 validate( $workflow, $data ) |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Determine whether your C<$data> is true or false. If necessary you can |
146
|
|
|
|
|
|
|
get the application context information from the C<$workflow> object. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 COPYRIGHT |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Copyright (c) 2003-2023 Chris Winters. All rights reserved. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
153
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Please see the F<LICENSE> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 AUTHORS |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Please see L<Workflow> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |