@@ -71,7 +71,8 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
71
71
72
72
/**
73
73
* Constructor
74
- * @sharedConstraints.hint A structure of shared constraints
74
+ *
75
+ * @sharedConstraints A structure of shared constraints
75
76
*/
76
77
ValidationManager function init ( struct sharedConstraints = structNew () ){
77
78
// valid validator registrations
@@ -83,26 +84,34 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
83
84
}
84
85
85
86
/**
86
- * Validate an object
87
- * @target.hint The target object to validate or a structure like a form or collection. If it is a collection, we will build a generic object for you so we can validate the structure of name-value pairs.
88
- * @fields.hint One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
89
- * @constraints.hint An optional shared constraints name or an actual structure of constraints to validate on.
90
- * @locale.hint An optional locale to use for i18n messages
91
- * @excludeFields.hint An optional list of fields to exclude from the validation.
92
- * @IncludeFields.hint An optional list of fields to include in the validation.
87
+ * Validate an object using constraints
88
+ *
89
+ * @target The target object to validate or a structure like a form or collection. If it is a collection, we will build a generic object for you so we can validate the structure of name-value pairs.
90
+ * @fields One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
91
+ * @constraints An optional shared constraints name or an actual structure of constraints to validate on.
92
+ * @locale An optional locale to use for i18n messages
93
+ * @excludeFields An optional list of fields to exclude from the validation.
94
+ * @IncludeFields An optional list of fields to include in the validation.
93
95
*/
94
- IValidationResult function validate ( required any target , string fields = " *" , any constraints = " " , string locale = " " , string excludeFields = " " , string includeFields = " " ){
96
+ IValidationResult function validate (
97
+ required any target ,
98
+ string fields = " *" ,
99
+ any constraints = " " ,
100
+ string locale = " " ,
101
+ string excludeFields = " " ,
102
+ string includeFields = " "
103
+ ){
95
104
var targetName = " " ;
96
105
97
106
// Do we have a real object or a structure?
98
107
if ( ! isObject ( arguments .target ) ){
99
108
arguments .target = new GenericObject ( arguments .target );
100
- if ( isSimpleValue ( arguments .constraints ) and len ( arguments .constraints ) )
109
+ if ( isSimpleValue ( arguments .constraints ) and len ( arguments .constraints ) ){
101
110
targetName = arguments .constraints ;
102
- else
111
+ } else {
103
112
targetName = " GenericForm" ;
104
- }
105
- else {
113
+ }
114
+ } else {
106
115
targetName = listLast ( getMetadata ( arguments .target ).name , " ." );
107
116
}
108
117
@@ -117,10 +126,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
117
126
constraints = allConstraints
118
127
};
119
128
var results = wirebox .getInstance ( name = " cbvalidation.models.result.ValidationResult" , initArguments = initArgs );
129
+
120
130
// iterate over constraints defined
121
- var this Field = " " ;
122
- for ( this Field in allConstraints ){
123
-
131
+ for ( var this Field in allConstraints ){
124
132
var validateField = true ;
125
133
if ( len ( arguments .includeFields ) AND NOT listFindNoCase ( arguments .includeFields , this Field ) ){
126
134
validateField = false ;
@@ -144,8 +152,18 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
144
152
145
153
/**
146
154
* Process validation rules on a target object and field
155
+ *
156
+ * @results The validation result object
157
+ * @rules The structure containing validation rules
158
+ * @target The target object to do validation on
159
+ * @field The field to validate
147
160
*/
148
- ValidationManager function processRules (required cbvalidation .models .result .IValidationResult results , required struct rules , required any target , required any field ){
161
+ ValidationManager function processRules (
162
+ required cbvalidation .models .result .IValidationResult results ,
163
+ required struct rules ,
164
+ required any target ,
165
+ required any field
166
+ ){
149
167
// process the incoming rules
150
168
for ( var key in arguments .rules ){
151
169
// if message validators, just ignore
@@ -158,7 +176,7 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
158
176
target = arguments .target ,
159
177
field = arguments .field ,
160
178
targetValue = evaluate ( " arguments.target.get#arguments .field #()" ),
161
- validationData = arguments .rules [key ]
179
+ validationData = arguments .rules [ key ]
162
180
);
163
181
164
182
}
@@ -167,44 +185,61 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
167
185
168
186
/**
169
187
* Create validators according to types and validation data
188
+ *
189
+ * @validatorType The type of validator to retrieve, either internal or class path or wirebox ID
190
+ * @validationData The validation data that is used for custom validators
191
+ *
192
+ * @throws ValidationManager.InvalidValidatorType
170
193
*/
171
- cbvalidation.models.validators.IValidator function getValidator (required string validatorType , required any validationData ){
194
+ cbvalidation.models.validators.IValidator function getValidator (
195
+ required string validatorType ,
196
+ required any validationData
197
+ ){
172
198
switch ( arguments .validatorType ){
173
- case " required" : { return wirebox .getInstance (" cbvalidation.models.validators.RequiredValidator" ); }
174
- case " type" : { return wirebox .getInstance (" cbvalidation.models.validators.TypeValidator" ); }
175
- case " size" : { return wirebox .getInstance (" cbvalidation.models.validators.SizeValidator" ); }
176
- case " range" : { return wirebox .getInstance (" cbvalidation.models.validators.RangeValidator" ); }
177
- case " regex" : { return wirebox .getInstance (" cbvalidation.models.validators.RegexValidator" ); }
178
- case " sameAs" : { return wirebox .getInstance (" cbvalidation.models.validators.SameAsValidator" ); }
179
- case " sameAsNoCase" : { return wirebox .getInstance (" cbvalidation.models.validators.SameAsNoCaseValidator" ); }
180
- case " inList" : { return wirebox .getInstance (" cbvalidation.models.validators.InListValidator" ); }
181
- case " discrete" : { return wirebox .getInstance (" cbvalidation.models.validators.DiscreteValidator" ); }
182
- case " min" : { return wirebox .getInstance (" cbvalidation.models.validators.MinValidator" ); }
183
- case " max" : { return wirebox .getInstance (" cbvalidation.models.validators.MaxValidator" ); }
184
- case " udf" : { return wirebox .getInstance (" cbvalidation.models.validators.UDFValidator" ); }
185
- case " method" : { return wirebox .getInstance (" cbvalidation.models.validators.MethodValidator" ); }
199
+ case " required" : { return wirebox .getInstance ( " cbvalidation.models.validators.RequiredValidator" ); }
200
+ case " type" : { return wirebox .getInstance ( " cbvalidation.models.validators.TypeValidator" ); }
201
+ case " size" : { return wirebox .getInstance ( " cbvalidation.models.validators.SizeValidator" ); }
202
+ case " range" : { return wirebox .getInstance ( " cbvalidation.models.validators.RangeValidator" ); }
203
+ case " regex" : { return wirebox .getInstance ( " cbvalidation.models.validators.RegexValidator" ); }
204
+ case " sameAs" : { return wirebox .getInstance ( " cbvalidation.models.validators.SameAsValidator" ); }
205
+ case " sameAsNoCase" : { return wirebox .getInstance ( " cbvalidation.models.validators.SameAsNoCaseValidator" ); }
206
+ case " inList" : { return wirebox .getInstance ( " cbvalidation.models.validators.InListValidator" ); }
207
+ case " discrete" : { return wirebox .getInstance ( " cbvalidation.models.validators.DiscreteValidator" ); }
208
+ case " min" : { return wirebox .getInstance ( " cbvalidation.models.validators.MinValidator" ); }
209
+ case " max" : { return wirebox .getInstance ( " cbvalidation.models.validators.MaxValidator" ); }
210
+ case " udf" : { return wirebox .getInstance ( " cbvalidation.models.validators.UDFValidator" ); }
211
+ case " method" : { return wirebox .getInstance ( " cbvalidation.models.validators.MethodValidator" ); }
186
212
case " validator" : {
187
- if ( find (" :" , arguments .validationData ) ){ return wirebox .getInstance ( getToken ( arguments .validationData , 2 , " :" ) ); }
213
+ if ( find ( " :" , arguments .validationData ) ){
214
+ return wirebox .getInstance ( getToken ( arguments .validationData , 2 , " :" ) );
215
+ }
188
216
return wirebox .getInstance ( arguments .validationData );
189
217
}
190
218
default : {
191
- if ( wirebox .getBinder ().mappingExists ( validatorType ) ) { return wirebox .getInstance ( validatorType ); }
192
- throw (message = " The validator you requested #arguments .validatorType # is not a valid validator" ,type = " ValidationManager.InvalidValidatorType" );
219
+ if ( wirebox .getBinder ().mappingExists ( validatorType ) ) {
220
+ return wirebox .getInstance ( validatorType );
221
+ }
222
+ throw (
223
+ message = " The validator you requested #arguments .validatorType # is not a valid validator" ,
224
+ type = " ValidationManager.InvalidValidatorType"
225
+ );
193
226
}
194
227
}
195
228
}
196
229
197
230
/**
198
231
* Retrieve the shared constraints, all of them or by name
199
- * @name.hint Filter by name or not
232
+ *
233
+ * @name Filter by name or not
200
234
*/
201
235
struct function getSharedConstraints ( string name ){
202
236
return ( structKeyExists ( arguments , " name" ) ? variables .sharedConstraints [ arguments .name ] : variables .sharedConstraints );
203
237
}
204
238
205
239
/**
206
240
* Check if a shared constraint exists by name
207
- * @name.hint The shared constraint to check
241
+ *
242
+ * @name The shared constraint to check
208
243
*/
209
244
boolean function sharedConstraintsExists ( required string name ){
210
245
return structKeyExists ( variables .sharedConstraints , arguments .name );
@@ -213,7 +248,8 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
213
248
214
249
/**
215
250
* Set the entire shared constraints structure
216
- * @constraints.hint Filter by name or not
251
+ *
252
+ * @constraints Filter by name or not
217
253
*/
218
254
IValidationManager function setSharedConstraints ( struct constraints ){
219
255
variables .sharedConstraints = arguments .constraints ;
@@ -222,8 +258,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
222
258
223
259
/**
224
260
* Store a shared constraint
225
- * @name.hint Filter by name or not
226
- * @constraint.hint The constraint to store.
261
+ *
262
+ * @name Filter by name or not
263
+ * @constraint The constraint to store.
227
264
*/
228
265
IValidationManager function addSharedConstraint ( required string name , required struct constraint ){
229
266
variables .sharedConstraints [ arguments .name ] = arguments .constraints ;
@@ -233,19 +270,26 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
233
270
234
271
/**
235
272
* Determine from where to take the constraints from
273
+ *
274
+ * @target The target object
275
+ * @constraints The constraints rules
276
+ *
277
+ * @throws ValidationManager.InvalidSharedConstraint
236
278
*/
237
- private struct function determineConstraintsDefinition (required any target , any constraints = " " ){
279
+ private struct function determineConstraintsDefinition ( required any target , any constraints = " " ){
238
280
var this Constraints = {};
239
281
240
282
// if structure, just return it back
241
283
if ( isStruct ( arguments .constraints ) ){ return arguments .constraints ; }
242
284
243
285
// simple value means shared lookup
244
- if ( isSimpleValue (arguments .constraints ) AND len ( arguments .constraints ) ){
245
- if ( ! sharedConstraintsExists (arguments .constraints ) ){
246
- throw (message = " The shared constraint you requested (#arguments .constraints #) does not exist" ,
247
- detail = " Valid constraints are: #structKeyList (sharedConstraints ) #" ,
248
- type = " ValidationManager.InvalidSharedConstraint" );
286
+ if ( isSimpleValue ( arguments .constraints ) AND len ( arguments .constraints ) ){
287
+ if ( ! sharedConstraintsExists ( arguments .constraints ) ){
288
+ throw (
289
+ message = " The shared constraint you requested (#arguments .constraints #) does not exist" ,
290
+ detail = " Valid constraints are: #structKeyList (sharedConstraints ) #" ,
291
+ type = " ValidationManager.InvalidSharedConstraint"
292
+ );
249
293
}
250
294
// retrieve the shared constraint and return, they are already processed.
251
295
return getSharedConstraints ( arguments .constraints );
@@ -257,8 +301,10 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
257
301
258
302
/**
259
303
* Get the constraints structure from target objects, if none, it returns an empty structure
304
+ *
305
+ * @target The target object
260
306
*/
261
- private struct function discoverConstraints (required any target ){
307
+ private struct function discoverConstraints ( required any target ){
262
308
if ( structKeyExists (arguments .target ," constraints" ) ){
263
309
return arguments .target .constraints ;
264
310
}
0 commit comments