QuestionValidator: Create
[akkoma] / lib / pleroma / web / activity_pub / object_validators / question_options_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
6 use Ecto.Schema
7
8 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsRepliesValidator
9
10 import Ecto.Changeset
11
12 @primary_key false
13
14 embedded_schema do
15 field(:name, :string)
16 embeds_one(:replies, QuestionOptionsRepliesValidator)
17 field(:type, :string)
18 end
19
20 def changeset(struct, data) do
21 struct
22 |> cast(data, [:name, :type])
23 |> cast_embed(:replies)
24 |> validate_inclusion(:type, ["Note"])
25 |> validate_required([:name, :type])
26 end
27 end
28
29 defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsRepliesValidator do
30 use Ecto.Schema
31
32 import Ecto.Changeset
33
34 @primary_key false
35
36 embedded_schema do
37 field(:totalItems, :integer)
38 field(:type, :string)
39 end
40
41 def changeset(struct, data) do
42 struct
43 |> cast(data, __schema__(:fields))
44 |> validate_inclusion(:type, ["Collection"])
45 |> validate_required([:type])
46 end
47 end