jonericcook
Best practices when needing to take in a large number of parameters?
So Im writing a wrapper around this API Redoc Interactive Demo
Some of the endpoints, like this one Redoc Interactive Demo (be sure to expand all of the objects to see all that’s needed), need a lot of user input in order to create the proper POST body.
Im at a loss as to how to collect all input from the user. I know its a no no to have a function accept more than 5 params. It also feels weird to have the user input a single param (a big map). They wont know what shape the map needs to be in. It also feels like a cop out to link to the API docs so they can see the map shape possibilities. Also, since the request body can take different shapes i feel that a struct wouldn’t work.
Check out this screenshot for the possible actions.
Thanks for any help / input.
Most Liked
soup
When I have done this for very complicated forms I make sub-structs as described.
I actually use embedded Ecto schemas and write each form section as its own component, probably each disclosure section in your API example would be one component.
The forms act as any other live view form but when they validate successfully I send(self(), {:form_section, %Struct{}}) or send(self(), {:form_section, normalised_params}) to the owning live view, which itself manages whether the whole “form” is submittable or not.
When a form “unvalidates” itself, it send(self(), {:form_section, nil}) which then “unvalidates” the owner too “for free”.
Whether I send a struct or params back depends on the complexity and who I want to own the final shape. If the sub-form section doesn’t match close enough to what I want to store, it’s probably sending params back up the chain instead.
That is to say, the form may ask for dimensions for example, with a unit option, but the main data structure may always store in mm, so the component would work with a %Dimension{value, unit} but send %{value: value_in_mm} back up to the owner. This can keep the validation/processing of unit in the one component and the owner can just trust that the data its getting is already correct.
I think this gives a good balance of coupling and separation, as well pretty easy control over showing parts of a form in a workflow or whatever.
dom
There’s only three required parameters, so you could do:
def build_transaction(network_id, actions, fee_payer, options \\ [])
at_state_id = Keyword.get(options, :at_state_id)
...
end
dom
Map.from_struct, which you can call recursively yourself. But 4 arguments is not excessive IMHO, the stdlib has functions with more than that: Task — Elixir v1.13.4
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










