Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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!
  • Loading branch information
elpete committed Feb 16, 2022
commit ea901500c8fca9febf6ab4356e673c44ceb7ece6
63 changes: 63 additions & 0 deletions models/validators/EmptyValidator.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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"
);
}

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;
}

}
77 changes: 77 additions & 0 deletions test-harness/tests/specs/validators/EmptyValidatorTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* *******************************************************************************
* *******************************************************************************
*/
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();
} );

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();
} );
} );
} );
}

}