|
1 | 1 | # AshRandomParams |
2 | 2 |
|
3 | | -Shortcut DSL for attributes and relationships in Ash resources. |
| 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 | | -### For attribute |
| 5 | +## Installation |
6 | 6 |
|
7 | | -| DSL | allow_nil? | public? | |
8 | | -|:---:|:----------:|:-------:| |
9 | | -| `attribute` (Ash) | true | false | |
10 | | -| `req` | false | true | |
11 | | -| `req_prv` | false | false | |
12 | | -| `opt` | true | true | |
13 | | -| `opt_prv` | true | false | |
| 7 | +Add `ash_random_params` to your list of dependencies in `mix.exs`: |
14 | 8 |
|
15 | | -### For belongs_to |
| 9 | +```elixir |
| 10 | +def deps do |
| 11 | + [ |
| 12 | + {:ash_random_params, "~> 0.2.0"} |
| 13 | + ] |
| 14 | +end |
| 15 | +``` |
16 | 16 |
|
17 | | -| DSL | allow_nil? | public? | |
18 | | -|:---:|:----------:|:-------:| |
19 | | -| `belongs_to` (Ash) | true | false | |
20 | | -| `req_belongs_to` | false | true | |
21 | | -| `req_prv_belongs_to` | false | false | |
22 | | -| `opt_belongs_to` | true | true | |
23 | | -| `opt_prv_belongs_to` | true | false | |
| 17 | +## Usage |
24 | 18 |
|
25 | | -## Usage Example |
| 19 | +Add the `random_params` DSL to your Ash resource: |
26 | 20 |
|
27 | 21 | ```elixir |
28 | | -defmodule MyApp.User do |
| 22 | +defmodule MyApp.Post do |
29 | 23 | use Ash.Resource, extensions: [AshRandomParams] |
30 | 24 |
|
31 | 25 | attributes do |
32 | 26 | uuid_primary_key :id |
| 27 | + attribute :title, :string, allow_nil?: false |
| 28 | + attribute :content, :string, allow_nil?: true |
| 29 | + end |
33 | 30 |
|
34 | | - # Required attributes |
35 | | - req :email, :string # allow_nil?: false, public?: true |
36 | | - req_prv :password_hash, :string # allow_nil?: false, public?: false |
| 31 | + random_params do |
| 32 | + random MyRandom # Optional: specify your custom random generator |
| 33 | + end |
| 34 | +end |
| 35 | +``` |
37 | 36 |
|
38 | | - # Optional attributes |
39 | | - opt :name, :string # allow_nil?: true, public?: true |
40 | | - opt_prv :last_login_at, :utc_datetime # allow_nil?: true, public?: false |
| 37 | +### Custom Random Generator |
41 | 38 |
|
42 | | - # Original attribute macro still works |
43 | | - attribute :nickname, :string, allow_nil?: true |
44 | | - end |
| 39 | +You can implement a custom random generator by using the `AshRandomParams.Random` behaviour: |
45 | 40 |
|
46 | | - relationships do |
47 | | - # Required relationships |
48 | | - req_belongs_to :company, Company # allow_nil?: false, public?: true |
49 | | - req_prv_belongs_to :created_by, User # allow_nil?: false, public?: false |
| 41 | +```elixir |
| 42 | +defmodule MyRandom do |
| 43 | + use AshRandomParams.Random |
50 | 44 |
|
51 | | - # Optional relationships |
52 | | - opt_belongs_to :manager, User # allow_nil?: true, public?: true |
53 | | - opt_prv_belongs_to :updated_by, User # allow_nil?: true, public?: false |
| 45 | + @impl AshRandomParams.Random |
| 46 | + def random(%{type: Ash.Type.Integer}, _opts, _context) do |
| 47 | + 777 # Custom random integer |
| 48 | + end |
54 | 49 |
|
55 | | - # Original belongs_to macro still works |
56 | | - belongs_to :department, Department, allow_nil?: true |
| 50 | + @impl AshRandomParams.Random |
| 51 | + def random(attr_or_arg, opts, context) do |
| 52 | + # Fallback to DefaultRandom for all other types |
| 53 | + AshRandomParams.DefaultRandom.random(attr_or_arg, opts, context) |
57 | 54 | end |
58 | 55 | end |
59 | 56 | ``` |
60 | 57 |
|
61 | | -## Installation |
62 | | - |
63 | | -Add `ash_random_params` to your list of dependencies in `mix.exs`: |
| 58 | +### Using Random Params |
64 | 59 |
|
65 | 60 | ```elixir |
66 | | -def deps do |
67 | | - [ |
68 | | - {:ash_random_params, "~> 0.2.0"} |
69 | | - ] |
70 | | -end |
| 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 | +}) |
71 | 72 | ``` |
72 | 73 |
|
| 74 | +## Features |
| 75 | + |
| 76 | +- Automatically generates random values for action accepts and arguments |
| 77 | +- Supports custom random value generators |
| 78 | +- Useful for testing and development |
| 79 | + |
73 | 80 | ## License |
74 | 81 |
|
75 | 82 | MIT |
76 | 83 |
|
| 84 | +## Links |
| 85 | + |
| 86 | +- [GitHub Repository](https://github.com/devall-org/ash_random_params) |
0 commit comments