Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking Change] Update UDF validator to treat null/empty values as valid #80

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only issue I see here, is that if arguments.targetValue is null, then line 46 will fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow. If arguments.targetValue is null, the method will return true, so it should never get to line 46.

if (
invoke(
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking that if the value is actually null, this will fail, did you try it?

Copy link
Contributor Author

@homestar9 homestar9 Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmajano I haven't tested it in production, but I updated the UDFValidatorTest.cfc lines 30-37 to account for passing a null value and the validator returns true. Do you want me to create a temporary repo to test the validator instead of relying on Testbox?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget it, the way it was reading on my phone it was that lines 37-41 where removed.

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
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
Loading