Skip to content

Literal defaults #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,18 @@ Str::make('email')->writableOnCreate();
### Default Values

If you would like to provide a default value to be used when creating a new
resource if there is no value provided in the request, you can pass a closure to
the `default` method:
resource if there is no value provided in the request, you can pass a closure or
a literal value to the `default` method.

A closure will receive the current request context as an argument when called.

```php
use Tobyz\JsonApiServer\Field\DateTime;
use Tobyz\JsonApiServer\Field;

DateTime::make('joinedAt')->default(fn() => new \DateTime());
Field\Str::make('name')->default('Anonymous');
Field\DateTime::make('joinedAt')->default(
fn(Context $context) => new \DateTime(),
);
```

### Required
Expand Down
6 changes: 5 additions & 1 deletion src/Schema/Concerns/SetsValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public function required(bool $required = true): static
/**
* Set a default value for this field.
*/
public function default(?Closure $default): static
public function default(mixed $default): static
{
if (!$default instanceof Closure) {
$default = fn() => $default;
}

$this->default = $default;

return $this;
Expand Down
28 changes: 27 additions & 1 deletion tests/feature/FieldDefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp(): void
$this->api = new JsonApi();
}

public function test_default_value_used_if_field_not_present()
public function test_default_closure_value_used_if_field_not_present()
{
$this->api->resource(
new MockResource(
Expand All @@ -43,6 +43,32 @@ public function test_default_value_used_if_field_not_present()
);
}

public function test_default_literal_value_used_if_field_not_present()
{
$this->api->resource(
new MockResource(
'users',
endpoints: [Create::make()],
fields: [
Attribute::make('name')
->writable()
->default('default'),
],
),
);

$response = $this->api->handle(
$this->buildRequest('POST', '/users')->withParsedBody([
'data' => ['type' => 'users'],
]),
);

$this->assertJsonApiDocumentSubset(
['data' => ['attributes' => ['name' => 'default']]],
$response->getBody(),
);
}

public function test_default_value_not_used_if_field_present()
{
$this->api->resource(
Expand Down