Skip to content

Commit d9c290f

Browse files
committed
fixed required if and unless to use struct literals
1 parent fa90613 commit d9c290f

File tree

7 files changed

+161
-116
lines changed

7 files changed

+161
-116
lines changed

box.json

Lines changed: 1 addition & 1 deletion
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.1.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",

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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+
37
## 2.0.0
48

59
### Features

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

readme.md

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,69 @@ Leverage CommandBox to install:
2525

2626
`box install cbvalidation`
2727

28-
The module will register several objects into WireBox using the `@cbvalidation` namespace. The validation manager is registered as `ValidationManager@cbvalidation`. It will also register several helper methods that can be used throughout the ColdBox application.
28+
The module will register several objects into WireBox using the `@cbvalidation` namespace. The validation manager is registered as `ValidationManager@cbvalidation`. It will also register several helper methods that can be used throughout the ColdBox application: `validate(), validateOrFail(), getValidationManager()`
29+
30+
## Mixins
31+
32+
The module will also register several methods in your handlers/interceptors/layouts/views
33+
34+
```js
35+
/**
36+
* Validate an object or structure according to the constraints rules.
37+
*
38+
* @target An object or structure to validate
39+
* @fields The fields to validate on the target. By default, it validates on all fields
40+
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
41+
* @locale The i18n locale to use for validation messages
42+
* @excludeFields The fields to exclude from the validation
43+
* @includeFields The fields to include in the validation
44+
*
45+
* @return cbvalidation.model.result.IValidationResult
46+
*/
47+
function validate()
48+
49+
/**
50+
* Validate an object or structure according to the constraints rules and throw an exception if the validation fails.
51+
* The validation errors will be contained in the `extendedInfo` of the exception in JSON format
52+
*
53+
* @target An object or structure to validate
54+
* @fields The fields to validate on the target. By default, it validates on all fields
55+
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
56+
* @locale The i18n locale to use for validation messages
57+
* @excludeFields The fields to exclude from the validation
58+
* @includeFields The fields to include in the validation
59+
*
60+
* @return The validated object or the structure fields that where validated
61+
* @throws ValidationException
62+
*/
63+
function validateOrFail()
64+
65+
/**
66+
* Retrieve the application's configured Validation Manager
67+
*/
68+
function getValidationManager()
69+
```
70+
71+
## Settings
72+
73+
Here are the module settings you can place in your `ColdBox.cfc` by using the `validation` settings structure:
74+
75+
```js
76+
validation = {
77+
// The third-party validation manager to use, by default it uses CBValidation.
78+
manager = "class path",
79+
80+
// You can store global constraint rules here with unique names
81+
sharedConstraints = {
82+
name = {
83+
field = { constraints here }
84+
}
85+
}
86+
87+
}
88+
```
89+
90+
You can read more about ColdBox Validation here: - https://coldbox-validation.ortusbooks.com/
2991

3092
## Constraints
3193

@@ -68,10 +130,14 @@ this.constraints = {
68130
required : boolean [false],
69131

70132
// The field under validation must be present and not empty if the `anotherfield` field is equal to the passed `value`.
71-
requiredIf : anotherfield:value,anotherfield:value,...
133+
requiredIf : {
134+
anotherfield:value, anotherfield:value
135+
}
72136

73137
// The field under validation must be present and not empty unless the `anotherfield` field is equal to the passed
74-
requiredUnless : anotherfield:value,anotherfield:value,...
138+
requiredUnless : {
139+
anotherfield:value, anotherfield:value
140+
}
75141

76142
// same as but with no case
77143
sameAsNoCase : propertyName
@@ -95,70 +161,6 @@ this.constraints = {
95161
}
96162
```
97163

98-
## Mixins
99-
100-
The module will also register several methods in your handlers/interceptors/layouts/views
101-
102-
```js
103-
/**
104-
* Validate an object or structure according to the constraints rules.
105-
*
106-
* @target An object or structure to validate
107-
* @fields The fields to validate on the target. By default, it validates on all fields
108-
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
109-
* @locale The i18n locale to use for validation messages
110-
* @excludeFields The fields to exclude from the validation
111-
* @includeFields The fields to include in the validation
112-
*
113-
* @return cbvalidation.model.result.IValidationResult
114-
*/
115-
function validate()
116-
117-
/**
118-
* Validate an object or structure according to the constraints rules and throw an exception if the validation fails.
119-
* The validation errors will be contained in the `extendedInfo` of the exception in JSON format
120-
*
121-
* @target An object or structure to validate
122-
* @fields The fields to validate on the target. By default, it validates on all fields
123-
* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation
124-
* @locale The i18n locale to use for validation messages
125-
* @excludeFields The fields to exclude from the validation
126-
* @includeFields The fields to include in the validation
127-
*
128-
* @return The validated object or the structure fields that where validated
129-
* @throws ValidationException
130-
*/
131-
function validateOrFail()
132-
133-
/**
134-
* Retrieve the application's configured Validation Manager
135-
*/
136-
function getValidationManager()
137-
```
138-
139-
## Settings
140-
141-
Here are the module settings you can place in your `ColdBox.cfc` by using the `validation` settings structure:
142-
143-
```js
144-
validation = {
145-
// The third-party validation manager to use, by default it uses CBValidation.
146-
manager = "class path",
147-
148-
// You can store global constraint rules here with unique names
149-
sharedConstraints = {
150-
name = {
151-
field = { constraints here }
152-
}
153-
}
154-
155-
}
156-
```
157-
158-
You can read more about ColdBox Validation here: - https://coldbox-validation.ortusbooks.com/
159-
160-
---
161-
162164
```
163165
********************************************************************************
164166
Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp

test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,35 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
2929
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();
3030

3131
expect(
32-
model.validate( result, mock, "testField", "", "name:luis" )
32+
model.validate( result, mock, "testField", "", {
33+
name : "luis"
34+
} )
3335
).toBeFalse();
3436

3537
expect(
36-
model.validate( result, mock, "testField", "", "name:luis,role:admin" )
38+
model.validate( result, mock, "testField", "", {
39+
name : "luis",
40+
role : "admin"
41+
} )
3742
).toBeFalse();
3843
expect(
39-
model.validate( result, mock, "testField", "test", "name:luis,role:admin" )
44+
model.validate( result, mock, "testField", "test", {
45+
name : "luis",
46+
role : "admin"
47+
} )
4048
).toBeTrue();
4149

4250
expect(
43-
model.validate( result, mock, "testField", "shouldPass", "name:luis,role:admin" )
51+
model.validate( result, mock, "testField", "shouldPass", {
52+
name : "luis",
53+
role : "admin"
54+
} )
4455
).toBeTrue();
4556

4657
expect(
47-
model.validate( result, mock, "testField", "", "name:luis" )
58+
model.validate( result, mock, "testField", "", {
59+
name : "luis"
60+
} )
4861
).toBeFalse();
4962

5063

0 commit comments

Comments
 (0)