File Coverage

lib/Workflow/Validator.pm
Criterion Covered Total %
statement 22 22 100.0
branch 5 6 83.3
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 36 37 97.3


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