Skip to content

Commit bcc70e9

Browse files
committed
* New ColdBox 7 delegate: Validatable@cbValidation which can be used to make objects validatable
1 parent 320dac3 commit bcc70e9

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

changelog.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
----
99

10+
## [4.1.0] => 2022-NOV-14
11+
12+
### Added
13+
14+
* New ColdBox 7 delegate: `Validatable@cbValidation` which can be used to make objects validatable
15+
* New validators: `notSameAs, notSameAsNoCase`
16+
17+
----
18+
1019
## [4.0.0] => 2022-OCT-10
1120

1221
### Added

models/delegates/Validatable.cfc

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* Copyright since 2016 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This delegate helps models validate themselves
6+
*/
7+
component accessors="true" {
8+
9+
// DI
10+
property name="validationManager" inject="ValidationManager@cbvalidation";
11+
12+
// Properties
13+
property name="validationResults";
14+
15+
/**
16+
* Validate the $parent and stores a validation result in the delegate
17+
*
18+
* @fields One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
19+
* @constraints An optional shared constraints name or an actual structure of constraints to validate on.
20+
* @locale An optional locale to use for i18n messages
21+
* @excludeFields An optional list of fields to exclude from the validation.
22+
* @IncludeFields An optional list of fields to include in the validation.
23+
* @profiles If passed, a list of profile names to use for validation constraints
24+
*/
25+
boolean function isValid(
26+
string fields = "*",
27+
any constraints = "",
28+
string locale = "",
29+
string excludeFields = "",
30+
string includeFields = "",
31+
string profiles = ""
32+
){
33+
// validate and save results in private scope
34+
variables.validationResults = validate( argumentCollection = arguments );
35+
36+
// return it
37+
return ( !variables.validationResults.hasErrors() );
38+
}
39+
40+
/**
41+
* Get the validation results object. This will be an empty validation object if isValid() has not being called yet.
42+
*
43+
* @return cbvalidation.models.result.IValidationResult
44+
*/
45+
any function getValidationResults(){
46+
if ( !isNull( variables.validationResults ) && isObject( variables.validationResults ) ) {
47+
return variables.validationResults;
48+
}
49+
return new cbvalidation.models.result.ValidationResult();
50+
}
51+
52+
/**
53+
* Validate the parent delegate
54+
*
55+
* @fields The fields to validate on the target. By default, it validates on all fields
56+
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
57+
* @locale The i18n locale to use for validation messages
58+
* @excludeFields The fields to exclude from the validation
59+
* @includeFields The fields to include in the validation
60+
* @profiles If passed, a list of profile names to use for validation constraints
61+
*
62+
* @return cbvalidation.model.result.IValidationResult
63+
*/
64+
function validate(){
65+
arguments.target = $parent;
66+
return variables.validationManager.validate( argumentCollection = arguments );
67+
}
68+
69+
/**
70+
* Validate an object or structure according to the constraints rules and throw an exception if the validation fails.
71+
* The validation errors will be contained in the `extendedInfo` of the exception in JSON format
72+
*
73+
* @fields The fields to validate on the target. By default, it validates on all fields
74+
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
75+
* @locale The i18n locale to use for validation messages
76+
* @excludeFields The fields to exclude from the validation
77+
* @includeFields The fields to include in the validation
78+
* @profiles If passed, a list of profile names to use for validation constraints
79+
*
80+
* @return The validated object or the structure fields that where validated
81+
*
82+
* @throws ValidationException
83+
*/
84+
function validateOrFail(){
85+
arguments.target = $parent;
86+
return variables.validationManager.validateOrFail( argumentCollection = arguments );
87+
}
88+
89+
/**
90+
* Verify if the target value has a value
91+
* Checks for nullness or for length if it's a simple value, array, query, struct or object.
92+
*/
93+
boolean function validateHasValue( any targetValue ){
94+
return variables.validationManager
95+
.getValidator( "Required", {} )
96+
.hasValue( argumentCollection = arguments );
97+
}
98+
99+
/**
100+
* Check if a value is null or is a simple value and it's empty
101+
*
102+
* @targetValue the value to check for nullness/emptyness
103+
*/
104+
boolean function validateIsNullOrEmpty( any targetValue ){
105+
return variables.validationManager
106+
.getValidator( "Required", {} )
107+
.hasValue( argumentCollection = arguments );
108+
}
109+
110+
/**
111+
* This method mimics the Java assert() function, where it evaluates the target to a boolean value and it must be true
112+
* to pass and return a true to you, or throw an `AssertException`
113+
*
114+
* @target The tareget to evaluate for being true
115+
* @message The message to send in the exception
116+
*
117+
* @return True, if the target is a non-null value. If false, then it will throw the `AssertError` exception
118+
*
119+
* @throws AssertException if the target is a false or null value
120+
*/
121+
boolean function assert( target, message = "" ){
122+
if ( !isNull( arguments.target ) && arguments.target ) {
123+
return true;
124+
}
125+
throw(
126+
type : "AssertException",
127+
message: len( arguments.message ) ? arguments.message : "Assertion failed from #callStackGet()[ 2 ].toString()#"
128+
);
129+
}
130+
131+
}

0 commit comments

Comments
 (0)