Skip to content

Commit

Permalink
Merge pull request #43 from SandroMiguel/fix/multi-name-field-validation
Browse files Browse the repository at this point in the history
fix(validator.php): fix validation of multi-name fields to ensure pre…
  • Loading branch information
SandroMiguel authored Mar 21, 2024
2 parents 2e249bb + 5530b9f commit f75e1f0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license MIT https://github.com/SandroMiguel/verum-php/blob/master/LICENSE
* @author Sandro Miguel Marques <[email protected]>
* @link https://github.com/SandroMiguel/verum-php
* @version 4.2.1 (2024-03-20)
* @version 4.2.2 (2024-03-21)
*/

declare(strict_types=1);
Expand Down Expand Up @@ -222,6 +222,10 @@ public function validate(): bool
\count($fieldValue) - 1
)
) {
if ($this->debugMode) {
echo "\n2.3 Field is an indexed array - Ignore";
}

continue;
}

Expand Down Expand Up @@ -457,6 +461,11 @@ private function getMultiNameFieldValues(string $fieldName): array

$multiNameFieldValues = [];

// Ensure that the field name is in the payload and validate it against null
if (\count($fieldValues) === 0) {
return [$fieldName => null];
}

foreach ($fieldValues as $fullFieldName => $value) {
if (\strpos($fullFieldName, $baseFieldName) !== 0) {
continue;
Expand Down
55 changes: 53 additions & 2 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,53 @@ public function testValidateMultiNameFieldsFullnameMatch(): void
);
}

/**
* Tests the validation when a multi-name field defined in the rules does
* not exist in the payload.
*/
public function testValidateMultiNameFieldNotInPayload(): void
{
$fieldValues = [
'age' => 'Text value',
];

$fieldRules = [
'age' => [
'label' => 'Age',
'rules' => [
RuleEnum::NUMERIC,
],
],
'multiName.*' => [
'rules' => [RuleEnum::REQUIRED],
],
];

$expected = [
'age' => [
'label' => 'Age',
'rules' => [
'numeric' => 'The "Age" field must be numeric.',
],
],
'multiName.*' => [
'label' => null,
'rules' => [
'required' => 'This field is required.',
],
],
];

$validator = new Validator($fieldValues, $fieldRules, debugMode: true);
$validator->validate();
$errors = $validator->getErrors();

$this->assertEquals(
\json_encode($expected),
\json_encode($errors)
);
}

/**
* Test validation with a payload containing numeric keys in an array field.
*/
Expand Down Expand Up @@ -905,7 +952,8 @@ public function testValidationWithNumericKeysInArrayField(): void
}

/**
* Test validation with an empty idTranslationValues field, which should fail due to its required nature.
* Test validation with an empty idTranslationValues field, which should
* fail due to its required nature.
*/
public function testValidationWithEmptyIdTranslationValuesField(): void
{
Expand Down Expand Up @@ -944,6 +992,9 @@ public function testValidationWithEmptyIdTranslationValuesField(): void
$errors = $validator->getErrors();

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

0 comments on commit f75e1f0

Please sign in to comment.