Skip to content

Beautiful Open-Source Form Builder

Refactor FormPropertiesRule and related validators for improved performance and clarity (#1008)

Chirag Chhatralabd58e5967c6e
diff --git a/api/app/Rules/FormPropertiesRule.php b/api/app/Rules/FormPropertiesRule.php
index e73ed8dbb..5f9d0559c 100644
--- a/api/app/Rules/FormPropertiesRule.php
+++ b/api/app/Rules/FormPropertiesRule.php
@@ -9,7 +9,6 @@ use App\Rules\PropertyValidators\PaymentPropertyValidator;
use App\Rules\PropertyValidators\PropertyValidatorInterface;
use App\Rules\PropertyValidators\TypePropertyValidator;
use Closure;
-use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Validation\Validator;
@@ -22,7 +21,7 @@ use Illuminate\Validation\Validator;
* This dramatically improves performance for forms with many properties
* by avoiding Laravel's validation framework overhead per property.
*/
-class FormPropertiesRule implements ValidationRule, ValidatorAwareRule, DataAwareRule
+class FormPropertiesRule implements ValidationRule, ValidatorAwareRule
{
/**
* @var PropertyValidatorInterface[]
@@ -33,8 +32,6 @@ class FormPropertiesRule implements ValidationRule, ValidatorAwareRule, DataAwar
private ?Validator $validator = null;
- private array $data = [];
-
public function __construct(?Workspace $workspace = null)
{
$this->workspace = $workspace;
@@ -57,15 +54,6 @@ class FormPropertiesRule implements ValidationRule, ValidatorAwareRule, DataAwar
return $this;
}
- /**
- * Set the data under validation.
- */
- public function setData(array $data): static
- {
- $this->data = $data;
- return $this;
- }
-
/**
* Run the validation rule.
*
diff --git a/api/app/Rules/PropertyValidators/LogicPropertyValidator.php b/api/app/Rules/PropertyValidators/LogicPropertyValidator.php
index decc6a5c7..0062b9fb8 100644
--- a/api/app/Rules/PropertyValidators/LogicPropertyValidator.php
+++ b/api/app/Rules/PropertyValidators/LogicPropertyValidator.php
@@ -51,12 +51,6 @@ class LogicPropertyValidator implements PropertyValidatorInterface
return $errors;
}
- // Validate logic is an array (nullable)
- if (!is_array($logic)) {
- $errors['logic'] = 'The logic field must be an array.';
- return $errors;
- }
-
// If no conditions, logic is valid (empty logic)
if (!isset($logic['conditions'])) {
return $errors;
diff --git a/api/app/Rules/PropertyValidators/PaymentPropertyValidator.php b/api/app/Rules/PropertyValidators/PaymentPropertyValidator.php
index c625bebbd..c03ea17b3 100644
--- a/api/app/Rules/PropertyValidators/PaymentPropertyValidator.php
+++ b/api/app/Rules/PropertyValidators/PaymentPropertyValidator.php
@@ -56,7 +56,7 @@ class PaymentPropertyValidator implements PropertyValidatorInterface
// Amount validation
if (!isset($property['amount']) || !is_numeric($property['amount']) || $property['amount'] < 1) {
- $errors['amount'] = 'Amount must be a number greater than 1';
+ $errors['amount'] = 'Amount must be a number of at least 1';
return $errors;
}
diff --git a/api/tests/Unit/Rules/FormPropertiesRuleTest.php b/api/tests/Unit/Rules/FormPropertiesRuleTest.php
new file mode 100644
index 000000000..2403366cf
--- /dev/null
+++ b/api/tests/Unit/Rules/FormPropertiesRuleTest.php
@@ -0,0 +1,663 @@
+<?php
+
+use App\Rules\FormPropertiesRule;
+use Tests\TestCase;
+
+uses(TestCase::class);
+
+describe('FormPropertiesRule', function () {
+ describe('basic validation', function () {
+ it('passes with valid properties', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ ],
+ [
+ 'id' => 'email',
+ 'name' => 'Email',
+ 'type' => 'email',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('fails when properties is not an array', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => 'not-an-array',
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ });
+
+ it('fails when a property is not an array', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ 'not-an-array',
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0'))->toBeTrue();
+ });
+
+ it('handles empty properties array per Laravel required rule', function () {
+ // Note: Laravel's 'required' rule considers empty arrays as failing validation
+ // This is expected behavior - forms should have at least one property
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ // Empty array fails 'required' validation in Laravel
+ expect($validator->passes())->toBeFalse();
+ });
+ });
+
+ describe('core property validation', function () {
+ it('fails when id is missing', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'name' => 'Title',
+ 'type' => 'text',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.id'))->toBeTrue();
+ });
+
+ it('fails when name is missing', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'type' => 'text',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.name'))->toBeTrue();
+ });
+
+ it('fails when type is missing', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.type'))->toBeTrue();
+ });
+
+ it('collects multiple missing field errors', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ // Missing all required fields
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.id'))->toBeTrue();
+ expect($validator->errors()->has('properties.0.name'))->toBeTrue();
+ expect($validator->errors()->has('properties.0.type'))->toBeTrue();
+ });
+
+ it('validates width enum values', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'width' => 'invalid-width',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.width'))->toBeTrue();
+ });
+
+ it('passes with valid width value', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'width' => '1/2',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('validates help_position enum values', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'help_position' => 'invalid',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.help_position'))->toBeTrue();
+ });
+
+ it('validates boolean fields accept valid boolean-like values', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'hidden' => true,
+ 'required' => false,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+ });
+
+ describe('image validation', function () {
+ it('validates image URL format', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'image' => [
+ 'url' => 'not-a-valid-url',
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.image.url'))->toBeTrue();
+ });
+
+ it('passes with valid image URL', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'image' => [
+ 'url' => 'https://example.com/image.png',
+ 'alt' => 'Example image',
+ 'layout' => 'between',
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('validates image layout enum', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'image' => [
+ 'layout' => 'invalid-layout',
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.image.layout'))->toBeTrue();
+ });
+
+ it('validates focal point values are between 0 and 100', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'image' => [
+ 'focal_point' => [
+ 'x' => 150,
+ 'y' => 50,
+ ],
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.image.focal_point.x'))->toBeTrue();
+ });
+ });
+
+ describe('type-specific validation', function () {
+ it('validates text field max_char_limit must be integer', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'text',
+ 'name' => 'Text Field',
+ 'type' => 'text',
+ 'max_char_limit' => 'not-an-integer',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.max_char_limit'))->toBeTrue();
+ });
+
+ it('validates text field max_char_limit minimum value', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'text',
+ 'name' => 'Text Field',
+ 'type' => 'text',
+ 'max_char_limit' => 0,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.max_char_limit'))->toBeTrue();
+ });
+
+ it('passes with valid text field configuration', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'text',
+ 'name' => 'Text Field',
+ 'type' => 'text',
+ 'max_char_limit' => 100,
+ 'multi_lines' => true,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('validates date field boolean options', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'date',
+ 'name' => 'Date Field',
+ 'type' => 'date',
+ 'with_time' => true,
+ 'date_range' => false,
+ 'prefill_today' => true,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('validates select field min_selection must be at least 0', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'select',
+ 'name' => 'Select Field',
+ 'type' => 'select',
+ 'min_selection' => -1,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.min_selection'))->toBeTrue();
+ });
+
+ it('validates files max_file_size must be at least 1', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'files',
+ 'name' => 'Files Field',
+ 'type' => 'files',
+ 'max_file_size' => 0.5,
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.max_file_size'))->toBeTrue();
+ });
+ });
+
+ describe('layout blocks', function () {
+ it('passes for layout blocks with minimal fields', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'text-block',
+ 'name' => 'Text Block',
+ 'type' => 'nf-text',
+ ],
+ [
+ 'id' => 'divider',
+ 'name' => 'Divider',
+ 'type' => 'nf-divider',
+ ],
+ [
+ 'id' => 'page-break',
+ 'name' => 'Page Break',
+ 'type' => 'nf-page-break',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+ });
+
+ describe('multiple properties validation', function () {
+ it('validates all properties and collects errors from each', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ // Missing name
+ 'type' => 'text',
+ ],
+ [
+ 'id' => 'email',
+ 'name' => 'Email',
+ // Missing type
+ ],
+ [
+ 'id' => 'text',
+ 'name' => 'Text',
+ 'type' => 'text',
+ 'width' => 'invalid',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.name'))->toBeTrue();
+ expect($validator->errors()->has('properties.1.type'))->toBeTrue();
+ expect($validator->errors()->has('properties.2.width'))->toBeTrue();
+ });
+
+ it('passes with multiple valid properties of different types', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ ],
+ [
+ 'id' => 'text',
+ 'name' => 'Description',
+ 'type' => 'text',
+ 'multi_lines' => true,
+ 'max_char_limit' => 500,
+ ],
+ [
+ 'id' => 'date',
+ 'name' => 'Due Date',
+ 'type' => 'date',
+ 'with_time' => true,
+ ],
+ [
+ 'id' => 'select',
+ 'name' => 'Priority',
+ 'type' => 'select',
+ 'allow_creation' => false,
+ ],
+ [
+ 'id' => 'divider',
+ 'name' => 'Divider',
+ 'type' => 'nf-divider',
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+ });
+
+ describe('logic validation integration', function () {
+ it('validates logic conditions through the rule', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'title',
+ 'name' => 'Title',
+ 'type' => 'text',
+ 'hidden' => false,
+ 'required' => false,
+ 'logic' => [
+ 'conditions' => [
+ 'operatorIdentifier' => 'and',
+ 'children' => [
+ [
+ 'identifier' => 'title',
+ 'value' => [
+ 'operator' => 'equals',
+ 'property_meta' => [
+ 'id' => 'title',
+ 'type' => 'text',
+ ],
+ 'value' => 'TEST',
+ ],
+ ],
+ ],
+ ],
+ 'actions' => ['hide-block'],
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeTrue();
+ });
+
+ it('fails with invalid logic actions', function () {
+ $rules = [
+ 'properties' => ['required', 'array', new FormPropertiesRule()],
+ ];
+
+ $data = [
+ 'properties' => [
+ [
+ 'id' => 'text',
+ 'name' => 'Custom Text',
+ 'type' => 'nf-text',
+ 'logic' => [
+ 'conditions' => [
+ 'operatorIdentifier' => 'and',
+ 'children' => [
+ [
+ 'identifier' => 'title',
+ 'value' => [
+ 'operator' => 'equals',
+ 'property_meta' => [
+ 'id' => 'title',
+ 'type' => 'text',
+ ],
+ 'value' => 'TEST',
+ ],
+ ],
+ ],
+ ],
+ 'actions' => ['require-answer'], // Invalid for layout blocks
+ ],
+ ],
+ ],
+ ];
+
+ $validator = $this->app['validator']->make($data, $rules);
+ expect($validator->passes())->toBeFalse();
+ expect($validator->errors()->has('properties.0.logic'))->toBeTrue();
+ });
+ });
+});