Skip to content

Commit c74701b

Browse files
committedOct 30, 2024·
add basic profanity filter
1 parent bf83546 commit c74701b

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed
 

‎lib/live_fridge/fridge/word.ex

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ defmodule LiveFridge.Fridge.Word do
1313
change LiveFridge.Changes.SetColor, on: :create
1414
end
1515

16+
validations do
17+
validate {LiveFridge.Validations.IsProfanity, []}, on: :create
18+
end
19+
1620
attributes do
1721
uuid_v7_primary_key :id
1822

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule LiveFridge.Validations.IsProfanity do
2+
use Ash.Resource.Validation
3+
4+
@impl true
5+
def init(opts) do
6+
{:ok, opts}
7+
end
8+
9+
@impl true
10+
def validate(changeset, _opts, _context) do
11+
config = Expletive.configure(blacklist: Expletive.Blacklist.english())
12+
word = Ash.Changeset.get_attribute(changeset, :word)
13+
14+
if Expletive.profane?(word, config) do
15+
{:error, field: :word, message: "Nasty language, be nice!"}
16+
else
17+
:ok
18+
end
19+
end
20+
21+
@impl true
22+
def atomic(changeset, opts, context) do
23+
validate(changeset, opts, context)
24+
end
25+
end

‎lib/live_fridge_web/live/fridge_live/index.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ defmodule LiveFridgeWeb.FridgeLive.Index do
6868
new_word_form: new_form()
6969
)}
7070

71-
{:error, _changeset} ->
71+
{:error, changeset} ->
7272
{:noreply,
7373
socket
7474
|> assign(new_word_form: new_form())
75-
|> put_flash(:error, "Failed to add word")}
75+
|> put_flash(:error, "Failed to add word: #{Enum.map_join(changeset.errors, ", ", &(&1.message))}")}
7676
end
7777
end
7878

‎mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ defmodule LiveFridge.MixProject do
3333
# Type `mix help deps` for examples and options.
3434
defp deps do
3535
[
36+
{:expletive, "~> 0.1"},
3637
{:ash, "~> 3.0"},
3738
{:igniter, "~> 0.3"},
3839
{:phoenix, "~> 1.7.14"},

‎mix.lock

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"},
1010
"esbuild": {:hex, :esbuild, "0.8.2", "5f379dfa383ef482b738e7771daf238b2d1cfb0222bef9d3b20d4c8f06c7a7ac", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "558a8a08ed78eb820efbfda1de196569d8bfa9b51e8371a1934fbb31345feda7"},
1111
"ets": {:hex, :ets, "0.9.0", "79c6a6c205436780486f72d84230c6cba2f8a9920456750ddd1e47389107d5fd", [:mix], [], "hexpm", "2861fdfb04bcaeff370f1a5904eec864f0a56dcfebe5921ea9aadf2a481c822b"},
12+
"expletive": {:hex, :expletive, "0.1.5", "9e29db97aa83309b169870a1c08554a214561a04dc3cc38476141f3c62bd27b7", [:mix], [], "hexpm", "f07359b0b4b8cc39001a688d080373aa05d10ad294f0701b96d9169831ac22fd"},
1213
"expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"},
1314
"file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"},
1415
"finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"},

0 commit comments

Comments
 (0)
Please sign in to comment.