Skip to content

Commit bc22c28

Browse files
committed
Add usage-rules.md for AI assistant context
1 parent 3926767 commit bc22c28

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

usage-rules.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Rules for working with AshRandomParams
2+
3+
AshRandomParams generates random parameters for Ash resource actions. Useful for test data generation.
4+
5+
## Setup
6+
7+
```elixir
8+
defmodule MyApp.Post do
9+
use Ash.Resource, extensions: [AshRandomParams]
10+
11+
random_params do
12+
random MyRandom # Optional: custom generator
13+
end
14+
end
15+
```
16+
17+
## Basic Usage
18+
19+
```elixir
20+
# Generate random params
21+
Post.random_params!(:create)
22+
23+
# With initial params
24+
Post.random_params!(:create, %{author: "James"})
25+
26+
# With options
27+
Post.random_params!(:create, %{}, %{
28+
enforce_random: [:content], # Force random generation
29+
exclude: [:title], # Skip generation
30+
include_defaults?: false # Exclude defaults
31+
})
32+
```
33+
34+
## Default Behavior
35+
36+
Generates random values only for accepts/arguments with `allow_nil?: false` and `default == nil`.
37+
38+
For `belongs_to` relationship foreign keys:
39+
- `create` actions: generated as `nil`
40+
- Other actions: not generated (preserves existing relationship)
41+
42+
## Custom Random Generator
43+
44+
```elixir
45+
defmodule MyRandom do
46+
use AshRandomParams.Random
47+
48+
@impl true
49+
def random(%{type: Ash.Type.Integer}, _opts, _context) do
50+
777
51+
end
52+
53+
@impl true
54+
def random(attr_or_arg, opts, context) do
55+
# Fallback to DefaultRandom for other types
56+
AshRandomParams.DefaultRandom.random(attr_or_arg, opts, context)
57+
end
58+
end
59+
```
60+
61+
## Options
62+
63+
- **enforce_random**: Force random generation for specific fields
64+
- **exclude**: Skip generation for specific fields
65+
- **include_defaults?**: Include fields with `allow_nil?: true` or non-nil defaults (default: `true`)
66+
67+
## Testing
68+
69+
```elixir
70+
test "create post" do
71+
params = Post.random_params!(:create, %{author: "Test Author"})
72+
assert {:ok, post} = MyApp.Blog.create_post(params)
73+
end
74+
```
75+

0 commit comments

Comments
 (0)