Skip to content

Commit 98048aa

Browse files
committed
Return true for null values in constraints validator
If the value is required, make sure to specify that using the required validator.
1 parent 2b2e314 commit 98048aa

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

models/validators/NestedConstraintsValidator.cfc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ component
3535
struct rules
3636
){
3737
// return true if no data to check, type needs a data element to be checked.
38-
if ( isNull( arguments.targetValue ) || !isStruct( arguments.targetValue ) ) {
38+
if ( isNull( arguments.targetValue ) ) {
39+
return true;
40+
}
41+
42+
// return false if the value is not a struct.
43+
if ( !isStruct( arguments.targetValue ) ) {
3944
var args = {
4045
message : "The '#arguments.field#' value '#(
4146
isSimpleValue( arguments.targetValue ) ? arguments.targetValue : serializeJSON(

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
8989
expect( vResult.getAllErrors() ).toBeEmpty();
9090
} );
9191

92+
it( "does no validation when a null value is passed", function(){
93+
var vResult = createMock( "cbvalidation.models.result.ValidationResult" ).init();
94+
expect(
95+
validator.validate(
96+
validationResult: vResult,
97+
target : this,
98+
field : "address",
99+
targetValue : javacast( "null", "" ),
100+
validationData : {
101+
"streetOne" : { required : true, type : "string" },
102+
"streetTwo" : { required : false, type : "string" },
103+
"city" : { required : true, type : "string" },
104+
"state" : {
105+
required : true,
106+
type : "string",
107+
size : 2
108+
},
109+
"zip" : {
110+
required : true,
111+
type : "numeric",
112+
size : 5
113+
}
114+
},
115+
rules: {}
116+
)
117+
).toBeTrue();
118+
expect( vResult.getAllErrors() ).toBeEmpty();
119+
} );
120+
92121
it( "shows the nested field name when a nested constraint fails", function(){
93122
var vResult = createMock( "cbvalidation.models.result.ValidationResult" ).init();
94123
expect(

0 commit comments

Comments
 (0)