Skip to content

Commit f41e0f0

Browse files
authored
Merge pull request #81 from coldbox-modules/development
Release v4.4
2 parents 7dd1273 + 8791353 commit f41e0f0

File tree

8 files changed

+74
-21
lines changed

8 files changed

+74
-21
lines changed

box.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name":"ColdBox Validation",
33
"author":"Ortus Solutions <[email protected]>",
4-
"version":"4.3.1",
4+
"version":"4.4.0",
55
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbvalidation/@build.version@/[email protected]@.zip",
66
"slug":"cbvalidation",
77
"type":"modules",

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
- requiredIf accepts a UDF and closure now
15+
16+
### Fixed
17+
18+
- UDF validator now treats nulls correctly
19+
1220
## [4.3.1] - 2023-06-15
1321

1422
### Fixed

models/validators/MethodValidator.cfc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ component extends="BaseValidator" accessors="true" singleton {
3838
return true;
3939
}
4040

41-
// return true if no data to check, type needs a data element to be checked.
42-
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
43-
return true;
44-
}
45-
4641
// Validate via method
4742
if (
4843
invoke(

models/validators/RequiredIfValidator.cfc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ component
3737
any validationData,
3838
struct rules
3939
){
40-
var isRequired = true;
40+
var isRequired = true;
41+
var errorMetadata = {};
4142

4243
// If you passed in simple data, simply check that the target field has a value
4344
if ( isSimpleValue( arguments.validationData ) && len( arguments.validationData ) ) {
@@ -60,13 +61,24 @@ component
6061
.reduce( function( result, key, value ){
6162
return ( arguments.value && arguments.result );
6263
}, true );
64+
// If passed a UDF/closure
65+
} else if (
66+
isCustomFunction( arguments.validationData ) ||
67+
isClosure( arguments.validationData )
68+
) {
69+
// Validate against the UDF/closure
70+
var isRequired = arguments.validationData(
71+
isNull( arguments.targetValue ) ? javacast( "null", "" ) : arguments.targetValue,
72+
arguments.target,
73+
errorMetadata
74+
);
6375
} else {
6476
validationResult.addError(
6577
validationResult.newError(
6678
message = "The target for RequiredIf must be a simple field name or a struct of field to target value pairs.",
6779
field = arguments.field,
6880
validationType = getName(),
69-
rejectedValue = arguments.validationData,
81+
rejectedValue = isSimpleValue( arguments.validationData ) ? arguments.validationData : "",
7082
validationData = arguments.validationData
7183
)
7284
);
@@ -94,7 +106,9 @@ component
94106
validationData : arguments.validationData
95107
};
96108

97-
validationResult.addError( validationResult.newError( argumentCollection = args ) );
109+
validationResult.addError(
110+
validationResult.newError( argumentCollection = args ).setErrorMetadata( errorMetadata )
111+
);
98112
return false;
99113
}
100114

models/validators/UDFValidator.cfc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ component extends="BaseValidator" accessors="true" singleton {
3434
){
3535
var errorMetadata = {};
3636

37+
// return true if no data to check, type needs a data element to be checked.
38+
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
39+
return true;
40+
}
41+
3742
// Validate against the UDF/closure
3843
var passed = arguments.validationData(
39-
isNull( arguments.targetValue ) ? javacast( "null", "" ) : arguments.targetValue,
44+
arguments.targetValue,
4045
arguments.target,
4146
errorMetadata
4247
);

test-harness/tests/specs/ValidationIntegrations.cfc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
159159
params = {
160160
username : "luis",
161161
email : "[email protected]",
162-
password : "luis"
162+
password : "luis",
163+
status : 4 // should not validate
163164
},
164165
method = "post"
165166
);

test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,47 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
106106

107107
expect( model.validate( result, mock, "testField", "", "name" ) ).toBeFalse();
108108

109-
// expect(
110-
// model.validate(
111-
// result,
112-
// mock,
113-
// "testField",
114-
// "",
115-
// "missing"
116-
// )
117-
// ).toBeTrue();
109+
expect( model.validate( result, mock, "testField", "", "missing" ) ).toBeTrue();
110+
} );
111+
it( "can accept a closure as validationData", function(){
112+
var mock = createStub()
113+
.$( "getName", "luis" )
114+
.$( "getRole", "admin" )
115+
.$( "getMissing", javacast( "null", "" ) );
116+
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
117+
118+
expect( model.validate( result, mock, "testField", "", isRequired1 ) ).toBeFalse();
119+
120+
expect( model.validate( result, mock, "testField", "", isRequired2 ) ).toBeTrue();
121+
} );
122+
it( "can use custom error metadata", function(){
123+
var mock = createStub()
124+
.$( "getName", "luis" )
125+
.$( "getRole", "admin" )
126+
.$( "getMissing", javacast( "null", "" ) );
127+
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
128+
129+
expect( model.validate( result, mock, "testField", "", isRequired3 ) ).toBeFalse();
130+
131+
var errorMetadata = result.getErrors()[ 1 ].getErrorMetadata();
132+
133+
expect( errorMetaData ).toHaveKey( "customMessage" );
134+
expect( errorMetaData.customMessage ).toBe( "This is custom data" );
118135
} );
119136
} );
120137
}
121138

139+
private function isRequired1( value, target, errorMetadata ){
140+
return true;
141+
}
142+
143+
private function isRequired2( value, target, errorMetadata ){
144+
return false;
145+
}
146+
147+
private function isRequired3( value, target, errorMetadata ){
148+
arguments.errorMetadata[ "customMessage" ] = "This is custom data";
149+
return true;
150+
}
151+
122152
}

test-harness/tests/specs/validators/UDFValidatorTest.cfc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
3434
javacast( "null", "" ),
3535
variables.validate3
3636
);
37-
assertEquals( false, r );
37+
assertEquals( true, r );
3838
}
3939

4040
private function validate( value, target ){

0 commit comments

Comments
 (0)