Skip to content

Commit 4a78825

Browse files
committed
improvement: verify pub_sub actions at compile time
1 parent f2cb11c commit 4a78825

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Diff for: lib/ash/notifier/pub_sub/pub_sub.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ defmodule Ash.Notifier.PubSub do
201201
- `:broadcast` sends `%{topic: (topic), event: (event), notification: (notification)}`
202202
"""
203203

204-
use Spark.Dsl.Extension, sections: @sections
204+
use Spark.Dsl.Extension,
205+
sections: @sections,
206+
verifiers: [Ash.Notifier.PubSub.Verifiers.VerifyActionNames]
205207

206208
use Ash.Notifier
207209

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule Ash.Notifier.PubSub.Verifiers.VerifyActionNames do
2+
use Spark.Dsl.Verifier
3+
4+
def verify(dsl) do
5+
module =
6+
Spark.Dsl.Verifier.get_persisted(dsl, :module)
7+
8+
for publication <- Ash.Notifier.PubSub.Info.publications(dsl),
9+
not is_nil(publication.action) do
10+
action = Ash.Resource.Info.action(dsl, publication.action)
11+
12+
if !action do
13+
raise Spark.Error.DslError,
14+
path: [:pub_sub, :publish, publication.action],
15+
module: module,
16+
message: """
17+
Non-existent action #{inspect(module)}.#{publication.action} referenced in `pub_sub` notifier.
18+
"""
19+
end
20+
21+
if action.type == :action do
22+
raise Spark.Error.DslError,
23+
path: [:pub_sub, :publish, publication.action],
24+
module: module,
25+
message: """
26+
Invalid action #{inspect(module)}.#{publication.action} referenced in `pub_sub` notifier.
27+
28+
Only create, update and destroy actions emit notifications, not generic actions.
29+
"""
30+
end
31+
end
32+
33+
:ok
34+
end
35+
end

0 commit comments

Comments
 (0)