Skip to content

Commit

Permalink
update example for optional doc
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi authored and NaridaL committed Aug 5, 2020
1 parent 673d744 commit 69fed6f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,16 +629,16 @@ const todo = deserialize(Todo, {
})
```
### _function_ `optional`(_propSchema_?: [PropSchema](#interface-propschemasrc) | boolean): [PropSchema](#interface-propschemasrc) <sub><a href="src/types/optional.ts#L22">src</a></sub>
### _function_ `optional`(_propSchema_?: [PropSchema](#interface-propschemasrc) | boolean): [PropSchema](#interface-propschemasrc) <sub><a href="src/types/optional.ts#L23">src</a></sub>
Optional indicates that this model property shouldn't be serialized if it isn't present.
Note that if we use `optional` together with another prop schema such as `custom`, the prop schema for `custom` will be applied first and the result of that serialization will be used to feed into `optional`.
Note that if we use `optional` together with another prop schema such as `custom`, the prop schema for `custom` will be applied first and the result of that serialization will be used to feed into `optional`. As such, it might be better to just use `custom` with `SKIP` to achieve the same goal.
```ts
createModelSchema(Todo, {
title: optional(primitive()),
user: optional(custom(value => value.name, () => SKIP))
user: optional(custom(value => value?.name, () => SKIP))
})

serialize(new Todo()) // {}
Expand Down
5 changes: 3 additions & 2 deletions src/types/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { PropSchema } from "../api/types"
*
* Note that if we use `optional` together with another prop schema such as `custom`,
* the prop schema for `custom` will be applied first and the result of that serialization
* will be used to feed into `optional`.
* will be used to feed into `optional`. As such, it might be better to just use `custom`
* with `SKIP` to achieve the same goal.
*
* @example
* createModelSchema(Todo, {
* title: optional(primitive()),
* user: optional(custom(value => value.name, () => SKIP))
* user: optional(custom(value => value?.name, () => SKIP))
* })
*
* serialize(new Todo()) // {}
Expand Down

0 comments on commit 69fed6f

Please sign in to comment.