Skip to content

Commit

Permalink
Fixed type value validation for nullable types (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Apr 5, 2024
1 parent 57a19ee commit 814c6c4
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return $value instanceof \DateTimeInterface;
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if (!\is_string($value)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/ListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if (!\is_array($value)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/MapType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if (!\is_array($value)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/StructureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if (!\is_array($value)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/UuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if (\is_object($value)) {
foreach ([Uuid::class, \Ramsey\Uuid\UuidInterface::class, \Symfony\Component\Uid\Uuid::class] as $uuidClass) {
if ($value instanceof $uuidClass) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/XMLNodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if ($value instanceof \DOMNode) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Logical/XMLType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

if ($value instanceof \DOMDocument) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return \is_array($value);
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return \is_callable($value);
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return \is_a($value, $this->class, true);
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return \is_a($value, $this->class, true);
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function isEqual(Type $type) : bool

public function isValid(mixed $value) : bool
{
if ($this->nullable && $value === null) {
return true;
}

return \is_resource($value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test_equals() : void

public function test_is_valid() : void
{
self::assertTrue(type_datetime(true)->isValid(null));
self::assertTrue(type_datetime()->isValid(new \DateTimeImmutable()));
self::assertTrue(type_datetime()->isValid(new \DateTime()));
self::assertFalse(type_datetime()->isValid('2020-01-01'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test_equals() : void

public function test_is_valid() : void
{
self::assertTrue(type_json(true)->isValid(null));
self::assertTrue(type_json()->isValid('{"foo": "bar"}'));
self::assertFalse(type_json()->isValid('{"foo": "bar"'));
self::assertFalse(type_json()->isValid('2'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function test_valid() : void
self::assertTrue(
(new ListType(ListElement::boolean()))->isValid([true, false])
);
self::assertTrue(
(new ListType(ListElement::boolean(), true))->isValid(null)
);
self::assertTrue(
(new ListType(ListElement::string()))->isValid(['one', 'two'])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function test_valid() : void
self::assertTrue(
(new MapType(MapKey::string(), MapValue::string()))->isValid(['one' => 'two'])
);
self::assertTrue(
(new MapType(MapKey::string(), MapValue::string(), true))->isValid(null)
);
self::assertTrue(
(new MapType(MapKey::integer(), MapValue::list(new ListType(ListElement::integer()))))->isValid([[1, 2], [3, 4]])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public function test_valid() : void
self::assertTrue(
(struct_type([struct_element('string', type_string())]))->isValid(['one' => 'two'])
);
self::assertTrue(
(struct_type([struct_element('string', type_string())], true))->isValid(null)
);
self::assertTrue(
(
struct_type([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function test_equals() : void

public function test_is_valid() : void
{
self::assertTrue(type_uuid(true)->isValid(null));
self::assertFalse(type_uuid()->isValid('f6d6e0e8-4b7e-4b0e-8d7a-ff0a0c9c9a5a'));
self::assertFalse(type_uuid()->isValid('f6d6e0e8-4b7e-4b0e-8d7a-ff0a0c9c9a5'));
self::assertFalse(type_uuid()->isValid('2'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test_equals() : void

public function test_is_valid() : void
{
self::assertTrue(type_xml_node(true)->isValid(null));
self::assertTrue(type_xml_node()->isValid(new \DOMDocument()));
self::assertFalse(type_xml_node()->isValid('<xml></xml>'));
self::assertFalse(type_xml_node()->isValid('2020-01-01'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function test_equals() : void

public function test_is_valid() : void
{
self::assertTrue(type_xml(true)->isValid(null));
self::assertTrue(type_xml()->isValid(new \DOMDocument()));
self::assertFalse(type_xml()->isValid('<xml></xml>'));
self::assertFalse(type_xml()->isValid('2020-01-01'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function test_valid() : void
self::assertTrue(
type_array()->isValid([1])
);
self::assertTrue(
type_array(nullable: true)->isValid(null)
);
self::assertFalse(
type_array()->isValid(null)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function test_valid() : void
self::assertTrue(
type_callable(false)->isValid('printf')
);
self::assertTrue(
type_callable(true)->isValid(null)
);
self::assertFalse(
type_callable(false)->isValid('one')
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\PHP\Type\Native;

use function Flow\ETL\DSL\{type_object};
use PHPUnit\Framework\TestCase;

final class ObjectTypeTest extends TestCase
{
public function test_valid() : void
{
self::assertTrue(
type_object(\stdClass::class, true)->isValid(null)
);
self::assertFalse(
type_object(\stdClass::class)->isValid(null)
);
self::assertFalse(
type_object(\stdClass::class)->isValid('one')
);
self::assertFalse(
type_object(\stdClass::class)->isValid(new \ArrayIterator([]))
);
self::assertTrue(
type_object(\stdClass::class)->isValid(new \stdClass())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function test_valid() : void
(type_resource(false))->isValid($handle)
);
\fclose($handle);
self::assertTrue(
type_resource(true)->isValid(null)
);
self::assertFalse(
(type_resource(false))->isValid('one')
);
Expand Down

0 comments on commit 814c6c4

Please sign in to comment.