Skip to content

Commit 320dac3

Browse files
committed
* New validators: notSameAs, notSameAsNoCase
1 parent 834cd04 commit 320dac3

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright since 2020 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This validator validates if a field is NOT the same as another field with no case sensitivity
6+
*/
7+
component extends="BaseValidator" accessors="true" singleton {
8+
9+
/**
10+
* Constructor
11+
*/
12+
NotSameAsNoCaseValidator function init(){
13+
variables.name = "NotSameAsNoCase";
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+
// get secondary value from property
36+
var compareValue = invoke( arguments.target, "get#arguments.validationData#" );
37+
38+
// Check if both null values
39+
if ( isNull( arguments.targetValue ) && isNull( compareValue ) ) {
40+
return true;
41+
}
42+
43+
// return true if no data to check, type needs a data element to be checked.
44+
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
45+
return true;
46+
}
47+
48+
// Evaluate now
49+
if ( compareNoCase( arguments.targetValue, compareValue ) NEQ 0 ) {
50+
return true;
51+
}
52+
53+
var args = {
54+
message : "The '#arguments.field#' value is the same as #compareValue.toString()#",
55+
field : arguments.field,
56+
validationType : getName(),
57+
rejectedValue : ( isSimpleValue( arguments.targetValue ) ? arguments.targetValue : "" ),
58+
validationData : arguments.validationData
59+
};
60+
var error = validationResult
61+
.newError( argumentCollection = args )
62+
.setErrorMetadata( { "notsameas" : arguments.validationData } );
63+
validationResult.addError( error );
64+
return false;
65+
}
66+
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright since 2020 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This validator validates if a field is NOT the same as another field with case sensitivity
6+
*/
7+
component extends="BaseValidator" accessors="true" singleton {
8+
9+
/**
10+
* Constructor
11+
*/
12+
NotSameAsValidator function init(){
13+
variables.name = "NotSameAs";
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+
// return true if no data to check, type needs a data element to be checked.
36+
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
37+
return true;
38+
}
39+
40+
// get secondary value from property
41+
var compareValue = invoke( arguments.target, "get#arguments.validationData#" );
42+
43+
// Compare it now
44+
if ( compare( arguments.targetValue, compareValue ) NEQ 0 ) {
45+
return true;
46+
}
47+
var args = {
48+
message : "The '#arguments.field#' value is the same as #compareValue.toString()#",
49+
field : arguments.field,
50+
validationType : getName(),
51+
rejectedValue : ( isSimpleValue( arguments.targetValue ) ? arguments.targetValue : "" ),
52+
validationData : arguments.validationData
53+
};
54+
var error = validationResult
55+
.newError( argumentCollection = args )
56+
.setErrorMetadata( { "notsameas" : arguments.validationData } );
57+
validationResult.addError( error );
58+
return false;
59+
}
60+
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
5+
component
6+
extends="coldbox.system.testing.BaseModelTest"
7+
model ="cbvalidation.models.validators.NotSameAsNoCaseValidator"
8+
{
9+
10+
function setup(){
11+
super.setup();
12+
model.init();
13+
}
14+
15+
function testValidate(){
16+
result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
17+
18+
// luis not the same as data
19+
r = model.validate(
20+
validationResult: result,
21+
target : this,
22+
field : "test",
23+
targetValue : "data",
24+
validationData : "luis"
25+
);
26+
expect( r ).toBeTrue();
27+
28+
// luis is the same as data
29+
r = model.validate(
30+
validationResult: result,
31+
target : this,
32+
field : "test",
33+
targetValue : "luis",
34+
validationData : "luis"
35+
);
36+
expect( r ).toBeFalse();
37+
}
38+
39+
function getLuis(){
40+
return "luis";
41+
}
42+
43+
function getData(){
44+
return "data";
45+
}
46+
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
5+
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.NotSameAsValidator" {
6+
7+
function setup(){
8+
super.setup();
9+
model.init();
10+
}
11+
12+
function testValidate(){
13+
result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
14+
15+
16+
// luis not the same as data
17+
r = model.validate(
18+
validationResult: result,
19+
target : this,
20+
field : "test",
21+
targetValue : "luis",
22+
validationData : "data"
23+
);
24+
expect( r ).toBeTrue();
25+
26+
// luis is the same as data
27+
r = model.validate(
28+
validationResult: result,
29+
target : this,
30+
field : "test",
31+
targetValue : "luis",
32+
validationData : "luis"
33+
);
34+
expect( r ).toBeFalse();
35+
}
36+
37+
function getLuis(){
38+
return "luis";
39+
}
40+
41+
function getData(){
42+
return "data";
43+
}
44+
45+
}

0 commit comments

Comments
 (0)