QuestionOptionsValidator: inline schema for replies

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-06-12 21:22:05 +02:00
parent c5efaf6b00
commit 89a2433154
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
1 changed files with 9 additions and 19 deletions

View File

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