summaryrefslogtreecommitdiff
path: root/lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex
blob: b8e5c9dad9c4180f9e90aa9d650f6ef3f051000b (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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.Uri do
  use Ecto.Type

  def type, do: :string

  def cast(uri) when is_binary(uri) do
    case URI.parse(uri) do
      %URI{host: nil} -> :error
      %URI{host: ""} -> :error
      %URI{scheme: scheme} when scheme in ["https", "http"] -> {:ok, uri}
      _ -> :error
    end
  end

  def cast(_), do: :error

  def dump(data), do: {:ok, data}

  def load(data), do: {:ok, data}
end