Skip to content
Snippets Groups Projects
Commit f16f149e authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

chore: validate mail

parent 5ef610ae
No related branches found
No related tags found
No related merge requests found
...@@ -86,24 +86,26 @@ final readonly class StringConstraint extends Constraint ...@@ -86,24 +86,26 @@ final readonly class StringConstraint extends Constraint
); );
} }
if (ConstraintStringFormat::Uuid === $this->format) { $isFormatValid = match ($this->format) {
if (uuid_is_valid($notValidatedData)) { ConstraintStringFormat::Mail => false !== filter_var($notValidatedData, FILTER_VALIDATE_EMAIL),
return new ConstraintResult( ConstraintStringFormat::Uuid => uuid_is_valid($notValidatedData),
castedData: $notValidatedData, default => throw new RuntimeException('Unknown string format'),
path: $path, };
reason: ConstraintReason::Ok,
status: ConstraintResultStatus::Valid,
);
}
if ($isFormatValid) {
return new ConstraintResult( return new ConstraintResult(
castedData: $notValidatedData, castedData: $notValidatedData,
path: $path, path: $path,
reason: ConstraintReason::InvalidFormat, reason: ConstraintReason::Ok,
status: ConstraintResultStatus::Invalid, status: ConstraintResultStatus::Valid,
); );
} }
throw new RuntimeException('Unknown string format'); return new ConstraintResult(
castedData: $notValidatedData,
path: $path,
reason: ConstraintReason::InvalidFormat,
status: ConstraintResultStatus::Invalid,
);
} }
} }
...@@ -52,6 +52,14 @@ final class StringConstraintTest extends TestCase ...@@ -52,6 +52,14 @@ final class StringConstraintTest extends TestCase
self::assertTrue($constraint->validate('hi')->status->isValid()); self::assertTrue($constraint->validate('hi')->status->isValid());
} }
public function test_validates_mail(): void
{
$constraint = new StringConstraint(format: ConstraintStringFormat::Mail);
self::assertFalse($constraint->validate('hi')->status->isValid());
self::assertTrue($constraint->validate('test@example.com')->status->isValid());
}
public function test_validates_uuid(): void public function test_validates_uuid(): void
{ {
$constraint = new StringConstraint(format: ConstraintStringFormat::Uuid); $constraint = new StringConstraint(format: ConstraintStringFormat::Uuid);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment