Reputation: 75
I have a validation function that return which part is failing. For example -
public class DateValidator{
public String validateDate(startDate, endDate){
try{
LocalDate.parse(startDate, formatter);
LocalDate.parse(endDate, formatter);
} catch(DateTimeParseException e) {
return "INVALID_DATE_FORMAT";
}
if (startDate.isAfter(endDate)) {
return "INVALID_START_AND_END_DATES";
}
..... so on
}
}
I want to use the this in Drools for Validation as
rule
"ValidateDate"
when
$error: Error();
$request: Request();
DateValidator( $dateValidation: validateDate($request.getStartDate(), $request.getEndDate()) != null);
then
$error.getBadRequest($dateValidation);
end;
I want to use the return type of the variable as not null means the validation did not passed. But I am getting below exception for the DRL file -
text=Variables can not be used inside bindings.
Variable [$request] is being used in binding
'validateDate($request.getStartDate(), $request.getEndDate())']
Upvotes: 1
Views: 867
Reputation: 6322
Try to execute the function call as part of a from
Conditional Element:
rule
"ValidateDate"
when
$error: Error();
$request: Request(
$startDate: startDate,
$endDate: endDate
);
$dv: DateValidator()
$msg: String() from $dv.validateDate($startDate, $endDate)
then
$error.getBadRequest($msg);
end
Hope it helps,
Upvotes: 4