Skip to content

Commit

Permalink
Merge pull request #41 from SandroMiguel/fix/indexed-fields-validation
Browse files Browse the repository at this point in the history
fix(validator.php): fix indexed fields validation bug
  • Loading branch information
SandroMiguel authored Mar 21, 2024
2 parents 79c65f0 + 588c9e7 commit 6f834fc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ public function validate(): bool
? $this->getMultiNameFieldValues($fieldName)
: $this->fieldValues[$fieldName] ?? null;

// Check if the field is an indexed array
if (
\is_array($fieldValue)
&& \array_keys($fieldValue)
=== \range(
0,
\count($fieldValue) - 1
)
) {
continue;
}

$fieldValues = \is_array($fieldValue)
? $fieldValue
: [$fieldName => $fieldValue];
Expand Down
75 changes: 75 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,79 @@ public function testValidateMultiNameFieldsFullnameMatch(): void
\json_encode($errors)
);
}

/**
* Test validation with a payload containing numeric keys in an array field.
*/
public function testValidationWithNumericKeysInArrayField(): void
{
// Given a payload with numeric keys in an array field
$payload = [
'formToken' => '123...abc',
'idTranslationValues' => [
0 => 'add_link',
1 => 'remove_link',
],
];

// Define the field rules with the required rule for the idTranslationValues field
$fieldRules = [
'idTranslationValues' => [
'rules' => [
'required' => true,
],
],
];

// Create a Validator instance with the payload and rules
$validator = new Validator($payload, $fieldRules, debugMode: true);

// Perform validation
$isValid = $validator->validate();

$this->assertTrue($isValid);
}

/**
* Test validation with an empty idTranslationValues field, which should fail due to its required nature.
*/
public function testValidationWithEmptyIdTranslationValuesField(): void
{
// Given a payload with an empty idTranslationValues field
$payload = [
'formToken' => '123...abc',
'idTranslationValues' => null,
];

// Define the field rules with the required rule for the idTranslationValues field
$fieldRules = [
'idTranslationValues' => [
'rules' => [
'required' => true,
],
],
];

// Define the expected errors for the empty idTranslationValues field
$expectedErrors = [
'idTranslationValues' => [
'label' => null,
'rules' => [
'required' => 'This field is required.',
],
],
];

// Create a Validator instance with the payload and rules
$validator = new Validator($payload, $fieldRules, debugMode: true);

// Perform validation
$validator->validate();

// Get the validation errors
$errors = $validator->getErrors();

// Assert that the validation errors match the expected errors
$this->assertEquals(json_encode($expectedErrors), json_encode($errors));
}
}

0 comments on commit 6f834fc

Please sign in to comment.