New type: `:existing_module`
am-kantox opened this issue · comments
I find myself copy-pasting the following implementation to be used to validate a dependency injection via module:
@doc false
def existing_module?(value) do
case Code.ensure_compiled(value) do
{:module, ^value} ->
{:ok, value}
{:error, error} ->
{:error, "Cannot find the requested module ‹" <> inspect(value) <> "› (#{error})"}
end
end
and use it in options schema declaration as
{:custom, MyValidators, :existing_module?, []}
I believe it’d be a great candidate for another default type. I am all-in to provide a PR if this would be desired.
I'm in favor of this 👍. Would love a PR, yes please! 💟
@whatyouhide I am not sure if this makes sense actually.
first, why ensure_compiled? Would it always be a compile time dependency?
then, often you want to the module to expose some API, so wouldn’t you most likely need to check it with function_exported? and need custom code anyway?
@josevalim the intent was to establish a compile-time dependency, yes.
The check for exported API might be needed, but at least it narrows down errors induced by typos.
After thinking more about this, I do think that this is probably out of scope for this library. You can always write a custom validator and do these checks in there. Thanks anyways @am-kantox! 💟