Skip to content

Commit 5f42393

Browse files
authored
Merge pull request #61 from coldbox-modules/empty
Add an EmptyValidator
2 parents c7d342a + bca68c1 commit 5f42393

18 files changed

+203
-47
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright since 2020 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This validator checks if a field has a value
6+
*/
7+
component extends="BaseValidator" accessors="true" singleton {
8+
9+
/**
10+
* Constructor
11+
*/
12+
EmptyValidator function init(){
13+
variables.name = "Empty";
14+
return this;
15+
}
16+
17+
/**
18+
* Will check if an incoming value validates
19+
*
20+
* @validationResult The result object of the validation
21+
* @target The target object to validate on
22+
* @field The field on the target object to validate on
23+
* @targetValue The target value to validate
24+
* @validationData The validation data the validator was created with
25+
* @rules The rules imposed on the currently validating field
26+
*/
27+
boolean function validate(
28+
required any validationResult,
29+
required any target,
30+
required string field,
31+
any targetValue,
32+
any validationData,
33+
struct rules
34+
){
35+
// check
36+
if ( !isBoolean( arguments.validationData ) ) {
37+
throw(
38+
message = "The Empty validator data needs to be boolean and you sent in: #arguments.validationData#",
39+
type = "EmptyValidator.InvalidValidationData"
40+
);
41+
}
42+
43+
// return true if no data to check, empty needs a data element to be checked.
44+
if ( isNull( arguments.targetValue ) ) {
45+
return true;
46+
}
47+
48+
var isFilled = hasValue( arguments.targetValue );
49+
if ( arguments.validationData && !isFilled ) {
50+
return true;
51+
}
52+
53+
// No data, fail it
54+
var args = {
55+
message : "The '#arguments.field#' value should #arguments.validationData ? "" : "not "#be empty",
56+
field : arguments.field,
57+
validationType : getName(),
58+
rejectedValue : (
59+
isNull( arguments.targetValue ) ? "NULL" : isSimpleValue( arguments.targetValue ) ? arguments.targetValue : ""
60+
),
61+
validationData : arguments.validationData
62+
};
63+
64+
validationResult.addError( validationResult.newError( argumentCollection = args ) );
65+
return false;
66+
}
67+
68+
}

test-harness/tests/Application.cfc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
component {
1+
component {
32

43
// APPLICATION CFC PROPERTIES
54
this.name = "ColdBoxTestingSuite" & hash( getCurrentTemplatePath() );

test-harness/tests/specs/GenericObjectTest.cfc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.GenericObject" {
66

77
/*********************************** LIFE CYCLE Methods ***********************************/

test-harness/tests/specs/result/ValidationErrorTest.cfc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.result.ValidationError" {
66

77
function setup(){

test-harness/tests/specs/result/ValidationResultTest.cfc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.result.ValidationResult" {
66

77
function setup(){

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.DiscreteValidator" {
66

77
function setup(){
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
5+
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.EmptyValidator" {
6+
7+
function beforeAll(){
8+
super.beforeAll();
9+
super.setup();
10+
model.init();
11+
}
12+
13+
function run(){
14+
describe( "empty validator", function(){
15+
beforeEach( function(){
16+
variables.result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
17+
} );
18+
19+
it( "skips over null values", function(){
20+
expect(
21+
model.validate(
22+
variables.result,
23+
this,
24+
"test",
25+
javacast( "null", "" ),
26+
true
27+
)
28+
).toBeTrue();
29+
} );
30+
31+
describe( "empty: true", function(){
32+
it( "passes when given an empty string", function(){
33+
expect( model.validate( variables.result, this, "test", "", true ) ).toBeTrue();
34+
} );
35+
36+
it( "passes when given an empty array", function(){
37+
expect( model.validate( variables.result, this, "test", [], true ) ).toBeTrue();
38+
} );
39+
40+
it( "passes when given an empty struct", function(){
41+
expect( model.validate( variables.result, this, "test", {}, true ) ).toBeTrue();
42+
} );
43+
44+
it( "fails when given a non-empty string", function(){
45+
expect( model.validate( result, this, "test", "woot", true ) ).toBeFalse();
46+
} );
47+
48+
it( "fails when given a number", function(){
49+
expect( model.validate( result, this, "test", 42.155, true ) ).toBeFalse();
50+
} );
51+
52+
it( "fails when given a date", function(){
53+
expect( model.validate( result, this, "test", now(), true ) ).toBeFalse();
54+
} );
55+
56+
it( "fails when given a non-empty array", function(){
57+
expect( model.validate( variables.result, this, "test", [ 1 ], true ) ).toBeFalse();
58+
} );
59+
60+
it( "fails when given a non-empty struct", function(){
61+
expect(
62+
model.validate(
63+
variables.result,
64+
this,
65+
"test",
66+
{ "foo" : "bar" },
67+
true
68+
)
69+
).toBeFalse();
70+
} );
71+
} );
72+
73+
describe( "empty: false", function(){
74+
it( "fails when given an empty string", function(){
75+
expect( model.validate( variables.result, this, "test", "", false ) ).toBeFalse();
76+
} );
77+
78+
it( "fails when given an empty array", function(){
79+
expect( model.validate( variables.result, this, "test", [], false ) ).toBeFalse();
80+
} );
81+
82+
it( "fails when given an empty struct", function(){
83+
expect( model.validate( variables.result, this, "test", {}, false ) ).toBeFalse();
84+
} );
85+
} );
86+
} );
87+
}
88+
89+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.InListValidator" {
66

77
function setup(){

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.MaxValidator" {
66

77
function setup(){

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* *******************************************************************************
3-
* *******************************************************************************
4-
*/
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
55
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.MethodValidator" {
66

77
function setup(){

0 commit comments

Comments
 (0)