Skip to content

Commit

Permalink
Merge pull request #81 from coldbox-modules/development
Browse files Browse the repository at this point in the history
Release v4.4
  • Loading branch information
lmajano authored Oct 16, 2023
2 parents 7dd1273 + 8791353 commit f41e0f0
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion box.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"ColdBox Validation",
"author":"Ortus Solutions <[email protected]>",
"version":"4.3.1",
"version":"4.4.0",
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbvalidation/@build.version@/[email protected]@.zip",
"slug":"cbvalidation",
"type":"modules",
Expand Down
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- requiredIf accepts a UDF and closure now

### Fixed

- UDF validator now treats nulls correctly

## [4.3.1] - 2023-06-15

### Fixed
Expand Down
5 changes: 0 additions & 5 deletions models/validators/MethodValidator.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ component extends="BaseValidator" accessors="true" singleton {
return true;
}

// return true if no data to check, type needs a data element to be checked.
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
return true;
}

// Validate via method
if (
invoke(
Expand Down
20 changes: 17 additions & 3 deletions models/validators/RequiredIfValidator.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ component
any validationData,
struct rules
){
var isRequired = true;
var isRequired = true;
var errorMetadata = {};

// If you passed in simple data, simply check that the target field has a value
if ( isSimpleValue( arguments.validationData ) && len( arguments.validationData ) ) {
Expand All @@ -60,13 +61,24 @@ component
.reduce( function( result, key, value ){
return ( arguments.value && arguments.result );
}, true );
// If passed a UDF/closure
} else if (
isCustomFunction( arguments.validationData ) ||
isClosure( arguments.validationData )
) {
// Validate against the UDF/closure
var isRequired = arguments.validationData(
isNull( arguments.targetValue ) ? javacast( "null", "" ) : arguments.targetValue,
arguments.target,
errorMetadata
);
} else {
validationResult.addError(
validationResult.newError(
message = "The target for RequiredIf must be a simple field name or a struct of field to target value pairs.",
field = arguments.field,
validationType = getName(),
rejectedValue = arguments.validationData,
rejectedValue = isSimpleValue( arguments.validationData ) ? arguments.validationData : "",
validationData = arguments.validationData
)
);
Expand Down Expand Up @@ -94,7 +106,9 @@ component
validationData : arguments.validationData
};

validationResult.addError( validationResult.newError( argumentCollection = args ) );
validationResult.addError(
validationResult.newError( argumentCollection = args ).setErrorMetadata( errorMetadata )
);
return false;
}

Expand Down
7 changes: 6 additions & 1 deletion models/validators/UDFValidator.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ component extends="BaseValidator" accessors="true" singleton {
){
var errorMetadata = {};

// return true if no data to check, type needs a data element to be checked.
if ( isNull( arguments.targetValue ) || isNullOrEmpty( arguments.targetValue ) ) {
return true;
}

// Validate against the UDF/closure
var passed = arguments.validationData(
isNull( arguments.targetValue ) ? javacast( "null", "" ) : arguments.targetValue,
arguments.targetValue,
arguments.target,
errorMetadata
);
Expand Down
3 changes: 2 additions & 1 deletion test-harness/tests/specs/ValidationIntegrations.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
params = {
username : "luis",
email : "[email protected]",
password : "luis"
password : "luis",
status : 4 // should not validate
},
method = "post"
);
Expand Down
48 changes: 39 additions & 9 deletions test-harness/tests/specs/validators/RequiredIfValidatorTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,47 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod

expect( model.validate( result, mock, "testField", "", "name" ) ).toBeFalse();

// expect(
// model.validate(
// result,
// mock,
// "testField",
// "",
// "missing"
// )
// ).toBeTrue();
expect( model.validate( result, mock, "testField", "", "missing" ) ).toBeTrue();
} );
it( "can accept a closure as validationData", function(){
var mock = createStub()
.$( "getName", "luis" )
.$( "getRole", "admin" )
.$( "getMissing", javacast( "null", "" ) );
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();

expect( model.validate( result, mock, "testField", "", isRequired1 ) ).toBeFalse();

expect( model.validate( result, mock, "testField", "", isRequired2 ) ).toBeTrue();
} );
it( "can use custom error metadata", function(){
var mock = createStub()
.$( "getName", "luis" )
.$( "getRole", "admin" )
.$( "getMissing", javacast( "null", "" ) );
var result = createMock( "cbvalidation.models.result.ValidationResult" ).init();

expect( model.validate( result, mock, "testField", "", isRequired3 ) ).toBeFalse();

var errorMetadata = result.getErrors()[ 1 ].getErrorMetadata();

expect( errorMetaData ).toHaveKey( "customMessage" );
expect( errorMetaData.customMessage ).toBe( "This is custom data" );
} );
} );
}

private function isRequired1( value, target, errorMetadata ){
return true;
}

private function isRequired2( value, target, errorMetadata ){
return false;
}

private function isRequired3( value, target, errorMetadata ){
arguments.errorMetadata[ "customMessage" ] = "This is custom data";
return true;
}

}
2 changes: 1 addition & 1 deletion test-harness/tests/specs/validators/UDFValidatorTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
javacast( "null", "" ),
variables.validate3
);
assertEquals( false, r );
assertEquals( true, r );
}

private function validate( value, target ){
Expand Down

0 comments on commit f41e0f0

Please sign in to comment.