@@ -87,15 +87,24 @@ component
87
87
* @sharedConstraints A structure of shared constraints
88
88
*/
89
89
ValidationManager function init ( struct sharedConstraints = {} ){
90
- // valid validator registrations
91
- variables .validValidators = " required,type,size,range,regex,sameAs,sameAsNoCase,inList,discrete,udf,method,validator,min,max" ;
92
90
// store shared constraints if passed
93
91
variables .sharedConstraints = arguments .sharedConstraints ;
94
92
// Validators Path
95
93
variables .validatorsPath = getDirectoryFromPath ( getMetadata ( this ).path ) & " validators" ;
96
94
// Register validators
97
- variables .registeredValidators = directoryList (
98
- variables .validatorsPath ,
95
+ variables .registeredValidators = discoverValidators ( variables .validatorsPath );
96
+ // Register aliases
97
+ variables .validatorAliases = {
98
+ " items" : " arrayItem" ,
99
+ " constraints" : " nestedConstraints"
100
+ };
101
+
102
+ return this ;
103
+ }
104
+
105
+ public struct function discoverValidators ( required string path ){
106
+ return directoryList (
107
+ arguments .path ,
99
108
false ,
100
109
" name" ,
101
110
" *.cfc"
@@ -113,8 +122,6 @@ component
113
122
result [ item .replaceNoCase ( " Validator" , " " ) ] = " cbvalidation.models.validators.#item #" ;
114
123
return result ;
115
124
}, {} );
116
-
117
- return this ;
118
125
}
119
126
120
127
/**
@@ -162,6 +169,9 @@ component
162
169
arguments .constraints
163
170
);
164
171
172
+ expandConstraintShortcuts ( allConstraints );
173
+ // writeDump( var = allConstraints );
174
+
165
175
// create new result object
166
176
var results = wirebox .getInstance (
167
177
name = " cbvalidation.models.result.ValidationResult" ,
@@ -328,6 +338,16 @@ component
328
338
required string validatorType ,
329
339
required any validationData
330
340
){
341
+ // Are we an alias?
342
+ if (
343
+ structKeyExists (
344
+ variables .validatorAliases ,
345
+ arguments .validatorType
346
+ )
347
+ ) {
348
+ arguments .validatorType = variables .validatorAliases [ arguments .validatorType ];
349
+ }
350
+
331
351
// Are we a core validator?
332
352
if (
333
353
structKeyExists (
@@ -459,4 +479,62 @@ component
459
479
return ( structKeyExists ( arguments .target , " constraints" ) ? arguments .target .constraints : {} );
460
480
}
461
481
482
+ private void function expandConstraintShortcuts ( required struct constraints ){
483
+ for ( var key in arguments .constraints ) {
484
+ if ( listLen ( key , " ." ) > 1 ) {
485
+ // is an object or an array shortcut
486
+ expandNestedConstraint (
487
+ constraintSlice = arguments .constraints ,
488
+ constraints = arguments .constraints [ key ],
489
+ currentKey = listFirst ( key , " ." ),
490
+ nestedKeys = listRest ( key , " ." )
491
+ );
492
+ structDelete ( arguments .constraints , key );
493
+ }
494
+ }
495
+ }
496
+
497
+ private void function expandNestedConstraint (
498
+ required struct constraintSlice ,
499
+ required struct constraints ,
500
+ required string currentKey ,
501
+ string nestedKeys = " "
502
+ ){
503
+ if ( arguments .nestedKeys == " " ) {
504
+ arguments .constraintSlice [ currentKey ] = arguments .constraints ;
505
+ return ;
506
+ }
507
+
508
+ if ( ! arguments .constraintSlice .keyExists ( currentKey ) ) {
509
+ arguments .constraintSlice [ currentKey ] = {};
510
+ }
511
+
512
+ var nextKey = listFirst ( arguments .nestedKeys , " ." );
513
+ var nextSlice = {};
514
+ var nextKeys = listRest ( arguments .nestedKeys , " ." );
515
+ if ( nextKey == " *" ) {
516
+ nextKey = listFirst ( nextKeys , " ." );
517
+ nextKeys = listRest ( nextKeys , " ." );
518
+ if ( nextKey == " " ) {
519
+ arguments .constraintSlice [ currentKey ][ " arrayItem" ] = arguments .constraints ;
520
+ return ;
521
+ }
522
+ if ( ! arguments .constraintSlice [ currentKey ].keyExists ( " arrayItem" ) ) {
523
+ arguments .constraintSlice [ currentKey ][ " arrayItem" ] = { " constraints" : {} };
524
+ }
525
+ nextSlice = arguments .constraintSlice [ currentKey ][ " arrayItem" ][ " constraints" ];
526
+ } else {
527
+ if ( ! arguments .constraintSlice [ currentKey ].keyExists ( " constraints" ) ) {
528
+ arguments .constraintSlice [ currentKey ][ " constraints" ] = {};
529
+ }
530
+ nextSlice = arguments .constraintSlice [ currentKey ][ " constraints" ];
531
+ }
532
+ expandNestedConstraint (
533
+ constraintSlice = nextSlice ,
534
+ constraints = arguments .constraints ,
535
+ currentKey = nextKey ,
536
+ nestedKeys = nextKeys
537
+ );
538
+ }
539
+
462
540
}
0 commit comments