You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-30Lines changed: 56 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,30 +2,20 @@
2
2
3
3
A library that generates random parameters for Ash resource actions. It provides a convenient way to create random test data for your Ash resources by automatically generating random values for accepts and arguments.
4
4
5
-
## Installation
6
-
7
-
Add `ash_random_params` to your list of dependencies in `mix.exs`:
By default, it generates random values for attributes and arguments that have `allow_nil?: false` and no default value (`default == nil`). In the example above, `author` and `title` fall into this category.
50
+
51
+
### Belongs To Relationships
52
+
53
+
For attributes and arguments that match the `name` or `source_attribute` of a `belongs_to` relationship:
54
+
- In `create` actions, they are generated with `nil` values
55
+
- In other actions, they are not generated at all
56
+
57
+
This behavior exists because:
58
+
- In `create` actions, omitting a value is equivalent to setting it to `nil`
59
+
- In `update` actions, omitting a value preserves the existing relationship, while explicitly setting it to `nil` removes the relationship
60
+
61
+
### Options
62
+
63
+
-`populate`: Forces generation of random values for specified attributes/arguments, overriding the default behavior
64
+
-`omit`: Prevents generation of random values for specified attributes/arguments, overriding the default behavior
65
+
-`include_defaults?`: When set to `true`, includes default values for attributes/arguments that have either `allow_nil?: true` or a non-nil default value. Defaults to `true`. In the example above, this would add `%{content: nil, tag: "JS"}` to the generated params.
66
+
37
67
### Custom Random Generator
38
68
39
69
You can implement a custom random generator by using the `AshRandomParams.Random` behaviour:
@@ -44,7 +74,7 @@ defmodule MyRandom do
44
74
45
75
@implAshRandomParams.Random
46
76
defrandom(%{type:Ash.Type.Integer}, _opts, _context) do
47
-
777# Custom random integer
77
+
777
48
78
end
49
79
50
80
@implAshRandomParams.Random
@@ -55,28 +85,24 @@ defmodule MyRandom do
55
85
end
56
86
```
57
87
58
-
### Using Random Params
59
-
60
-
```elixir
61
-
# Basic usage
62
-
Post.random_params!(:create)
63
-
64
-
# With initial params
65
-
Post.random_params!(:create, %{key:"value"})
66
-
67
-
# With options
68
-
Post.random_params!(:create, %{key:"value"}, %{
69
-
fill: [:optional_field], # Fill optional fields
70
-
unfill: [:required_field] # Leave required fields as nil
71
-
})
72
-
```
73
-
74
88
## Features
75
89
76
90
- Automatically generates random values for action accepts and arguments
77
91
- Supports custom random value generators
78
92
- Useful for testing and development
79
93
94
+
## Installation
95
+
96
+
Add `ash_random_params` to your list of dependencies in `mix.exs`:
0 commit comments