Skip to content

Commit

Permalink
* feature : Updated RequiredUnless and RequiredIf to use struct…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
lmajano committed Feb 4, 2020
1 parent e010706 commit a618ed7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion box.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"ColdBox Validation",
"author":"Ortus Solutions <[email protected]>",
"version":"2.0.1",
"version":"2.1.0",
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbvalidation/@build.version@/[email protected]@.zip",
"slug":"cbvalidation",
"type":"modules",
Expand Down
7 changes: 4 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# CHANGELOG

## 2.0.1
## 2.1.0

* Updated `RequiredUnless` and `RequiredIf` to use struct literal notation instead of the weird parsing we did.
* Added the `Unique` validator thanks to @elpete!
* `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

## 2.0.0

Expand Down
4 changes: 4 additions & 0 deletions models/validators/RequiredIfValidator.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ component accessors="true" extends="RequiredValidator" singleton {
.map( function( key, value ){
// Get comparison values
var comparePropertyValue = invoke( target, "get#key#" );
// Null checks
if( isNull( comparePropertyValue ) ){
return isNull( arguments.value );
}
// Check if the compareValue is the same as the defined one
return ( arguments.value == comparePropertyValue ? true : false );
} )
Expand Down
4 changes: 4 additions & 0 deletions models/validators/RequiredUnlessValidator.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ component accessors="true" extends="RequiredValidator" singleton {
.map( function( key, value ){
// Get comparison values
var comparePropertyValue = invoke( target, "get#key#" );
// Null checks
if( isNull( comparePropertyValue ) ){
return isNull( arguments.value );
}
// Check if the compareValue is the same as the defined one
return ( arguments.value == comparePropertyValue ? true : false );
} )
Expand Down
18 changes: 14 additions & 4 deletions test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
it( "can make targets required if the properties passed have the right value", function(){
var mock = createStub()
.$( "getName", "luis" )
.$( "getRole", "admin" );
.$( "getRole", "admin" )
.$( "getMissing", javacast( "null", "" ) );
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();

expect(
model.validate( result, mock, "testField", javacast( "null", "" ), {
missing : javaCast( "null", "" )
} )
).toBeFalse();

expect(
model.validate( result, mock, "testField", "", {
name : "luis"
Expand Down Expand Up @@ -60,11 +67,14 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
} )
).toBeFalse();



expect(
model.validate( result, mock, "testField", "shouldPass", {
missing : javaCast( "null", "" )
} )
).toBeTrue();
} );

});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod

function run( testResults, testBox ){
// all your suites go here.
describe( "Accepted", function(){
describe( "RequiredUnless", function(){

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


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

expect(
model.validate( result, mock, "testField", javaCast( "null", "" ), {
missing : javaCast( "null", "" )
} )
).toBeFalse();

expect(
model.validate( result, mock, "testField", "not null", {
missing : javaCast( "null", "" )
} )
).toBeTrue();

} );

});
}

}
}

0 comments on commit a618ed7

Please sign in to comment.