File Coverage

blib/lib/Workflow/Validator/HasRequiredField.pm
Criterion Covered Total %
statement 20 22 90.9
branch 2 4 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 29 33 87.8


line stmt bran cond sub pod time code
1             package Workflow::Validator::HasRequiredField;
2              
3 30     30   1436105 use warnings;
  30         67  
  30         2124  
4 30     30   198 use strict;
  30         70  
  30         864  
5 30     30   345 use v5.14.0;
  30         108  
6 30     30   182 use parent qw( Workflow::Validator );
  30         63  
  30         213  
7 30     30   2910 use Workflow::Exception qw( validation_error );
  30         106  
  30         7923  
8              
9             $Workflow::Validator::HasRequiredField::VERSION = '2.09';
10              
11             sub validate {
12 36     36 1 138 my ( $self, $wf, @required_fields ) = @_;
13 36         174 my $context = $wf->context;
14 36         90 my @no_value = ();
15 36         100 foreach my $field (@required_fields) {
16 36 50       93 unless ( defined $context->param($field) ) {
17 0         0 push @no_value, $field;
18             }
19             }
20 36 50       251 if ( scalar @no_value ) {
21 0           validation_error "The following fields require a value: ",
22             join(', ',@no_value), { invalid_fields => \@no_value };
23             }
24             }
25              
26             1;
27              
28             __END__
29              
30             =pod
31              
32             =head1 NAME
33              
34             Workflow::Validator::HasRequiredField - Validator to ensure certain data are in the context
35              
36             =head1 VERSION
37              
38             This documentation describes version 2.09 of this package
39              
40             =head1 SYNOPSIS
41              
42             # Validator is created automatically when you mark a field as
43             # 'is_required: yes' in the action, such as:
44              
45             action:
46             - name: CreateUser
47             field:
48             - name: username
49             is_required: yes
50             source_class: App::Field::ValidUsers
51             ...
52              
53             =head1 DESCRIPTION
54              
55             This is a simple validator to ensure that each of the fields you have
56             marked with the 'is_required' property as 'yes' are indeed present
57             before the associated action is executed.
58              
59             for instance, given the configuration:
60              
61             action:
62             - name: CreateUser
63             field:
64             - name: username
65             is_required: yes
66             - name: email
67             is_required: yes
68             - name: office
69              
70             An action executed with such a context:
71              
72             my $wf = FACTORY->get_workflow( $id );
73             $wf->context( username => 'foo' );
74             $wf->context( office => 'Ottumwa' );
75             $wf->execute_action( 'CreateUser' );
76              
77             Would fail with a message:
78              
79             The following fields require a value: email
80              
81             You normally do not need to configure this validator yourself. It gets
82             generated automatically when the Action configration is read
83             in based on the 'is_required: yes' attribute. However, if you do need
84             to create it yourself:
85              
86             action:
87             - name: Foo
88             validator:
89             - name: HasRequiredField
90             arg:
91             - fieldOne
92             - field_two
93              
94             Note that we do not try to match the value in the context against a
95             set of known values or algorithm, just see if the value is defined --
96             using the Perl notion for defined rather than true/false, which means
97             '0' and the empty string will both be valid.
98              
99             =head2 METHODS
100              
101             =head3 validate
102              
103             Validates whether a given set of required fields are defined.
104              
105             Takes two parameters: a workflow object and an array of names of fields.
106              
107             The provided fields are matched against the workflow in question and
108             L<Workflow::Exception>'s are thrown in case of missing fields.
109              
110             =head1 COPYRIGHT
111              
112             Copyright (c) 2003-2021 Chris Winters. All rights reserved.
113              
114             This library is free software; you can redistribute it and/or modify
115             it under the same terms as Perl itself.
116              
117             Please see the F<LICENSE>
118              
119             =head1 AUTHORS
120              
121             Please see L<Workflow>
122              
123             =cut