Skip to content

Commit 9786f76

Browse files
authored
Merge pull request #21 from elpete/custom_validators_by_key_with_arbitrary_data
Allow custom validators to be specified just by key and the payload to be passed in as validation data
2 parents 4a12fb8 + 4fcfe4f commit 9786f76

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

modules/cbvalidation/models/ValidationManager.cfc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
140140
// process the incoming rules
141141
for( var key in arguments.rules ){
142142
// if message validators, just ignore
143-
if( reFindNoCase( "^(#replace( variables.validValidators, ",", "|", "all" )#)Message$", key ) ){ continue; }
144-
// if not in list, ignore
145-
if( !listFindNoCase( variables.validValidators, key ) ){ continue; }
143+
if( reFindNoCase( "Message$", key ) ){ continue; }
146144

147145
// had to use nasty evaluate until adobe cf get's their act together on invoke.
148146
getValidator( validatorType=key, validationData=arguments.rules[ key ] )
@@ -181,6 +179,7 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
181179
return wirebox.getInstance( arguments.validationData );
182180
}
183181
default : {
182+
if ( wirebox.getBinder().mappingExists( validatorType ) ) { return wirebox.getInstance( validatorType ); }
184183
throw(message="The validator you requested #arguments.validatorType# is not a valid validator",type="ValidationManager.InvalidValidatorType");
185184
}
186185
}

tests/specs/ValidationManagerTest.cfc

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
3333

3434
}
3535

36-
function testProcessRulesIgnoresInvalidValidators(){
37-
results = getMockBox().createMock( "cbvalidation.models.result.ValidationResult" ).init();
36+
function testIgnoresAllKeysEndingInMessage(){
37+
var results = getMockBox()
38+
.createMock( "cbvalidation.models.result.ValidationResult" )
39+
.init();
3840

39-
mockRule = {
41+
var mockRule = {
4042
required = true,
4143
testMessage="Hello",
4244
uniqueMessage="Not Unique Man",
@@ -47,7 +49,41 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
4749
model.processRules( results=results, rules=mockRule, target=mock, field="name" );
4850

4951
assertEquals( 0, results.getErrorCount() );
52+
}
53+
54+
function testProcessRulesLooksForWireBoxMappingOfKeyIfNotAValidValidator() {
55+
var results = getMockBox()
56+
.createMock( "cbvalidation.models.result.ValidationResult" )
57+
.init();
58+
59+
var customValidatorMock = getMockBox()
60+
.createStub( implements = "cbvalidation.models.validators.IValidator" );
61+
customValidatorMock.$( "validate", true );
62+
63+
var mockBinder = getMockBox()
64+
.createMock( "coldbox.system.ioc.config.Binder" );
65+
mockBinder.$( "mappingExists" )
66+
.$args( "customValidator" )
67+
.$results( true );
68+
69+
mockWireBox.$( "getBinder", mockBinder )
70+
mockWireBox
71+
.$( "getInstance" )
72+
.$args( "customValidator" )
73+
.$results( customValidatorMock );
74+
75+
var mockRule = {
76+
customValidator = {
77+
customField = "hi"
78+
}
79+
};
80+
81+
var mock = createStub().$( "getName","luis" );
82+
model.processRules( results=results, rules=mockRule, target=mock, field="name" );
5083

84+
assertTrue( customValidatorMock.$once( "validate" ), "[validate] should have been called on [customValidator]" );
85+
var args = customValidatorMock.$callLog().validate[ 1 ];
86+
assertEquals( args.validationData, { customField = "hi" }, "validationData was not passed through correctly to [customValidator]" );
5187
}
5288

5389
function testGetConstraints(){

0 commit comments

Comments
 (0)