QuestionOptionsValidator: inline schema for replies
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Fri, 12 Jun 2020 19:22:05 +0000 (21:22 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Wed, 15 Jul 2020 09:39:54 +0000 (11:39 +0200)
lib/pleroma/web/activity_pub/object_validators/question_options_validator.ex

index 9bc7e0cc0886cf5d25d21cc1cedd8a3c65471bb5..8291d7b9fb23debe879dbea69475d810d2983dfe 100644 (file)
@@ -5,42 +5,32 @@
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
   use Ecto.Schema
 
-  alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsRepliesValidator
-
   import Ecto.Changeset
 
   @primary_key false
 
   embedded_schema do
     field(:name, :string)
-    embeds_one(:replies, QuestionOptionsRepliesValidator)
+
+    embeds_one :replies, Replies do
+      field(:totalItems, :integer)
+      field(:type, :string)
+    end
+
     field(:type, :string)
   end
 
   def changeset(struct, data) do
     struct
     |> cast(data, [:name, :type])
-    |> cast_embed(:replies)
+    |> cast_embed(:replies, with: &replies_changeset/2)
     |> validate_inclusion(:type, ["Note"])
     |> validate_required([:name, :type])
   end
-end
-
-defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsRepliesValidator do
-  use Ecto.Schema
-
-  import Ecto.Changeset
 
-  @primary_key false
-
-  embedded_schema do
-    field(:totalItems, :integer)
-    field(:type, :string)
-  end
-
-  def changeset(struct, data) do
+  def replies_changeset(struct, data) do
     struct
-    |> cast(data, __schema__(:fields))
+    |> cast(data, [:totalItems, :type])
     |> validate_inclusion(:type, ["Collection"])
     |> validate_required([:type])
   end