Skip to content

Commit d1e5be3

Browse files
bug #58387 [Validator][CidrValidator] Fix error message for OutOfRangeNetmask validation (Fabdouarrahmane)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [Validator][CidrValidator] Fix error message for `OutOfRangeNetmask` validation <table> <tr> <th>Q</th> <th>A</th> </tr> <tr> <td>Branch?</td> <td>7.1</td> </tr> <tr> <td>Bug fix?</td> <td>yes</td> </tr> <tr> <td>New feature?</td> <td>no</td> </tr> <tr> <td>Deprecations?</td> <td>no</td> </tr> <tr> <td>License</td> <td>MIT</td> </tr> </table> **Description** This pull request addresses an issue with the error message generated for the OutOfRangeNetmask validation in the CidrValidator. The previous implementation allowed a maximum netmask of 128, which is incorrect for IPv4 addresses. The maximum should be set to 32 instead. **Changes Made:** - Updated the error message parameters in the CidrValidator to reflect the correct maximum netmask for IPv4. - Modified the corresponding test to ensure that it accurately verifies the expected behavior. **Benefits:** This fix ensures that users receive accurate feedback when providing invalid CIDR notations, which enhances the overall reliability of the validation process. **Testing:** All existing tests have been run, and the new behavior has been verified. The tests confirm that the error message now correctly states the maximum allowed netmask for IPv4 addresses. Feel free to adjust any part of it to better fit your style or add any additional information you think is relevant! Commits ------- bc0cabb7893 [Validator][CidrValidator] Fix error message for `OutOfRangeNetmask` validation
2 parents 8383185 + ad53851 commit d1e5be3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

Constraints/CidrValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function validate($value, Constraint $constraint): void
8181
$this->context
8282
->buildViolation($constraint->netmaskRangeViolationMessage)
8383
->setParameter('{{ min }}', $constraint->netmaskMin)
84-
->setParameter('{{ max }}', $constraint->netmaskMax)
84+
->setParameter('{{ max }}', $netmaskMax)
8585
->setCode(Cidr::OUT_OF_RANGE_ERROR)
8686
->addViolation();
8787
}

Tests/Constraints/CidrValidatorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function testInvalidIpAddressAndNetmask(string|\Stringable $cidr)
106106
/**
107107
* @dataProvider getOutOfRangeNetmask
108108
*/
109-
public function testOutOfRangeNetmask(string $cidr, ?string $version = null, ?int $min = null, ?int $max = null)
109+
public function testOutOfRangeNetmask(string $cidr, int $maxExpected, ?string $version = null, ?int $min = null, ?int $max = null)
110110
{
111111
$cidrConstraint = new Cidr([
112112
'version' => $version,
@@ -118,7 +118,7 @@ public function testOutOfRangeNetmask(string $cidr, ?string $version = null, ?in
118118
$this
119119
->buildViolation('The value of the netmask should be between {{ min }} and {{ max }}.')
120120
->setParameter('{{ min }}', $cidrConstraint->netmaskMin)
121-
->setParameter('{{ max }}', $cidrConstraint->netmaskMax)
121+
->setParameter('{{ max }}', $maxExpected)
122122
->setCode(Cidr::OUT_OF_RANGE_ERROR)
123123
->assertRaised();
124124
}
@@ -240,9 +240,9 @@ public static function getWithInvalidMasksAndIps(): array
240240
public static function getOutOfRangeNetmask(): array
241241
{
242242
return [
243-
['10.0.0.0/24', Ip::V4, 10, 20],
244-
['10.0.0.0/128'],
245-
['2001:0DB8:85A3:0000:0000:8A2E:0370:7334/24', Ip::V6, 10, 20],
243+
['10.0.0.0/24', 20, Ip::V4, 10, 20],
244+
['10.0.0.0/128', 32],
245+
['2001:0DB8:85A3:0000:0000:8A2E:0370:7334/24', 20, Ip::V6, 10, 20],
246246
];
247247
}
248248

0 commit comments

Comments
 (0)