mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
more documentation updates
This commit is contained in:
@@ -233,20 +233,24 @@ enum ISODateTransformer: Transformer {
|
||||
}
|
||||
```
|
||||
|
||||
Then you define the attribute as a `TransformAttribute` instead of an `Attribute`:
|
||||
Then you define the attribute as a `TransformedAttribute` instead of an `Attribute`:
|
||||
```
|
||||
let date: TransformAttribute<String, ISODateTransformer>
|
||||
let date: TransformedAttribute<String, ISODateTransformer>
|
||||
```
|
||||
|
||||
Note that the first generic parameter of `TransformAttribute` is the type you expect to decode from JSON, not the type you want to end up with after transformation.
|
||||
|
||||
#### `Validator`
|
||||
|
||||
You can also creator `Validator`s and `ValidatedAttribute`s. A `Validator` is just a `Transformer` that by convention does not perform a transformation. It simply `throws` if an attribute value is invalid.
|
||||
|
||||
### `JSONAPIDocument`
|
||||
|
||||
The entirety of a JSON API request or response is encoded or decoded from- or to a `JSONAPIDocument`. As an example, a JSON API response containing one `Person` and no included entities could be decoded as follows:
|
||||
```
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
let responseStructure = JSONAPIDocument<SingleResourceBody<Person>, NoMetadata, NoIncludes, BasicJSONAPIError>.self
|
||||
let responseStructure = JSONAPIDocument<SingleResourceBody<Person>, NoMetadata, NoLinks, NoIncludes, BasicJSONAPIError>.self
|
||||
|
||||
let document = try decoder.decode(responseStructure, from: data)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user