summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/activity_pub/object_validators/question_options_validator.ex
blob: 541945fa407b6a8a2d9f236abddfe1af4b923f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key false

  embedded_schema do
    field(:name, :string)

    embeds_one :replies, Replies, primary_key: false 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, with: &replies_changeset/2)
    |> validate_inclusion(:type, ["Note"])
    |> validate_required([:name, :type])
  end

  def replies_changeset(struct, data) do
    struct
    |> cast(data, [:totalItems, :type])
    |> validate_inclusion(:type, ["Collection"])
    |> validate_required([:type])
  end
end