Implement regex validation conditions in form logic (#645)
* Implement regex validation conditions in form logic - Added support for 'matches_regex' and 'does_not_match_regex' conditions in FormPropertyLogicRule and FormLogicConditionChecker. - Updated validation logic to handle regex patterns, including error handling for invalid patterns. - Enhanced tests to cover scenarios for successful and failed regex validation, ensuring proper feedback for form submissions. - Updated JSON schema to include new regex condition types. These changes improve the flexibility of form validation by allowing regex-based conditions, enhancing user experience through more robust validation mechanisms. * update resource filters * Remove ray --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -71,6 +71,18 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
|
||||
'content_length_less_than_or_equal_to' => [
|
||||
'expected_type' => 'number',
|
||||
],
|
||||
'matches_regex' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'regex'
|
||||
]
|
||||
],
|
||||
'does_not_match_regex' => [
|
||||
'expected_type' => 'string',
|
||||
'format' => [
|
||||
'type' => 'regex'
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
'matrix' => [
|
||||
@@ -672,6 +684,8 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
|
||||
|
||||
private $data = [];
|
||||
|
||||
private $operator = '';
|
||||
|
||||
private function checkBaseCondition($condition)
|
||||
{
|
||||
|
||||
@@ -712,6 +726,7 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
|
||||
|
||||
$typeField = $condition['value']['property_meta']['type'];
|
||||
$operator = $condition['value']['operator'];
|
||||
$this->operator = $operator;
|
||||
$value = $condition['value']['value'];
|
||||
|
||||
if (!isset(self::CONDITION_MAPPING[$typeField])) {
|
||||
@@ -750,6 +765,19 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
|
||||
|
||||
private function valueHasCorrectType($type, $value)
|
||||
{
|
||||
if ($type === 'string' && isset(self::CONDITION_MAPPING[$this->field['type']]['comparators'][$this->operator]['format'])) {
|
||||
$format = self::CONDITION_MAPPING[$this->field['type']]['comparators'][$this->operator]['format'];
|
||||
if ($format['type'] === 'regex') {
|
||||
try {
|
||||
preg_match('/' . $value . '/', '');
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->conditionErrors[] = 'invalid regex pattern';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
($type === 'string' && gettype($value) !== 'string') ||
|
||||
($type === 'boolean' && !is_bool($value)) ||
|
||||
|
||||
Reference in New Issue
Block a user