Skip to content

Commit a618ed7

Browse files
committed
* feature : Updated RequiredUnless and RequiredIf to use struct literal notation instead of the weird parsing we did.
* `feature` : Added the `Unique` validator thanks to @elpete! * `improvement` : Added `null` support for the `RequiredIf,RequiredUnless` validators
1 parent e010706 commit a618ed7

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
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":"2.0.1",
4+
"version":"2.1.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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# CHANGELOG
22

3-
## 2.0.1
3+
## 2.1.0
44

5-
* Updated `RequiredUnless` and `RequiredIf` to use struct literal notation instead of the weird parsing we did.
6-
* Added the `Unique` validator thanks to @elpete!
5+
* `feature` : Updated `RequiredUnless` and `RequiredIf` to use struct literal notation instead of the weird parsing we did.
6+
* `feature` : Added the `Unique` validator thanks to @elpete!
7+
* `improvement` : Added `null` support for the `RequiredIf,RequiredUnless` validators
78

89
## 2.0.0
910

models/validators/RequiredIfValidator.cfc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ component accessors="true" extends="RequiredValidator" singleton {
4343
.map( function( key, value ){
4444
// Get comparison values
4545
var comparePropertyValue = invoke( target, "get#key#" );
46+
// Null checks
47+
if( isNull( comparePropertyValue ) ){
48+
return isNull( arguments.value );
49+
}
4650
// Check if the compareValue is the same as the defined one
4751
return ( arguments.value == comparePropertyValue ? true : false );
4852
} )

models/validators/RequiredUnlessValidator.cfc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ component accessors="true" extends="RequiredValidator" singleton {
4343
.map( function( key, value ){
4444
// Get comparison values
4545
var comparePropertyValue = invoke( target, "get#key#" );
46+
// Null checks
47+
if( isNull( comparePropertyValue ) ){
48+
return isNull( arguments.value );
49+
}
4650
// Check if the compareValue is the same as the defined one
4751
return ( arguments.value == comparePropertyValue ? true : false );
4852
} )

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
2525
it( "can make targets required if the properties passed have the right value", function(){
2626
var mock = createStub()
2727
.$( "getName", "luis" )
28-
.$( "getRole", "admin" );
28+
.$( "getRole", "admin" )
29+
.$( "getMissing", javacast( "null", "" ) );
2930
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
3031

32+
expect(
33+
model.validate( result, mock, "testField", javacast( "null", "" ), {
34+
missing : javaCast( "null", "" )
35+
} )
36+
).toBeFalse();
37+
3138
expect(
3239
model.validate( result, mock, "testField", "", {
3340
name : "luis"
@@ -60,11 +67,14 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
6067
} )
6168
).toBeFalse();
6269

63-
64-
70+
expect(
71+
model.validate( result, mock, "testField", "shouldPass", {
72+
missing : javaCast( "null", "" )
73+
} )
74+
).toBeTrue();
6575
} );
6676

6777
});
6878
}
6979

70-
}
80+
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
2020

2121
function run( testResults, testBox ){
2222
// all your suites go here.
23-
describe( "Accepted", function(){
23+
describe( "RequiredUnless", function(){
2424

2525
it( "can make targets required unless the properties passed have the right value", function(){
2626
var mock = createStub()
2727
.$( "getName", "luis" )
28-
.$( "getRole", "admin" );
28+
.$( "getRole", "admin" )
29+
.$( "getMissing", javacast( "null", "" ) );
2930
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
3031

3132

@@ -79,9 +80,21 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
7980
} )
8081
).toBeTrue();
8182

83+
expect(
84+
model.validate( result, mock, "testField", javaCast( "null", "" ), {
85+
missing : javaCast( "null", "" )
86+
} )
87+
).toBeFalse();
88+
89+
expect(
90+
model.validate( result, mock, "testField", "not null", {
91+
missing : javaCast( "null", "" )
92+
} )
93+
).toBeTrue();
94+
8295
} );
8396

8497
});
8598
}
8699

87-
}
100+
}

0 commit comments

Comments
 (0)