Component attribute definition
Dear community. I have a component
but I want to contraint data. It is a list of elements like
Instead of defining my attribute as
:any. Is there a way to define the structure ?
Also, the structure can be flatten if needed.
Shall I have a function inside my component that verifies the structure is correct ?
What is the best practice for this ?
(note: It is maybe not really a ash thing... in fact... more an elixir thing... so probably out of this channel...)
Thanks,
Angy.1 Reply
That's a Phoenix thing, and unfortunately the answer is no. The reason is that the type checking needs to be handled either at compile time where possible, or at runtime where it isn't. Both are handled when the components are compiled into AST for the engine, so all type checking has to be explicitly defined there. Here is the complete list of supported types: https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#attr/3-types
If you're not tied to that particular data format, you could refactor to use a struct, which is supported as a type.