Skip to content

Commit 96b3a98

Browse files
committed
Accelerated validation by removing type checks. ACF chokes on interface checks
1 parent e3fd23e commit 96b3a98

24 files changed

+206
-141
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* New Validator: `Alpha` - Only allows alphabetic characters
1010
* New Validator: `RequiredUnless` with validation data: `anotherField:value,...` - The field under validation must be present and not empty unless the `anotherfield` field is equal to the passed `value`.
1111
* New Validator: `RequiredIf` with validation data: `anotherField:value,...` - The field under validation must be present and not empty if the `anotherfield` field is equal to the passed `value`.
12+
* Accelerated validation by removing type checks. ACF chokes on interface checks
1213

1314
### Improvements
1415

models/IValidationManager.cfc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ interface {
1818
* @excludeFieldsAn optional list of fields to exclude from the validation.
1919
* @includeFieldsAn optional list of fields to include in the validation.
2020
*
21-
* @result ValidationResult object
21+
* @return IValidationResult
2222
*/
23-
IValidationResult function validate(
23+
any function validate(
2424
required any target,
2525
string fields,
2626
any constraints,
@@ -47,15 +47,19 @@ interface {
4747
* Set the shared constraints into the validation manager, usually these are described in the ColdBox configuraiton file
4848
*
4949
* @constraintsFilter by name or not
50+
*
51+
* @return IValidationManager
5052
*/
51-
IValidationManager function setSharedConstraints( struct constraints );
53+
any function setSharedConstraints( struct constraints );
5254

5355
/**
5456
* Store a shared constraint
5557
*
5658
* @nameFilter by name or not
5759
* @constraintThe constraint to store.
60+
*
61+
* @return IValidationManager
5862
*/
59-
IValidationManager function addSharedConstraint( required string name, required struct constraint );
63+
any function addSharedConstraint( required string name, required struct constraint );
6064

6165
}

models/ValidationManager.cfc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
import cbvalidation.models.*;
5454
import cbvalidation.models.result.*;
55-
component accessors="true" serialize="false" implements="IValidationManager" singleton {
55+
component accessors="true" serialize="false" singleton {
5656

5757
/**
5858
* WireBox Object Factory
@@ -118,9 +118,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
118118
* @excludeFields An optional list of fields to exclude from the validation.
119119
* @IncludeFields An optional list of fields to include in the validation.
120120
*
121-
* @return Results object
121+
* @return IValidationResult
122122
*/
123-
IValidationResult function validate(
123+
any function validate(
124124
required any target,
125125
string fields = "*",
126126
any constraints = "",
@@ -231,12 +231,13 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
231231
* Process validation rules on a target object and field
232232
*
233233
* @results The validation result object
234+
* @results_generic cbvalidation.models.result.IValidationResult
234235
* @rules The structure containing validation rules
235236
* @target The target object to do validation on
236237
* @field The field to validate
237238
*/
238239
ValidationManager function processRules(
239-
required cbvalidation.models.result.IValidationResult results,
240+
required any results,
240241
required struct rules,
241242
required any target,
242243
required any field
@@ -249,13 +250,15 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
249250
}
250251

251252
// had to use nasty evaluate until adobe cf get's their act together on invoke.
252-
getValidator( validatorType = key, validationData = arguments.rules[ key ] ).validate(
253-
validationResult = results,
254-
target = arguments.target,
255-
field = arguments.field,
256-
targetValue = invoke( arguments.target, "get" & arguments.field ),
257-
validationData = arguments.rules[ key ]
258-
);
253+
getValidator( validatorType = key, validationData = arguments.rules[ key ] )
254+
.validate(
255+
validationResult = results,
256+
target = arguments.target,
257+
field = arguments.field,
258+
targetValue = invoke( arguments.target, "get" & arguments.field ),
259+
validationData = arguments.rules[ key ],
260+
rules = arguments.rules
261+
);
259262
}
260263
return this;
261264
}
@@ -267,8 +270,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
267270
* @validationData The validation data that is used for custom validators
268271
*
269272
* @throws ValidationManager.InvalidValidatorType
273+
* @return cbvalidation.models.validators.IValidator
270274
*/
271-
cbvalidation.models.validators.IValidator function getValidator(
275+
any function getValidator(
272276
required string validatorType,
273277
required any validationData
274278
){
@@ -324,8 +328,10 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
324328
* Set the entire shared constraints structure
325329
*
326330
* @constraints Filter by name or not
331+
*
332+
* @return IValidationManager
327333
*/
328-
IValidationManager function setSharedConstraints( struct constraints ){
334+
any function setSharedConstraints( struct constraints ){
329335
variables.sharedConstraints = arguments.constraints;
330336
return this;
331337
}
@@ -335,8 +341,10 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
335341
*
336342
* @name The name to store the constraint as
337343
* @constraint The constraint structures to store.
344+
*
345+
* @return IValidationManager
338346
*/
339-
IValidationManager function addSharedConstraint( required string name, required struct constraint ){
347+
any function addSharedConstraint( required string name, required struct constraint ){
340348
variables.sharedConstraints[ arguments.name ] = arguments.constraints;
341349
return this;
342350
}

models/result/IValidationResult.cfc

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,86 @@ import cbvalidation.models.result.*;
88
interface{
99

1010
/**
11-
* Add errors into the result object
12-
* @errorThe validation error to add into the results object
13-
*/
14-
IValidationResult function addError(required IValidationError error);
11+
* Add errors into the result object
12+
* @error The validation error to add into the results object
13+
* @error_generic IValidationError
14+
*
15+
* @return IValidationResult
16+
*/
17+
any function addError(required error);
1518

1619
/**
17-
* Set the validation target object name
18-
*/
19-
IValidationResult function setTargetName(required string name);
20+
* Set the validation target object name
21+
* @return IValidationResult
22+
*/
23+
any function setTargetName(required string name);
2024

2125
/**
22-
* Get the name of the target object that got validated
23-
*/
26+
* Get the name of the target object that got validated
27+
*/
2428
string function getTargetName();
2529

2630
/**
27-
* Get the validation locale
28-
*/
31+
* Get the validation locale
32+
*/
2933
string function getValidationLocale();
3034

3135
/**
32-
* has locale information
33-
*/
36+
* has locale information
37+
*/
3438
boolean function hasLocale();
3539

3640
/**
37-
* Set the validation locale
38-
*/
39-
IValidationResult function setLocale(required string locale);
41+
* Set the validation locale
42+
*
43+
* @return IValidationResult
44+
*/
45+
any function setLocale(required string locale);
4046

4147

4248
/**
43-
* Determine if the results had error or not
44-
* @fieldThe field to count on (optional)
45-
*/
49+
* Determine if the results had error or not
50+
* @fieldThe field to count on (optional)
51+
*/
4652
boolean function hasErrors(string field);
4753

4854
/**
49-
* Clear All errors
50-
*/
51-
IValidationResult function clearErrors();
55+
* Clear All errors
56+
* @return IValidationResult
57+
*/
58+
any function clearErrors();
5259

5360

5461
/**
55-
* Get how many errors you have
56-
* @fieldThe field to count on (optional)
57-
*/
62+
* Get how many errors you have
63+
* @fieldThe field to count on (optional)
64+
*/
5865
numeric function getErrorCount(string field);
5966

6067
/**
61-
* Get the Errors Array, which is an array of error messages (strings)
62-
* @fieldThe field to use to filter the error messages on (optional)
63-
*/
68+
* Get the Errors Array, which is an array of error messages (strings)
69+
* @fieldThe field to use to filter the error messages on (optional)
70+
*/
6471
array function getAllErrors(string field);
6572

6673
/**
67-
* Get an error object for a specific field that failed. Throws exception if the field does not exist
68-
* @fieldThe field to return error objects on
69-
*/
70-
IValidationError[] function getFieldErrors(required string field);
74+
* Get an error object for a specific field that failed. Throws exception if the field does not exist
75+
* @fieldThe field to return error objects on
76+
*
77+
* @return IValidationError[]
78+
*/
79+
array function getFieldErrors(required string field);
7180

7281
/**
73-
* Get a collection of metadata about the validation results
74-
*/
82+
* Get a collection of metadata about the validation results
83+
*/
7584
struct function getResultMetadata();
7685

7786
/**
78-
* Set a collection of metadata into the results object
79-
*/
80-
IValidationResult function setResultMetadata(required struct data);
87+
* Set a collection of metadata into the results object
88+
*
89+
* @return IValidationResult
90+
*/
91+
any function setResultMetadata(required struct data);
8192

8293
}

models/result/ValidationError.cfc

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ---
55
* The ColdBox validation error, all inspired by awesome Hyrule Validation Framework by Dan Vega
66
*/
7-
component accessors="true" implements="cbvalidation.models.result.IValidationError" {
7+
component accessors="true" {
88

99
// constructor
1010
ValidationError function init(){
@@ -20,8 +20,10 @@ component accessors="true" implements="cbvalidation.models.result.IValidationErr
2020
/**
2121
* Set error metadata that can be used in i18n message replacements or in views
2222
* @data The name-value pairs of data to store in this error.
23+
*
24+
* @return IValidationError
2325
*/
24-
IValidationError function setErrorMetadata( required any data ){
26+
any function setErrorMetadata( required any data ){
2527
errorMetadata = arguments.data;
2628
return this;
2729
}
@@ -36,8 +38,10 @@ component accessors="true" implements="cbvalidation.models.result.IValidationErr
3638
/**
3739
* Set the validator data
3840
* @data The data of the validator
41+
*
42+
* @return IValidationError
3943
*/
40-
IValidationError function setValidationData( required any data ){
44+
any function setValidationData( required any data ){
4145
validationData = arguments.data;
4246
return this;
4347
}
@@ -53,35 +57,43 @@ component accessors="true" implements="cbvalidation.models.result.IValidationErr
5357
/**
5458
* Set the error message
5559
* @message The error message
60+
*
61+
* @return IValidationError
5662
*/
57-
IValidationError function setMessage( required string message ){
63+
any function setMessage( required string message ){
5864
variables.message = arguments.message;
5965
return this;
6066
}
6167

6268
/**
6369
* Set the field
6470
* @message The error message
71+
*
72+
* @return IValidationError
6573
*/
66-
IValidationError function setField( required string field ){
74+
any function setField( required string field ){
6775
variables.field = arguments.field;
6876
return this;
6977
}
7078

7179
/**
7280
* Set the rejected value
7381
* @value The rejected value
82+
*
83+
* @return IValidationError
7484
*/
75-
IValidationError function setRejectedValue( required any value ){
85+
any function setRejectedValue( required any value ){
7686
variables.rejectedValue = arguments.value;
7787
return this;
7888
}
7989

8090
/**
8191
* Set the validator type name that rejected
8292
* @validationType The name of the rejected validator
93+
*
94+
* @return IValidationError
8395
*/
84-
IValidationError function setValidationType( required any validationType ){
96+
any function setValidationType( required any validationType ){
8597
variables.validationType = arguments.validationType;
8698
return this;
8799
}
@@ -135,8 +147,10 @@ component accessors="true" implements="cbvalidation.models.result.IValidationErr
135147
* @field The required field that case the exception
136148
* @rejectedValue The optional rejected value
137149
* @validationType The name of the rejected validator
150+
*
151+
* @return IValidationError
138152
*/
139-
IValidationError function configure(
153+
any function configure(
140154
required string message,
141155
required string field,
142156
string rejectedValue,

0 commit comments

Comments
 (0)