improvement: warn on api missing from config

This commit is contained in:
Zach Daniel 2023-07-13 08:42:52 -04:00
parent 9987cb05e8
commit 94815605b9
5 changed files with 47 additions and 3 deletions

View file

@ -28,4 +28,5 @@ if Mix.env() == :test do
]
config :ash, :validate_api_resource_inclusion?, false
config :ash, :validate_api_config_inclusion?, false
end

View file

@ -36,7 +36,14 @@ defmodule Ash.Api do
Additionally, you can define a `code_interface` on each resource. See the code interface guide for more.
"""
use Spark.Dsl, default_extensions: [extensions: [Ash.Api.Dsl]]
use Spark.Dsl,
default_extensions: [extensions: [Ash.Api.Dsl]],
opt_schema: [
validate_config_inclusion?: [
type: :boolean,
default: true
]
]
import Spark.OptionsHelpers, only: [merge_schemas: 3]
@ -1398,6 +1405,36 @@ defmodule Ash.Api do
end
end
@impl true
def verify(module, opts) do
if Application.get_env(:ash, :validate_api_config_inclusion?, true) &&
Keyword.get(opts, :validate_config_inclusion?, true) do
otp_app = Mix.Project.config()[:app]
apis =
Application.get_env(otp_app, :ash_apis, [])
if module not in apis do
IO.warn("""
Api #{inspect(module)} is not present in
config :#{otp_app}, ash_apis: #{inspect(apis)}.
To resolve this warning, do one of the following.
1. Add the api to your configured api modules. The following snippet can be used.
config :#{otp_app}, ash_apis: #{inspect(apis ++ [module])}
2. Add the option `validate_config_inclusion?: false` to `use Ash.Api`
3. Configure all apis not to warn, with `config :ash, :validate_api_config_inclusion?, false`
""")
end
end
end
@doc false
# sobelow_skip ["DOS.StringToAtom"]
@impl Spark.Dsl

View file

@ -61,7 +61,7 @@ defmodule Ash.EmbeddableType do
defmodule ShadowApi do
@moduledoc false
use Ash.Api
use Ash.Api, validate_config_inclusion?: false
resources do
allow_unregistered?(true)

View file

@ -192,7 +192,7 @@ defmodule Ash.Filter do
# Used for fetching related data in filters, which will have already had authorization rules applied
defmodule ShadowApi do
@moduledoc false
use Ash.Api
use Ash.Api, validate_config_inclusion?: false
resources do
allow_unregistered?(true)

View file

@ -17,6 +17,12 @@ defmodule Ash.Resource do
default_extensions: [
data_layer: Ash.DataLayer.Simple,
extensions: [Ash.Resource.Dsl]
],
opt_schema: [
validate_api_inclusion?: [
type: :boolean,
default: true
]
]
@doc false