Skip to content

Commit

Permalink
refactor: use client id rule in id field
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Mar 29, 2024
1 parent d6c1087 commit aeedead
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Fields/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use LaravelJsonApi\Core\Schema\Concerns\Sortable;
use LaravelJsonApi\Eloquent\Contracts\Fillable;
use LaravelJsonApi\Validation\Fields\IsValidated;
use LaravelJsonApi\Validation\Rules\ClientId;

class ID implements IDContract, Fillable, IsValidated
{
Expand Down Expand Up @@ -126,7 +127,7 @@ public function nullable(): self
public function rulesForCreation(?Request $request): array|null
{
if ($this->acceptsClientIds()) {
return [$this->validationModifier, "regex:/^{$this->pattern}$/{$this->flags}"];
return [$this->validationModifier, new ClientId($this)];
}

return null;
Expand Down
5 changes: 3 additions & 2 deletions tests/lib/Integration/Fields/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Tests\Integration\TestCase;
use LaravelJsonApi\Validation\Fields\IsValidated;
use LaravelJsonApi\Validation\Rules\ClientId;

class IdTest extends TestCase
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testIsValidatedWhenClientId(): void
$id = ID::make()->clientIds();

$this->assertInstanceOf(IsValidated::class, $id);
$this->assertSame(['required', 'regex:/^[0-9]+$/iD'], $id->rulesForCreation(null));
$this->assertEquals(['required', new ClientId($id)], $id->rulesForCreation(null));
$this->assertNull($id->rulesForUpdate(null, new \stdClass()));
}

Expand All @@ -70,7 +71,7 @@ public function testIsValidatedWhenNullableClientId(): void
$id = ID::make()->clientIds()->nullable();

$this->assertInstanceOf(IsValidated::class, $id);
$this->assertSame(['nullable', 'regex:/^[0-9]+$/iD'], $id->rulesForCreation(null));
$this->assertEquals(['nullable', new ClientId($id)], $id->rulesForCreation(null));
$this->assertNull($id->rulesForUpdate(null, new \stdClass()));
}

Expand Down

0 comments on commit aeedead

Please sign in to comment.