How to put Typespecs as items of a map

Hello, I need to store Typespecs as item of a map, like the blow code:

def system_events do
      %__MODULE__{name: :post_before_submit, allowed_input: String.t()}

Is there a way to save them as items of a map?

As far as I know, typespecs are an entirely compile time construct and do not exist at runtime. As such you can’t store them into a map. You can handle the AST representing typespecs in a macro (and put the AST in a map in a macro) the same as any AST, but that AST can’t be compiled in that position (as a value), only in @type(p) and @spec sections.

What do you want to do with this?


I want to check my user custom plugins that they want to add to my CMS, then I need to check users function outputs which is allowed or not. Then each event of my CMS events has own @spec

I just wanted to force them to create a plugin like a structure.

Do you mean plugins that are added at compile or runtime? Do you want to run Dialyzer on the user plugins?

1 Like

Something like Dialyzer at compile or runtime, if they break the structure; software notices them after runtime when the project is running, or break runtime and show them an error.

I’m not sure how difficult it would be to run Dialyzer on user supplied code. I think it’s doable but not sure if it’s reasonable. But can’t really help more on that part.

But no, you can’t store typespecs at runtime. You could store the AST and unquote it to attach it to the user supplied code, or something like that, but not really an expert on it.

1 Like

This is not how typespecs work, they are not a runtime data validation tool at all.


Yes, I got it.
I am trying to change my way, and also I have created many ‍‍callbacks, but I need to find a way to force my users to do my template.

This is my task to improve my elixir open-source CMS.

When inputs and outputs are valid, there is still uncertainty about how the plugin’s code affects the system. Like: does it write something unexpected on disk, or execute other applications?

There is no good solution for sandboxing Elixir to disallow selected functions. At the same time, there is one for sandboxed LUA running inside the Elixir GitHub - rvirding/luerl: Lua in Erlang with the support of functions blacklisting.


Do you provide your CMS as a service or as source code that users compile themselves?

In the first case it will be totally unsafe. In the latter case, I guess just providing well documented typespecs, callback specs and good documentation should be enough.

1 Like

Yes it is open-source, I changed my way and got an idea from, I am developing as a custom plugins system manager than I described it in link blow

Yes you are right, I created some optional and Necessary @callbacks

1 Like