2
2
* Copyright since 2020 by Ortus Solutions, Corp
3
3
* www.ortussolutions.com
4
4
* ---
5
- * This validator checks if a field has value and not null
5
+ * This validator checks a struct of key-value pairs passed in the validation data.
6
+ * If those key-value pairs are equal then the target field will NOT be required
6
7
*/
7
8
component accessors = " true" extends = " RequiredValidator" singleton {
8
9
@@ -18,11 +19,12 @@ component accessors="true" extends="RequiredValidator" singleton {
18
19
19
20
/**
20
21
* Will check if an incoming value validates
21
- * @validationResultThe result object of the validation
22
- * @targetThe target object to validate on
23
- * @fieldThe field on the target object to validate on
24
- * @targetValueThe target value to validate
25
- * @validationDataThe validation data the validator was created with
22
+ *
23
+ * @validationResult The result object of the validation
24
+ * @target The target object to validate on
25
+ * @field The field on the target object to validate on
26
+ * @targetValue The target value to validate
27
+ * @validationData The validation data the validator was created with
26
28
*/
27
29
boolean function validate (
28
30
required any validationResult ,
@@ -31,24 +33,26 @@ component accessors="true" extends="RequiredValidator" singleton {
31
33
any targetValue ,
32
34
any validationData
33
35
){
34
- // Validation Data Format: property:value,...
35
- var validationArray = arguments .validationData .listToArray ();
36
- // Inflate to array to test multiple properties
37
- var isOptional = validationArray
38
- .map ( function ( item ){
36
+ // If you passed in simple data, conver it to a struct, simple values are not evaluated
37
+ if ( isSimpleValue ( arguments .validationData ) ){
38
+ arguments .validationData = {};
39
+ }
40
+
41
+ // Test the data
42
+ var isOptional = arguments .validationData
43
+ .map ( function ( key , value ){
39
44
// Get comparison values
40
- var compareProperty = getToken ( arguments .item , 1 , " :" );
41
- var compareValue = getToken ( arguments .item , 2 , " :" );
42
- var comparePropertyValue = invoke ( target , " get#compareProperty #" );
45
+ var comparePropertyValue = invoke ( target , " get#key #" );
43
46
// Check if the compareValue is the same as the defined one
44
- return ( compareValue == comparePropertyValue ? true : false );
47
+ return ( arguments . value == comparePropertyValue ? true : false );
45
48
} )
46
49
// AND them all for a single result
47
- .reduce ( function ( result , item ){
48
- return ( arguments .item && arguments .result );
50
+ .reduce ( function ( result , key , value ){
51
+ return ( arguments .value && arguments .result );
49
52
}, true );
50
53
51
- if ( validationArray .len () && isOptional ){
54
+ // If we have data, then test the optional
55
+ if ( arguments .validationData .count () && isOptional ){
52
56
return true ;
53
57
}
54
58
0 commit comments