Skip to content

Commit

Permalink
Correct required condition. (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: david_smith <[email protected]>
  • Loading branch information
zero-to-prod and david_smith authored Nov 12, 2024
1 parent f91758d commit f9c4336
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ class User
'protocols' => ['http', 'udp'], // Optional. Defaults to all.
'on_fail' => [MyAction::class, 'method'], // Optional. Invoked when validation fails.
'exception' => InvalidUrlException::class, // Optional. Throws an exception when not url.
'required' // Optional. Throws \Zerotoprod\DataModel\PropertyRequiredException::class
])]
public string $url;
}
Expand Down
4 changes: 2 additions & 2 deletions src/DataModelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public static function isUrl(mixed $value, array $context, ?ReflectionAttribute
return null;
}

if (!$value || in_array('required', $args, true)) {
throw new PropertyRequiredException("Property `\${$Property->getName()}` is required");
if (!$value && in_array('required', $args, true)) {
throw new PropertyRequiredException("Property `\${$Property->getName()}` is required.");
}

if (!is_string($value)) {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/IsUrl/Required/RequiredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RequiredTest extends TestCase
#[Test] public function required(): void
{
$this->expectException(PropertyRequiredException::class);
$this->expectExceptionMessage('Property `$url` is required.');
User::from();
}

Expand Down

0 comments on commit f9c4336

Please sign in to comment.