Skip to content

Commit

Permalink
Ignore property validation when property value is null (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartosz Ozga <[email protected]>
  • Loading branch information
ozgaB and Bartosz Ozga authored Jan 10, 2025
1 parent b64d0aa commit 4a2bc40
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/Interfaces/Type/CoercerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult;
}
3 changes: 1 addition & 2 deletions src/Interfaces/Type/CoercionServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult|null;
}
2 changes: 0 additions & 2 deletions src/Service/Type/Coercer/BoolCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
): CoerceResult {
if (!is_array($value)) {
$value = [$value];
Expand All @@ -50,7 +49,6 @@ public function coerce(
$property,
$property->isCollection() ? $value : $value[0],
[new Type(['type' => 'bool'])],
$validatePropertyConstraints
);
}
}
1 change: 0 additions & 1 deletion src/Service/Type/Coercer/DateTimeImmutableCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
): CoerceResult {
// php8
$format = ($property->getFormat() ?? new Format())->format ?? $this->defaultDateFormat;
Expand Down
5 changes: 0 additions & 5 deletions src/Service/Type/Coercer/EnumCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
): CoerceResult {
$this->fromKey = $property->getDtoAttributes(FromKey::class)[0] ?? null;

Expand All @@ -63,10 +62,6 @@ public function coerce(
];
}

if ($validatePropertyConstraints) {
$constraints = array_merge($constraints, $property->getConstraints());
}

$violations = $this->validator->startContext()
->atPath($propertyPath)
->validate($value, $constraints)
Expand Down
4 changes: 1 addition & 3 deletions src/Service/Type/Coercer/FloatCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult {
if (!is_array($value)) {
$value = [$value];
Expand All @@ -48,7 +47,6 @@ public function coerce(
$property,
$property->isCollection() ? $value : $value[0],
[new Type(['type' => 'float'])],
$validatePropertyConstraints
);
}
}
4 changes: 1 addition & 3 deletions src/Service/Type/Coercer/IntCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult {
if (!is_array($value)) {
$value = [$value];
Expand All @@ -48,7 +47,6 @@ public function coerce(
$property,
$property->isCollection() ? $value : $value[0],
[new Type(['type' => 'int'])],
$validatePropertyConstraints
);
}
}
1 change: 0 additions & 1 deletion src/Service/Type/Coercer/StringCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function coerce(
$property,
$value, /** @phpstan-ignore-line */
[new Type(['type' => 'string'])],
$validatePropertyConstraints
);
}
}
4 changes: 1 addition & 3 deletions src/Service/Type/Coercer/UploadedFileCoercer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult {
if (!is_array($value)) {
$value = [$value];
Expand All @@ -46,7 +45,6 @@ public function coerce(
$property,
$property->isCollection() ? $value : $value[0] ?? null,
[new Type(['type' => UploadedFile::class])],
$validatePropertyConstraints
);
}
}
17 changes: 2 additions & 15 deletions src/Service/Type/CoercerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,9 @@ public function supports(
public function coerce(
string $propertyPath,
Property $property,
mixed $value,
bool $validatePropertyConstraints = false
mixed $value
): CoerceResult|null {
if (null === $value && !empty($property->getConstraints())) {
$violations = $this->validator->startContext()
->atPath($propertyPath)
->validate($value, $property->getConstraints())
->getViolations();

if (0 !== $violations->count()) {
return new CoerceResult( // @phpstan-ignore-line
null,
$violations
);
}
} elseif ($property->isCollection()) {
if ($property->isCollection()) {
$violations = $this->validator->startContext()
->atPath($propertyPath)
->validate($value, new Assert\Type(['type' => 'array']))
Expand Down
5 changes: 0 additions & 5 deletions src/Traits/Type/CoercerResultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ private function buildResult(
Property $property,
mixed $value,
array $constraints,
bool $validatePropertyConstraints = false
): CoerceResult {
if ($property->isCollection()) {
$constraints = [
Expand All @@ -37,10 +36,6 @@ private function buildResult(
];
}

if ($validatePropertyConstraints) {
$constraints = array_merge($constraints, $property->getConstraints());
}

$violations = $validator->startContext()
->atPath($propertyPath)
->validate($value, $constraints)
Expand Down

0 comments on commit 4a2bc40

Please sign in to comment.