summaryrefslogtreecommitdiff
path: root/lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex')
-rw-r--r--lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex b/lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex
new file mode 100644
index 000000000..2054c26be
--- /dev/null
+++ b/lib/pleroma/ecto_type/activity_pub/object_validators/uri.ex
@@ -0,0 +1,24 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 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