Current section

Files

Jump to
frontier lib frontier validation.ex
Raw

lib/frontier/validation.ex

defmodule Frontier.Validation do
@moduledoc "Validates Frontier config after compilation — catches bad reaches, exports, reclassifications, and public_schemas entries."
alias Frontier.Store
@doc "Validates all registered Frontier config. Returns a list of diagnostic-like maps."
@spec validate :: [%{message: String.t(), module: module()}]
def validate do
validate_reaches() ++
validate_exports() ++ validate_reclassified() ++ validate_public_schemas()
end
defp validate_reaches do
contexts = Store.contexts()
for {module, config} <- contexts,
reaches = config.reaches,
is_list(reaches),
target <- reaches,
not Map.has_key?(contexts, target) do
%{
message:
"#{inspect(target)} is listed in reaches: for #{inspect(module)}, but is not a declared Frontier context",
module: module
}
end
end
defp validate_exports do
contexts = Store.contexts()
for {module, config} <- contexts,
is_list(config.exports),
export <- config.exports,
is_atom(export),
not Code.ensure_loaded?(export) do
short = String.replace(inspect(export), inspect(module) <> ".", "")
%{
message:
"#{short} is listed in exports: for #{inspect(module)}, but the module does not exist",
module: module
}
end
end
defp validate_reclassified do
contexts = Store.contexts()
reclassified = Store.all_reclassified()
for {module, target} <- reclassified,
not Map.has_key?(contexts, target) do
%{
message:
"#{inspect(module)} has belongs_to: #{inspect(target)}, but #{inspect(target)} is not a declared Frontier context",
module: module
}
end
end
defp validate_public_schemas do
contexts = Store.contexts()
for {module, config} <- contexts,
is_list(config[:public_schemas]),
schema <- config.public_schemas,
is_atom(schema),
not (Code.ensure_loaded?(schema) and function_exported?(schema, :__schema__, 1)) do
short = String.replace(inspect(schema), inspect(module) <> ".", "")
%{
message:
"#{short} is listed in public_schemas: for #{inspect(module)}, but the module does not exist or is not a schema",
module: module
}
end
end
end