From a618ed7636f2258f471523847bc79fa77e63a4fb Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Tue, 4 Feb 2020 10:15:11 -0600 Subject: [PATCH] * `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 --- box.json | 2 +- changelog.md | 7 ++++--- models/validators/RequiredIfValidator.cfc | 4 ++++ models/validators/RequiredUnlessValidator.cfc | 4 ++++ .../validators/RequiredIfValidatorTest.cfc | 18 ++++++++++++++---- .../RequiredUnlessValidatorTest.cfc | 19 ++++++++++++++++--- 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/box.json b/box.json index f071e77..72b3579 100644 --- a/box.json +++ b/box.json @@ -1,7 +1,7 @@ { "name":"ColdBox Validation", "author":"Ortus Solutions ", - "version":"2.0.1", + "version":"2.1.0", "location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbvalidation/@build.version@/cbvalidation-@build.version@.zip", "slug":"cbvalidation", "type":"modules", diff --git a/changelog.md b/changelog.md index dbc0c0f..3ed298f 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/models/validators/RequiredIfValidator.cfc b/models/validators/RequiredIfValidator.cfc index 373eed1..6da077a 100644 --- a/models/validators/RequiredIfValidator.cfc +++ b/models/validators/RequiredIfValidator.cfc @@ -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 ); } ) diff --git a/models/validators/RequiredUnlessValidator.cfc b/models/validators/RequiredUnlessValidator.cfc index 841f904..268378a 100644 --- a/models/validators/RequiredUnlessValidator.cfc +++ b/models/validators/RequiredUnlessValidator.cfc @@ -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 ); } ) diff --git a/test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc b/test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc index 7768532..65bf1ed 100644 --- a/test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc +++ b/test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc @@ -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" @@ -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(); } ); }); } - } \ No newline at end of file + } diff --git a/test-harness/tests/specs/validators/RequiredUnlessValidatorTest.cfc b/test-harness/tests/specs/validators/RequiredUnlessValidatorTest.cfc index ab9cc2c..af9b5c5 100644 --- a/test-harness/tests/specs/validators/RequiredUnlessValidatorTest.cfc +++ b/test-harness/tests/specs/validators/RequiredUnlessValidatorTest.cfc @@ -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(); @@ -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(); + } ); }); } - } \ No newline at end of file +}