diff --git a/models/validators/EmptyValidator.cfc b/models/validators/EmptyValidator.cfc new file mode 100644 index 0000000..5d75487 --- /dev/null +++ b/models/validators/EmptyValidator.cfc @@ -0,0 +1,68 @@ +/** + * Copyright since 2020 by Ortus Solutions, Corp + * www.ortussolutions.com + * --- + * This validator checks if a field has a value + */ +component extends="BaseValidator" accessors="true" singleton { + + /** + * Constructor + */ + EmptyValidator function init(){ + variables.name = "Empty"; + return this; + } + + /** + * Will check if an incoming value validates + * + * @validationResult The result object of the validation + * @target The target object to validate on + * @field The field on the target object to validate on + * @targetValue The target value to validate + * @validationData The validation data the validator was created with + * @rules The rules imposed on the currently validating field + */ + boolean function validate( + required any validationResult, + required any target, + required string field, + any targetValue, + any validationData, + struct rules + ){ + // check + if ( !isBoolean( arguments.validationData ) ) { + throw( + message = "The Empty validator data needs to be boolean and you sent in: #arguments.validationData#", + type = "EmptyValidator.InvalidValidationData" + ); + } + + // return true if no data to check, empty needs a data element to be checked. + if ( isNull( arguments.targetValue ) ) { + return true; + } + + var isFilled = hasValue( arguments.targetValue ); + if ( arguments.validationData && !isFilled ) { + return true; + } + + // No data, fail it + var args = { + message : "The '#arguments.field#' value should #arguments.validationData ? "" : "not "#be empty", + field : arguments.field, + validationType : getName(), + rejectedValue : ( + isNull( arguments.targetValue ) ? "NULL" : isSimpleValue( arguments.targetValue ) ? arguments.targetValue : "" + ), + validationData : arguments.validationData + }; + + validationResult.addError( validationResult.newError( argumentCollection = args ) ); + return false; + } + +} diff --git a/test-harness/tests/Application.cfc b/test-harness/tests/Application.cfc index c2865e2..62db57c 100644 --- a/test-harness/tests/Application.cfc +++ b/test-harness/tests/Application.cfc @@ -1,5 +1,4 @@ - -component { +component { // APPLICATION CFC PROPERTIES this.name = "ColdBoxTestingSuite" & hash( getCurrentTemplatePath() ); diff --git a/test-harness/tests/specs/GenericObjectTest.cfc b/test-harness/tests/specs/GenericObjectTest.cfc index b40ffdc..800bda7 100644 --- a/test-harness/tests/specs/GenericObjectTest.cfc +++ b/test-harness/tests/specs/GenericObjectTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.GenericObject" { /*********************************** LIFE CYCLE Methods ***********************************/ diff --git a/test-harness/tests/specs/result/ValidationErrorTest.cfc b/test-harness/tests/specs/result/ValidationErrorTest.cfc index 96bf7b7..0e575fb 100644 --- a/test-harness/tests/specs/result/ValidationErrorTest.cfc +++ b/test-harness/tests/specs/result/ValidationErrorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.result.ValidationError" { function setup(){ diff --git a/test-harness/tests/specs/result/ValidationResultTest.cfc b/test-harness/tests/specs/result/ValidationResultTest.cfc index 9b6b991..76b86a8 100644 --- a/test-harness/tests/specs/result/ValidationResultTest.cfc +++ b/test-harness/tests/specs/result/ValidationResultTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.result.ValidationResult" { function setup(){ diff --git a/test-harness/tests/specs/validators/DiscreteValidatorTest.cfc b/test-harness/tests/specs/validators/DiscreteValidatorTest.cfc index fb5d877..a3a747b 100644 --- a/test-harness/tests/specs/validators/DiscreteValidatorTest.cfc +++ b/test-harness/tests/specs/validators/DiscreteValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.DiscreteValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/EmptyValidatorTest.cfc b/test-harness/tests/specs/validators/EmptyValidatorTest.cfc new file mode 100644 index 0000000..c41593d --- /dev/null +++ b/test-harness/tests/specs/validators/EmptyValidatorTest.cfc @@ -0,0 +1,89 @@ +/** + * ******************************************************************************* + * ******************************************************************************* + */ +component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.EmptyValidator" { + + function beforeAll(){ + super.beforeAll(); + super.setup(); + model.init(); + } + + function run(){ + describe( "empty validator", function(){ + beforeEach( function(){ + variables.result = createMock( "cbvalidation.models.result.ValidationResult" ).init(); + } ); + + it( "skips over null values", function(){ + expect( + model.validate( + variables.result, + this, + "test", + javacast( "null", "" ), + true + ) + ).toBeTrue(); + } ); + + describe( "empty: true", function(){ + it( "passes when given an empty string", function(){ + expect( model.validate( variables.result, this, "test", "", true ) ).toBeTrue(); + } ); + + it( "passes when given an empty array", function(){ + expect( model.validate( variables.result, this, "test", [], true ) ).toBeTrue(); + } ); + + it( "passes when given an empty struct", function(){ + expect( model.validate( variables.result, this, "test", {}, true ) ).toBeTrue(); + } ); + + it( "fails when given a non-empty string", function(){ + expect( model.validate( result, this, "test", "woot", true ) ).toBeFalse(); + } ); + + it( "fails when given a number", function(){ + expect( model.validate( result, this, "test", 42.155, true ) ).toBeFalse(); + } ); + + it( "fails when given a date", function(){ + expect( model.validate( result, this, "test", now(), true ) ).toBeFalse(); + } ); + + it( "fails when given a non-empty array", function(){ + expect( model.validate( variables.result, this, "test", [ 1 ], true ) ).toBeFalse(); + } ); + + it( "fails when given a non-empty struct", function(){ + expect( + model.validate( + variables.result, + this, + "test", + { "foo" : "bar" }, + true + ) + ).toBeFalse(); + } ); + } ); + + describe( "empty: false", function(){ + it( "fails when given an empty string", function(){ + expect( model.validate( variables.result, this, "test", "", false ) ).toBeFalse(); + } ); + + it( "fails when given an empty array", function(){ + expect( model.validate( variables.result, this, "test", [], false ) ).toBeFalse(); + } ); + + it( "fails when given an empty struct", function(){ + expect( model.validate( variables.result, this, "test", {}, false ) ).toBeFalse(); + } ); + } ); + } ); + } + +} diff --git a/test-harness/tests/specs/validators/InListValidatorTest.cfc b/test-harness/tests/specs/validators/InListValidatorTest.cfc index 2a2540b..0ed54bf 100644 --- a/test-harness/tests/specs/validators/InListValidatorTest.cfc +++ b/test-harness/tests/specs/validators/InListValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.InListValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/MaxValidatorTest.cfc b/test-harness/tests/specs/validators/MaxValidatorTest.cfc index 0d0b4cd..c2be87c 100644 --- a/test-harness/tests/specs/validators/MaxValidatorTest.cfc +++ b/test-harness/tests/specs/validators/MaxValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.MaxValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/MethodValidatorTest.cfc b/test-harness/tests/specs/validators/MethodValidatorTest.cfc index 451c281..e7b1fcc 100644 --- a/test-harness/tests/specs/validators/MethodValidatorTest.cfc +++ b/test-harness/tests/specs/validators/MethodValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.MethodValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/MinValidatorTest.cfc b/test-harness/tests/specs/validators/MinValidatorTest.cfc index 4a219f5..3f3046b 100644 --- a/test-harness/tests/specs/validators/MinValidatorTest.cfc +++ b/test-harness/tests/specs/validators/MinValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.MinValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/RegexValidatorTest.cfc b/test-harness/tests/specs/validators/RegexValidatorTest.cfc index e9a6580..607d801 100644 --- a/test-harness/tests/specs/validators/RegexValidatorTest.cfc +++ b/test-harness/tests/specs/validators/RegexValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.RegexValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/RequiredValidatorTest.cfc b/test-harness/tests/specs/validators/RequiredValidatorTest.cfc index 83e669b..c58a4c5 100644 --- a/test-harness/tests/specs/validators/RequiredValidatorTest.cfc +++ b/test-harness/tests/specs/validators/RequiredValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.RequiredValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/SameAsNoCaseValidatorTest.cfc b/test-harness/tests/specs/validators/SameAsNoCaseValidatorTest.cfc index f4c5c79..2bb1ff5 100644 --- a/test-harness/tests/specs/validators/SameAsNoCaseValidatorTest.cfc +++ b/test-harness/tests/specs/validators/SameAsNoCaseValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model ="cbvalidation.models.validators.SameAsNoCaseValidator" diff --git a/test-harness/tests/specs/validators/SameAsValidatorTest.cfc b/test-harness/tests/specs/validators/SameAsValidatorTest.cfc index 6964943..4f265a1 100644 --- a/test-harness/tests/specs/validators/SameAsValidatorTest.cfc +++ b/test-harness/tests/specs/validators/SameAsValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.SameAsValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/SizeValidatorTest.cfc b/test-harness/tests/specs/validators/SizeValidatorTest.cfc index 1c79b8a..28620a7 100644 --- a/test-harness/tests/specs/validators/SizeValidatorTest.cfc +++ b/test-harness/tests/specs/validators/SizeValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.SizeValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/TypeValidatorTest.cfc b/test-harness/tests/specs/validators/TypeValidatorTest.cfc index 482efa9..f52b108 100644 --- a/test-harness/tests/specs/validators/TypeValidatorTest.cfc +++ b/test-harness/tests/specs/validators/TypeValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.TypeValidator" { function setup(){ diff --git a/test-harness/tests/specs/validators/UDFValidatorTest.cfc b/test-harness/tests/specs/validators/UDFValidatorTest.cfc index 3b12d81..c9172f1 100644 --- a/test-harness/tests/specs/validators/UDFValidatorTest.cfc +++ b/test-harness/tests/specs/validators/UDFValidatorTest.cfc @@ -1,7 +1,7 @@ /** -* ******************************************************************************* -* ******************************************************************************* -*/ + * ******************************************************************************* + * ******************************************************************************* + */ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.UDFValidator" { function setup(){