Skip to content

Commit cbb54f9

Browse files
committed
Update docs
- Add concept of collections - Add page for Heterogeneous Collections - Update Attributes page with new Types paradigm - Move Deferred Values into its own page - Update VitePress
1 parent fd089c6 commit cbb54f9

21 files changed

+1763
-843
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use Tobyz\JsonApiServer\Laravel\EloquentResource;
4343
use Tobyz\JsonApiServer\Laravel\Filter;
4444
use Tobyz\JsonApiServer\Endpoint;
4545
use Tobyz\JsonApiServer\Schema\Field;
46+
use Tobyz\JsonApiServer\Schema\Type;
4647
use Tobyz\JsonApiServer\JsonApi;
4748

4849
class UsersResource extends EloquentResource
@@ -71,13 +72,14 @@ class UsersResource extends EloquentResource
7172
public function fields(): array
7273
{
7374
return [
74-
Field\Str::make('name')
75+
Field\Attribute::make('name')
76+
->type(Type\Str::make())
7577
->writable()
7678
->required(),
7779

78-
Field\HasOne::make('address')->includable(),
80+
Field\ToOne::make('address')->includable(),
7981

80-
Field\HasMany::make('friends')
82+
Field\ToMany::make('friends')
8183
->type('users')
8284
->includable(),
8385
];

docs/.vitepress/config.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ export default defineConfig({
1414
{ text: 'Introduction', link: '/' },
1515
{ text: 'Installation', link: '/install' },
1616
{ text: 'Handling Requests', link: '/requests' },
17-
{ text: 'Defining Resources', link: '/resources' },
1817
{ text: 'Laravel Integration', link: '/laravel' },
1918
],
2019
},
2120
{
22-
text: 'Schema',
21+
text: 'Resources',
2322
items: [
24-
{ text: 'Fields', link: '/fields' },
23+
{ text: 'Defining Resources', link: '/resources' },
24+
{ text: 'Defining Fields', link: '/fields' },
2525
{ text: 'Attributes', link: '/attributes' },
2626
{ text: 'Relationships', link: '/relationships' },
2727
],
2828
},
2929
{
3030
text: 'Endpoints',
3131
items: [
32-
{ text: 'List', link: '/list' },
32+
{ text: 'Index', link: '/list' },
3333
{ text: 'Create', link: '/create' },
3434
{ text: 'Show', link: '/show' },
3535
{ text: 'Update', link: '/update' },
@@ -40,9 +40,14 @@ export default defineConfig({
4040
text: 'Advanced',
4141
items: [
4242
{ text: 'Context', link: '/context' },
43+
{ text: 'Deferred Values', link: '/deferred' },
4344
{ text: 'Error Handling', link: '/errors' },
4445
{ text: 'Extensions', link: '/extensions' },
45-
{ text: 'OpenAPI', link: '/openapi' },
46+
{
47+
text: 'Heterogeneous Collections',
48+
link: '/collections',
49+
},
50+
{ text: 'OpenAPI Definitions', link: '/openapi' },
4651
],
4752
},
4853
],
@@ -59,7 +64,4 @@ export default defineConfig({
5964
'https://github.com/tobyzerner/json-api-server/edit/main/docs/:path',
6065
},
6166
},
62-
markdown: {
63-
theme: 'nord',
64-
},
6567
});

docs/.vitepress/theme/custom.css

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
:root {
2-
--vp-c-brand: #0000ff;
2+
--vp-c-brand-1: #0000ff;
3+
--vp-c-brand-2: var(--vp-c-brand-1);
34
}
45

56
.dark {
6-
--vp-c-brand: #8a8aff;
7-
}
8-
9-
.vp-doc h1 + p {
10-
font-size: 140%;
11-
line-height: 1.5;
12-
}
13-
14-
.vp-doc a:not(.header-anchor) {
15-
text-decoration: underline;
16-
text-decoration-style: solid;
17-
font-weight: inherit;
7+
--vp-c-brand-1: #8a8aff;
188
}

docs/attributes.md

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,90 @@
11
# Attributes
22

3-
## Generic Attributes
4-
5-
You can define a generic attribute on your resource using the `Attribute` class.
6-
No specific serialization, deserialization, or validation will be performed:
3+
You can define a attribute field on your resource using the `Attribute` class.
74

85
```php
9-
use Tobyz\JsonApiServer\Field\Attribute;
6+
use Tobyz\JsonApiServer\Schema\Field\Attribute;
107

118
Attribute::make('title');
129
```
1310

14-
## Typed Attributes
11+
## Attribute Types
1512

16-
json-api-server includes a selection of typed attribute implementations to match
17-
the data types in the
13+
Attributes can be configured with a type to automatically perform appropriate
14+
serialization, deserialization, and validation. json-api-server includes a
15+
selection of type implementations to match the data types in the
1816
[OpenAPI specification](https://swagger.io/docs/specification/data-models/data-types/).
1917

2018
### Boolean
2119

22-
The `Boolean` attribute serializes values to booleans, and performs validation
23-
to ensure that incoming values are booleans.
20+
The `Boolean` type serializes values to booleans, and performs validation to
21+
ensure that incoming values are booleans.
2422

2523
```php
26-
use Tobyz\JsonApiServer\Field\Boolean;
24+
use Tobyz\JsonApiServer\Schema\Type\Boolean;
2725

28-
Boolean::make('active');
26+
Attribute::make('active')->type(Boolean::make());
2927
```
3028

3129
### Date and DateTime
3230

33-
The `Date` and `DateTime` attributes serialize and deserialize values between
34-
strings using RFC 3339 notation and `DateTime` objects, and perform validation
35-
to ensure that incoming values match this format.
31+
The `Date` and `DateTime` types serialize and deserialize values between strings
32+
using RFC 3339 notation and `DateTime` objects, and perform validation to ensure
33+
that incoming values match this format.
3634

3735
```php
38-
use Tobyz\JsonApiServer\Field\Date;
39-
use Tobyz\JsonApiServer\Field\DateTime;
40-
41-
Date::make('dob');
42-
DateTime::make('publishedAt');
43-
```
44-
45-
### BooleanDateTime
46-
47-
The `BooleanDateTime` attribute behaves like the `Boolean` attribute, except
48-
that before setting the value to the model, a `true` value is set as the current
49-
date, and a `false` value is set as `null`. This can be used to represent a
50-
field that is stored internally as a timestamp as a boolean in the API.
36+
use Tobyz\JsonApiServer\Schema\Type\Date;
37+
use Tobyz\JsonApiServer\Schema\Type\DateTime;
5138

52-
```php
53-
use Tobyz\JsonApiServer\Field\DateTime;
54-
55-
BooleanDateTime::make('isDeleted')
56-
->property('deleted_at')
57-
->writable();
39+
Attribute::make('dob')->type(Date::make());
40+
Attribute::make('publishedAt')->type(DateTime::make());
5841
```
5942

6043
### Number and Integer
6144

62-
The `Number` attribute serializes values to floats, and performs validation to
63-
ensure that incoming values are numeric.
45+
The `Number` type serializes values to floats, and performs validation to ensure
46+
that incoming values are numeric.
6447

6548
```php
66-
use Tobyz\JsonApiServer\Field\Number;
49+
use Tobyz\JsonApiServer\Schema\Type\Number;
6750

68-
Number::make('weight');
51+
Attribute::make('weight')->type(Number::make());
6952
```
7053

71-
The `Integer` attribute serializes values to integers, and performs validation
72-
to ensure that incoming values are integers.
54+
The `Integer` type serializes values to integers, and performs validation to
55+
ensure that incoming values are integers.
7356

7457
```php
75-
use Tobyz\JsonApiServer\Field\Integer;
58+
use Tobyz\JsonApiServer\Schema\Type\Integer;
7659

77-
Integer::make('commentCount');
60+
Attribute::make('commentCount')->type(Integer::make());
7861
```
7962

8063
#### Minimum and Maximum
8164

8265
Use the `minimum` and `maximum` methods to specify the range of possible values.
8366

8467
```php
85-
use Tobyz\JsonApiServer\Field\Number;
68+
use Tobyz\JsonApiServer\Schema\Type\Number;
8669

87-
Number::make('number')
88-
->minimum(1)
89-
->maximum(20);
70+
Attribute::make('number')->type(
71+
Number::make()
72+
->minimum(1)
73+
->maximum(20),
74+
);
9075
```
9176

9277
By default, these values are included in the range. To exclude the boundary
9378
values, you can add a second argument:
9479

9580
```php
96-
use Tobyz\JsonApiServer\Field\Number;
81+
use Tobyz\JsonApiServer\Schema\Type\Number;
9782

98-
Number::make('number')
99-
->minimum(1, exclusive: true)
100-
->maximum(20, exclusive: true);
83+
Attribute::make('number')->type(
84+
Number::make()
85+
->minimum(1, exclusive: true)
86+
->maximum(20, exclusive: true),
87+
);
10188
```
10289

10390
#### Multiples
@@ -106,38 +93,40 @@ Use the `multipleOf` method to specify that a number must be the multiple of
10693
another number:
10794

10895
```php
109-
use Tobyz\JsonApiServer\Field\Integer;
96+
use Tobyz\JsonApiServer\Schema\Type\Integer;
11097

111-
Integer::make('number')->multipleOf(10);
98+
Attribute::make('number')->type(Integer::make()->multipleOf(10));
11299
```
113100

114101
### String
115102

116-
The `Str` attribute serializes values to strings, and performs validation to
117-
ensure that incoming values are strings.
103+
The `Str` type serializes values to strings, and performs validation to ensure
104+
that incoming values are strings.
118105

119106
```php
120-
use Tobyz\JsonApiServer\Field\Str;
107+
use Tobyz\JsonApiServer\Schema\Type\Str;
121108

122-
Str::make('name');
109+
Attribute::make('name')->type(Str::make());
123110
```
124111

125112
#### `minLength` and `maxLength`
126113

127114
String length can be restricted using the `minLength` and `maxLength` methods:
128115

129116
```php
130-
Str::make('name')
131-
->minLength(3)
132-
->maxLength(20);
117+
Attribute::make('name')->type(
118+
Str::make()
119+
->minLength(3)
120+
->maxLength(20),
121+
);
133122
```
134123

135124
#### `enum`
136125

137126
You can restrict the string to a set of possible values using the `enum` method.
138127

139128
```php
140-
Str::make('status')->enum(['to do', 'doing', 'done']);
129+
Attribute::make('status')->type(Str::make()->enum(['to do', 'doing', 'done']));
141130
```
142131

143132
#### `pattern`
@@ -147,7 +136,7 @@ You can also validate the string against a regular expression using the
147136
and are case-sensitive.
148137

149138
```php
150-
Str::make('ssn')->pattern('^\d{3}-\d{2}-\d{4}$');
139+
Attribute::make('ssn')->type(Str::make()->pattern('^\d{3}-\d{2}-\d{4}$'));
151140
```
152141

153142
#### `format`
@@ -157,10 +146,28 @@ You can mark strings with a
157146
to serve as a hint in your OpenAPI definition:
158147

159148
```php
160-
Str::make('email')->format('email');
149+
Attribute::make('email')->type(Str::make()->format('email'));
161150
```
162151

163152
Note that this will not add any additional behaviour (like serialization and
164153
validation) to the field – you will need to implement this yourself. For the
165154
`date` and `date-time` formats, you should use the
166-
[Date and DateTime](#date-and-datetime) attributes instead.
155+
[Date and DateTime](#date-and-datetime) types instead.
156+
157+
## Special Attributes
158+
159+
### BooleanDateTime
160+
161+
The `BooleanDateTime` attribute subclass behaves like a `Boolean`-typed
162+
attribute, except that before setting the value to the model, a `true` value is
163+
set as the current date, and a `false` value is set as `null`. This can be used
164+
to represent a field that is stored internally as a timestamp as a boolean in
165+
the API.
166+
167+
```php
168+
use Tobyz\JsonApiServer\Schema\Field\BooleanDateTime;
169+
170+
BooleanDateTime::make('isDeleted')
171+
->property('deleted_at')
172+
->writable();
173+
```

0 commit comments

Comments
 (0)