Skip to content

Commit b7b5f16

Browse files
committed
Merge branch 'development' of github.com:coldbox-modules/cbox-validation into development
2 parents 65bc82a + 41a9f88 commit b7b5f16

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-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 verifies field type
6+
*/
7+
component extends="BaseValidator" accessors="true" singleton {
8+
9+
/**
10+
* Constructor
11+
*/
12+
InstanceOfValidator function init(){
13+
variables.name = "InstanceOf";
14+
return this;
15+
}
16+
17+
/**
18+
* Will check if an incoming instance type
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+
// value is valid if it is an object and of the correct type
41+
var r = (
42+
isObject( arguments.targetValue ) &&
43+
isInstanceOf( arguments.targetValue, arguments.validationData )
44+
);
45+
46+
if ( !r ) {
47+
var args = {
48+
message : "The '#arguments.field#' has an invalid instance type, expected type is #arguments.validationData#",
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( { "type" : arguments.validationData } );
57+
validationResult.addError( error );
58+
}
59+
60+
return r;
61+
}
62+
63+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* *******************************************************************************
3+
* *******************************************************************************
4+
*/
5+
component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.models.validators.InstanceOfValidator" {
6+
7+
// executes before all suites+specs in the run() method
8+
function beforeAll(){
9+
super.setup();
10+
model.init();
11+
}
12+
13+
// executes after all suites+specs in the run() method
14+
function afterAll(){
15+
}
16+
17+
/*********************************** BDD SUITES ***********************************/
18+
19+
function run( testResults, testBox ){
20+
// all your suites go here.
21+
describe( "InstanceOf", function(){
22+
beforeEach( function( currentSpec ){
23+
result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
24+
} );
25+
26+
it( "validates true when expecting a User and receives a User", function(){
27+
28+
var user = createMock( "root.models.User" ).init();
29+
30+
expect(
31+
model.validate(
32+
validationResult: result,
33+
target : this,
34+
field : "testField",
35+
targetValue : user,
36+
validationData : "user",
37+
rules : {}
38+
)
39+
).toBeTrue();
40+
} );
41+
42+
it( "validates false when expecting a Car and receives a User", function(){
43+
44+
var user = createMock( "root.models.User" ).init();
45+
46+
expect(
47+
model.validate(
48+
validationResult: result,
49+
target : this,
50+
field : "testField",
51+
targetValue : user,
52+
validationData : "car",
53+
rules : {}
54+
)
55+
).toBeFalse();
56+
} );
57+
58+
it( "validates true when passed nothing", function(){
59+
60+
expect(
61+
model.validate(
62+
validationResult: result,
63+
target : this,
64+
field : "testField",
65+
targetValue : "",
66+
validationData : "car",
67+
rules : {}
68+
)
69+
).toBeTrue();
70+
} );
71+
72+
it( "validates false when passed a non-object", function(){
73+
74+
expect(
75+
model.validate(
76+
validationResult: result,
77+
target : this,
78+
field : "testField",
79+
targetValue : "not an object", // not an object
80+
validationData : "car",
81+
rules : {}
82+
)
83+
).toBeFalse();
84+
expect(
85+
model.validate(
86+
validationResult: result,
87+
target : this,
88+
field : "testField",
89+
targetValue : { "name": "not an object" }, // not an object
90+
validationData : "car",
91+
rules : {}
92+
)
93+
).toBeFalse();
94+
expect(
95+
model.validate(
96+
validationResult: result,
97+
target : this,
98+
field : "testField",
99+
targetValue : [ 1, 2, 3 ], // not an object
100+
validationData : "car",
101+
rules : {}
102+
)
103+
).toBeFalse();
104+
} );
105+
} );
106+
}
107+
108+
}

0 commit comments

Comments
 (0)