Skip to content

Commit

Permalink
Fixed Validation example, more tests, fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
acelot committed Oct 4, 2023
1 parent 51e9fa5 commit e4599eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions integrations/respect-validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ $result = marshalArray(
a::get('[email]'),
a::validate(v::email()),
a::ifValidationFailed(a::ignore())
)),
a::toKey('phone', a::pipe(
a::get('[phone]'),
a::validate(v::phone()),
a::ifValidationFailed(a::ignore())
))
);

Expand Down
31 changes: 21 additions & 10 deletions integrations/respect-validation/tests/Functional/validateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function testFunction(): void
{
$source = [
'id' => 10,
'email' => '[email protected]',
'phone' => '+1-111-111-1111',
'email' => 'some@@email.com',
'phone' => '000+1-111-111-1111',
'ip' => '127.0.0.256'
];

Expand All @@ -26,11 +26,13 @@ public function testFunction(): void
$source,
a::toKey('email', a::pipe(
a::get('[email]'),
a::validate(v::email()),
a::validate(v::email()->setName('E-mail')),
a::ifValidationFailed(a::value('[email protected]')),
)),
a::toKey('phone', a::pipe(
a::get('[phone]'),
a::validate(v::phone()),
a::validate(v::phone()->setName('Phone')),
a::ifValidationFailed(a::ignore())
)),
a::toKey('ip', a::pipe(
a::get('[ip]'),
Expand All @@ -41,19 +43,28 @@ public function testFunction(): void

self::assertSame(
[
'email' => '[email protected]',
'phone' => '+1-111-111-1111',
'email' => '[email protected]',
'ip' => '127.0.0.1',
],
$result
);

$validationContext = $context->get(ValidationContextInterface::class);

self::assertTrue($context->has(ValidationContextInterface::class));
self::assertTrue($context->get(ValidationContextInterface::class)->hasErrors());
self::assertCount(1, $context->get(ValidationContextInterface::class)->getErrors());
self::assertTrue($validationContext->hasErrors());
self::assertCount(3, $validationContext->getErrors());
self::assertSame(
['IP Address' => 'IP Address must be an IP address'],
$context->get(ValidationContextInterface::class)->getErrors()[0]->getMessages()
[
'E-mail' => 'E-mail must be valid email',
'Phone' => 'Phone must be a valid telephone number',
'IP Address' => 'IP Address must be an IP address',
],
[
...$validationContext->getErrors()[0]->getMessages(),
...$validationContext->getErrors()[1]->getMessages(),
...$validationContext->getErrors()[2]->getMessages(),
]
);
}
}
4 changes: 2 additions & 2 deletions src/Api/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function trimString(string $characters = " \t\n\r\0\x0B"): Pipeline

public function toBool(): Call
{
return new Call('boolval');
return new Call('boolval');
}

public function toFloat(): Pipeline
Expand Down Expand Up @@ -121,7 +121,7 @@ public function toString(): Pipeline

public function toArray(): Call
{
return new Call(function ($value) {
return new Call(function ($value) {
if ($value instanceof Traversable) {
return iterator_to_array($value);
}
Expand Down

0 comments on commit e4599eb

Please sign in to comment.