1Workflow::Validator::InUEsneurmeCroantterdiTbyuptee(d3W)PoerrklflDoowc:u:mVeanltiadtaitoonr::InEnumeratedType(3)
2
3
4
6 Workflow::Validator::InEnumeratedType - Ensure a value is one of a
7 declared set of values
8
10 This documentation describes version 1.59 of this package
11
13 # Inline the enumeration...
14
15 <action name="PlayGame">
16 <validator name="InEnumeratedType">
17 <value>Rock</value>
18 <value>Scissors</value>
19 <value>Paper</value>
20 <arg value="$play"/>
21 </validator>
22 </action>
23
24 # Or declare it in the validator to be more readable...
25 <validator name="RSP"
26 class="Validator::InEnumeratedType">
27 <value>Rock</value>
28 <value>Scissors</value>
29 <value>Paper</value>
30 </validator>
31
32 # ...and use it in your action
33 <action name="PlayGame">
34 <validator name="RSP">
35 <arg value="$play"/>
36 </validator>
37 </action>
38
40 This validator ensures that a value matches one of a set of values. You
41 declare the values in the set (or enumerated type) in either the main
42 validator declaration or in the declaration inside the action, then
43 pass a single argument of the value in the context you would like to
44 check.
45
46 Declaring the members of the enumerated type in the validator
47 configuration makes for more readable (and brief) action
48 configurations, as well as making the types more reusable, but it is
49 really up to you.
50
52 Strategy
53 Unlike some other validator classes this one is setup to be
54 subclassable. It is usable as-is, of course, but many times you will
55 find that you have need of more interesting types in your enumeration
56 than simple strings. So this class provides the hooks for you to simply
57 create your own.
58
59 For instance, in a trouble ticket system you may have the idea that
60 tickets can only be assigned to particular users. Maybe they are in a
61 'worker' role, maybe they are some administrators, whatever. By
62 creating a class to have these users as an enumerated type, combined
63 with declaring the required Action fields, you make for a pretty
64 powerful piece of reflection.
65
66 Onto the code. First we declare a field type of 'worker':
67
68 <field type="worker"
69 class="MyApp::Field::Worker"/>
70
71 Next a validator of this enumerated type:
72
73 <validator name="IsWorker"
74 class="MyApp::Validator::WorkerEnumeration"/>
75
76 We then associate this field type with a field in the action and the
77 validator to ensure the user selects a worker from the right pool:
78
79 <action name="AssignTicket">
80 <field name="assignee"
81 type="worker"
82 is_required="yes"/>
83 ...
84 <validator name="IsWorker">
85 <arg value="$assignee"/>
86 </validator>
87
88 Note that the name of the field and the name used in the validator are
89 the same. This allows external applications to query the action for its
90 fields, get 'assignee' as the name and get a list of User objects (or
91 something similar) as the types from which to choose a value, and
92 checks that same field to ensure a correct choice was submitted.
93
94 The implementation for the validator might look like:
95
96 package MyApp::Validator::WorkerEnumeration;
97
98 sub validate {
99 my ( $self, $wf, $worker_id ) = @_;
100 my $ticket = $context->param( 'ticket' );
101 unless ( $ticket ) {
102 my $ticket_id = $context->param( 'ticket_id' );
103 $ticket = Ticket->fetch( $ticket_id );
104 }
105 my $workers = $ticket->fetch_available_workers();
106 my @worker_id = map { $_->id } @{ $workers };
107 $self->add_enumerated_values( @worker_id );
108 $self->SUPER::validate( $wf, $worker_id );
109 }
110
111 METHODS
112 _init( \%params )
113
114 This method initializes the class and the enumerated class.
115
116 It uses "add_enumerated_values" to add the set of values for
117 enumeration.
118
119 The primary parameter is value, which should be used to specify the
120 either a single value or a reference to array of values to be added.
121
122 validate
123
124 The validate method is the public API. It encapulates
125 "is_enumerated:value" and works with Workflow.
126
127 add_enumerated_values( @values )
128
129 This method ads an array of values to be regarded as enumerations for
130 the validator.
131
132 get_enumerated_values()
133
134 This method returns the defined enumerated values for the class as an
135 array.
136
137 is_enumerated_value( $value )
138
139 This is most often the single method you will want to modify.
140
141 The method offers assertion of a given value, as to whether it is an
142 enumerated type as defined in the class.
143
145 • Validator 'InEnumeratedType' must be initialized with the values
146 you wish to validate against using the parameter 'value'.
147
148 This Workflow::Exception is thrown from "_init" if the 'value'
149 parameter is not set.
150
151 • Value '$value' must be one of: <@values>
152
153 This Workflow::Exception is thrown from "_validator" if the value
154 to be asserted is not mathing any of the enumerated values defined
155 as part of the set.
156
158 Copyright (c) 2003-2022 Chris Winters. All rights reserved.
159
160 This library is free software; you can redistribute it and/or modify it
161 under the same terms as Perl itself.
162
163 Please see the LICENSE
164
166 Please see Workflow
167
168
169
170perl v5.34.0 2022-W0o2r-k0f6low::Validator::InEnumeratedType(3)