Skip to content

Commit 1d91b95

Browse files
committed
docs: improve null option documentation clarity
- Clarify that null: true is the default behavior - Condense option tables from 8 rows to 4 using "-" for irrelevant values - Use consistent column order (default, enforce, null) across tables - Improve wording for "Providing Defaults" section
1 parent 5963c25 commit 1d91b95

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595

9696
### Providing Defaults
9797

98-
Fields with defaults aren't nullable (no reason to be):
98+
Fields with defaults don't need to be nullable since they always have a value:
9999

100100
```elixir
101101
typed_structor do
@@ -128,12 +128,12 @@ end
128128

129129
The interaction between `enforce`, `default`, and `null` follows this logic:
130130

131-
| enforce | default | null | Type includes nil? |
132-
|---------|---------|---------|-------------------|
133-
| false | no | true | Yes (nullable) |
134-
| false | no | false | No |
135-
| false | yes | - | No |
136-
| true | - | - | No |
131+
| `:default` | `:enforce` | `:null` | Type includes `nil`? |
132+
|------------|------------|---------|----------------------|
133+
| `unset` | `false` | `true` | yes |
134+
| `unset` | `false` | `false` | no |
135+
| `set` | - | - | no |
136+
| - | `true` | - | no |
137137

138138
This is particularly useful when modeling database records where some fields can be `nil`:
139139

lib/typed_structor.ex

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule TypedStructor do
2727
2828
* `:module` - if provided, a new submodule will be created with the struct.
2929
* `:enforce` - if `true`, the struct will enforce the keys, see `field/3` options for more information.
30-
* `:null` - if `true`, all fields without a default value and not enforced will have `nil` added to their type.
30+
* `:null` - if `true` (default), fields without a default value and not enforced will have `nil` added to their type. If `false`, prevents `nil` from being added.
3131
* `:definer` - the definer module to use to define the struct, record or exception. Defaults to `:defstruct`. It also accepts a macro that receives the definition struct and returns the AST. See definer section below.
3232
* `:type_kind` - the kind of type to use for the struct. Defaults to `type`, can be `opaque` or `typep`.
3333
* `:type_name` - the name of the type to use for the struct. Defaults to `t`.
@@ -238,19 +238,16 @@ defmodule TypedStructor do
238238
* `:default` - sets the default value for the field
239239
* `:enforce` - if set to `true`, enforces the field, and makes its type
240240
non-nullable if `:default` is not set
241-
* `:null` - if set to `true`, makes the field type nullable
242-
if `:default` is not set and `:enforce` is not set
241+
* `:null` - when set to `true` (the default), allows `nil` to be added to the
242+
field type when `:default` is not set and `:enforce` is not set; when set
243+
to `false`, prevents `nil` from being added in that case
243244
244245
> ### How `:default`, `:enforce` and `:null` affect `type` and `@enforce_keys` {: .tip}
245246
246247
> | **`:default`** | **`:enforce`** | **`:null`** | **`type`** | **`@enforce_keys`** |
247248
> | -------------- | -------------- | ----------- | ----------------- | ------------------- |
248-
> | `set` | `true` | `true` | `t()` | `excluded` |
249-
> | `set` | `true` | `false` | `t()` | `excluded` |
250-
> | `set` | `false` | `true` | `t()` | `excluded` |
251-
> | `set` | `false` | `false` | `t()` | `excluded` |
252-
> | `unset` | `true` | `true` | `t()` | **`included`** |
253-
> | `unset` | `true` | `false` | `t()` | **`included`** |
249+
> | `set` | - | - | `t()` | `excluded` |
250+
> | `unset` | `true` | - | `t()` | **`included`** |
254251
> | `unset` | `false` | `true` | **`t() \\| nil`** | `excluded` |
255252
> | `unset` | `false` | `false` | `t()` | `excluded` |
256253
"""

0 commit comments

Comments
 (0)