Component attribute definition

Dear community. I have a component
attr :data, :any, required: true

def whateven_chart(assigns) do
~H"""
... code here ...
"""
end
attr :data, :any, required: true

def whateven_chart(assigns) do
~H"""
... code here ...
"""
end
but I want to contraint data. It is a list of elements like
{{String.t(), String.t()}, flot(), float()}
{{String.t(), String.t()}, flot(), float()}
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
frankdugan3
frankdugan32mo ago
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.

Did you find this page helpful?