Skip to content

Commit e08cc0b

Browse files
committed
Merge branch 'development'
2 parents e3481c6 + e010706 commit e08cc0b

13 files changed

+277
-143
lines changed

box.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name":"ColdBox Validation",
33
"author":"Ortus Solutions <[email protected]>",
4-
"version":"2.0.0",
4+
"version":"2.0.1",
55
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbvalidation/@build.version@/[email protected]@.zip",
66
"slug":"cbvalidation",
77
"type":"modules",
@@ -31,9 +31,9 @@
3131
".git*"
3232
],
3333
"scripts":{
34-
"toMaster":"recipe build/toMaster.boxr",
35-
"format":"cfformat run models/**/*.cfc,ModuleConfig.cfc,tests/specs/**/*.cfc,*.cfc --overwrite",
36-
"format:check":"cfformat run models/**/*.cfc,ModuleConfig.cfc,tests/specs/**/*.cfc,*.cfc --check",
37-
"lint":"cflint models/**.cfc --text --html --json --!exitOnError --suppress"
34+
"toMaster":"recipe build/toMaster.boxr",
35+
"format":"cfformat run models/**/*.cfc,ModuleConfig.cfc,tests/specs/**/*.cfc,*.cfc --overwrite",
36+
"format:check":"cfformat run models/**/*.cfc,ModuleConfig.cfc,tests/specs/**/*.cfc,*.cfc --check",
37+
"lint":"cflint models/**.cfc --text --html --json --!exitOnError --suppress"
3838
}
3939
}

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 2.0.1
4+
5+
* Updated `RequiredUnless` and `RequiredIf` to use struct literal notation instead of the weird parsing we did.
6+
* Added the `Unique` validator thanks to @elpete!
7+
38
## 2.0.0
49

510
### Features

