Skip to content

Commit ea90150

Browse files
committed
Add an EmptyValidator
This validator is useful when a field is not required but if it exists it cannot be empty. This is needed since type validators ignore empty values as valid, but an empty string is not a date!
1 parent c7d342a commit ea90150

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
var isFilled = hasValue( arguments.targetValue );
44+
if ( arguments.validationData && !isFilled ) {
45+
return true;
46+
}
47+
48+
// No data, fail it
49+
var args = {
50+
message : "The '#arguments.field#' value should #arguments.validationData ? "" : "not "#be empty",
51+
field : arguments.field,
52+
validationType : getName(),
53+
rejectedValue : (
54+
isNull( arguments.targetValue ) ? "NULL" : isSimpleValue( arguments.targetValue ) ? arguments.targetValue : ""
55+
),
56+
validationData : arguments.validationData
57+
};
58+
59+
validationResult.addError( validationResult.newError( argumentCollection = args ) );
60+
return false;
61+
}
62+
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
describe( "empty: true", function(){
20+
it( "passes when given an empty string", function(){
21+
expect( model.validate( variables.result, this, "test", "", true ) ).toBeTrue();
22+
} );
23+
24+
it( "passes when given an empty array", function(){
25+
expect( model.validate( variables.result, this, "test", [], true ) ).toBeTrue();
26+
} );
27+
28+
it( "passes when given an empty struct", function(){
29+
expect( model.validate( variables.result, this, "test", {}, true ) ).toBeTrue();
30+
} );
31+
32+
it( "fails when given a non-empty string", function(){
33+
expect( model.validate( result, this, "test", "woot", true ) ).toBeFalse();
34+
} );
35+
36+
it( "fails when given a number", function(){
37+
expect( model.validate( result, this, "test", 42.155, true ) ).toBeFalse();
38+
} );
39+
40+
it( "fails when given a date", function(){
41+
expect( model.validate( result, this, "test", now(), true ) ).toBeFalse();
42+
} );
43+
44+
it( "fails when given a non-empty array", function(){
45+
expect( model.validate( variables.result, this, "test", [ 1 ], true ) ).toBeFalse();
46+
} );
47+
48+
it( "fails when given a non-empty struct", function(){
49+
expect(
50+
model.validate(
51+
variables.result,
52+
this,
53+
"test",
54+
{ "foo" : "bar" },
55+
true
56+
)
57+
).toBeFalse();
58+
} );
59+
} );
60+
61+
describe( "empty: false", function(){
62+
it( "fails when given an empty string", function(){
63+
expect( model.validate( variables.result, this, "test", "", false ) ).toBeFalse();
64+
} );
65+
66+
it( "fails when given an empty array", function(){
67+
expect( model.validate( variables.result, this, "test", [], false ) ).toBeFalse();
68+
} );
69+
70+
it( "fails when given an empty struct", function(){
71+
expect( model.validate( variables.result, this, "test", {}, false ) ).toBeFalse();
72+
} );
73+
} );
74+
} );
75+
}
76+
77+
}

0 commit comments

Comments
 (0)