models/validators/AcceptedValidator.cfc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ component accessors="true" singleton {
1818

1919
/**
2020
* Will check if an incoming value validates
21-
* @validationResultThe result object of the validation
22-
* @targetThe target object to validate on
23-
* @fieldThe field on the target object to validate on
24-
* @targetValueThe target value to validate
25-
* @validationDataThe validation data the validator was created with
21+
*
22+
* @validationResult The result object of the validation
23+
* @target The target object to validate on
24+
* @field The field on the target object to validate on
25+
* @targetValue The target value to validate
26+
* @validationData The validation data the validator was created with
2627
*/
2728
boolean function validate(
2829
required any validationResult,

models/validators/RequiredIfValidator.cfc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Copyright since 2020 by Ortus Solutions, Corp
33
* www.ortussolutions.com
44
* ---
5-
* This validator checks if a field has value and not null
5+
* This validator checks a struct of key-value pairs passed in the validation data.
6+
* If those key-value pairs are equal then the target field will be required
67
*/
78
component accessors="true" extends="RequiredValidator" singleton {
89

@@ -18,11 +19,12 @@ component accessors="true" extends="RequiredValidator" singleton {
1819

1920
/**
2021
* Will check if an incoming value validates
21-
* @validationResultThe result object of the validation
22-
* @targetThe target object to validate on
23-
* @fieldThe field on the target object to validate on
24-
* @targetValueThe target value to validate
25-
* @validationDataThe validation data the validator was created with
22+
*
23+
* @validationResult The result object of the validation
24+
* @target The target object to validate on
25+
* @field The field on the target object to validate on
26+
* @targetValue The target value to validate
27+
* @validationData The validation data the validator was created with
2628
*/
2729
boolean function validate(
2830
required any validationResult,
@@ -31,24 +33,25 @@ component accessors="true" extends="RequiredValidator" singleton {
3133
any targetValue,
3234
any validationData
3335
){
34-
// Validation Data Format: property:value,...
35-
var validationArray = arguments.validationData.listToArray();
36+
// If you passed in simple data, conver it to a struct, simple values are not evaluated
37+
if( isSimpleValue( arguments.validationData ) ){
38+
arguments.validationData = {};
39+
}
40+
3641
// Inflate to array to test multiple properties
37-
var isRequired = validationArray
38-
.map( function( item ){
42+
var isRequired = arguments.validationData
43+
.map( function( key, value ){
3944
// Get comparison values
40-
var compareProperty = getToken( arguments.item, 1, ":" );
41-
var compareValue = getToken( arguments.item, 2, ":" );
42-
var comparePropertyValue = invoke( target, "get#compareProperty#" );
45+
var comparePropertyValue = invoke( target, "get#key#" );
4346
// Check if the compareValue is the same as the defined one
44-
return ( compareValue == comparePropertyValue ? true : false );
47+
return ( arguments.value == comparePropertyValue ? true : false );
4548
} )
4649
// AND them all for a single result
47-
.reduce( function( result, item ){
48-
return arguments.result && arguments.item;
50+
.reduce( function( result, key, value ){
51+
return ( arguments.value && arguments.result );
4952
}, true );
5053

51-
if( !validationArray.len() || !isRequired ){
54+
if( !arguments.validationData.count() || !isRequired ){
5255
return true;
5356
}
5457

models/validators/RequiredUnlessValidator.cfc

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Copyright since 2020 by Ortus Solutions, Corp
33
* www.ortussolutions.com
44
* ---
5-
* This validator checks if a field has value and not null
5+
* This validator checks a struct of key-value pairs passed in the validation data.
6+
* If those key-value pairs are equal then the target field will NOT be required
67
*/
78
component accessors="true" extends="RequiredValidator" singleton {
89

@@ -18,11 +19,12 @@ component accessors="true" extends="RequiredValidator" singleton {
1819

1920
/**
2021
* Will check if an incoming value validates
21-
* @validationResultThe result object of the validation
22-
* @targetThe target object to validate on
23-
* @fieldThe field on the target object to validate on
24-
* @targetValueThe target value to validate
25-
* @validationDataThe validation data the validator was created with
22+
*
23+
* @validationResult The result object of the validation
24+
* @target The target object to validate on
25+
* @field The field on the target object to validate on
26+
* @targetValue The target value to validate
27+
* @validationData The validation data the validator was created with
2628
*/
2729
boolean function validate(
2830
required any validationResult,
@@ -31,24 +33,26 @@ component accessors="true" extends="RequiredValidator" singleton {
3133
any targetValue,
3234
any validationData
3335
){
34-
// Validation Data Format: property:value,...
35-
var validationArray = arguments.validationData.listToArray();
36-
// Inflate to array to test multiple properties
37-
var isOptional = validationArray
38-
.map( function( item ){
36+
// If you passed in simple data, conver it to a struct, simple values are not evaluated
37+
if( isSimpleValue( arguments.validationData ) ){
38+
arguments.validationData = {};
39+
}
40+
41+
// Test the data
42+
var isOptional = arguments.validationData
43+
.map( function( key, value ){
3944
// Get comparison values
40-
var compareProperty = getToken( arguments.item, 1, ":" );
41-
var compareValue = getToken( arguments.item, 2, ":" );
42-
var comparePropertyValue = invoke( target, "get#compareProperty#" );
45+
var comparePropertyValue = invoke( target, "get#key#" );
4346
// Check if the compareValue is the same as the defined one
44-
return ( compareValue == comparePropertyValue ? true : false );
47+
return ( arguments.value == comparePropertyValue ? true : false );
4548
} )
4649
// AND them all for a single result
47-
.reduce( function( result, item ){
48-
return ( arguments.item && arguments.result );
50+
.reduce( function( result, key, value ){
51+
return ( arguments.value && arguments.result );
4952
}, true );
5053

51-
if( validationArray.len() && isOptional ){
54+
// If we have data, then test the optional
55+
if( arguments.validationData.count() && isOptional ){
5256
return true;
5357
}
5458

models/validators/UDFValidator.cfc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ component accessors="true" singleton {
3131
any targetValue,
3232
any validationData
3333
){
34-
// return true if no data to check, type needs a data element to be checked.
35-
if ( isNull( arguments.targetValue ) || ( isSimpleValue( arguments.targetValue ) && !len( arguments.targetValue ) ) ) {
36-
return true;
37-
}
34+
// Validate against the UDF/closure
35+
var passed = arguments.validationData(
36+
isNull( arguments.targetValue ) ? javacast( "null", "" ) : arguments.targetValue,
37+
arguments.target
38+
);
3839

39-
// Validate against the UDF/closure
40-
if ( arguments.validationData( arguments.targetValue, arguments.target ) ) {
40+
if ( passed ) {
4141
return true;
4242
}
4343

4444
var args = {
4545
message : "The '#arguments.field#' value does not validate",
4646
field : arguments.field,
4747
validationType : getName(),
48-
rejectedValue : ( isSimpleValue( arguments.targetValue ) ? arguments.targetValue : "" ),
48+
rejectedValue : !isNull( arguments.targetValue ) && isSimpleValue( arguments.targetValue ) ? arguments.targetValue : "",
4949
validationData : arguments.validationData
5050
};
5151

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright since 2020 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This validator checks the database according to validation data for field uniqueness
6+
* - table : The table name to seek
7+
* - column : The column to evaluate for uniqueness or defaults to the name of the field
8+
*/
9+
component accessors="true" singleton {
10+
11+
property name="name";
12+
13+
/**
14+
* Constructor
15+
*/
16+
UniqueValidator function init(){
17+
variables.name = "Unique";
18+
return this;
19+
}
20+
21+
/**
22+
* Will check if an incoming value validates
23+
*
24+
* @validationResult The result object of the validation
25+
* @target The target object to validate on
26+
* @field The field on the target object to validate on
27+
* @targetValue The target value to validate
28+
* @validationData The validation data the validator was created with
29+
*/
30+
boolean function validate(
31+
required any validationResult,
32+
required any target,
33+
required string field,
34+
any targetValue,
35+
any validationData
36+
){
37+
// Default the target column
38+
var targetColumn = ( isNull( arguments.validationData.column ) ? arguments.field : arguments.validationData.column );
39+
// Query it
40+
var exists = queryExecute(
41+
"SELECT 1 FROM #arguments.validationData.table# WHERE #targetColumn# = ?",
42+
[ arguments.targetValue ]
43+
).recordCount > 0;
44+
45+
if ( !exists ) {
46+
return true;
47+
}
48+
49+
validationResult.addError(
50+
validationResult.newError(
51+
argumentCollection = {
52+
message : "The #targetColumn# '#arguments.targetValue#' is already in use",
53+
field : arguments.field,
54+
validationType : getName(),
55+
validationData : arguments.validationData
56+
}
57+
)
58+
);
59+
60+
return false;
61+
}
62+
63+
/**
64+
* Get the name of the validator
65+
*/
66+
string function getName(){
67+
return "Unique";
68+
}
69+
70+
}

0 commit comments

Comments
 (0)