mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-11-20 00:20:59 +00:00
Add ObjectValidators.LanguageCode type
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
049045cf2a
commit
04c8f6b4d1
4 changed files with 59 additions and 6 deletions
|
@ -0,0 +1,25 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode do
|
||||||
|
use Ecto.Type
|
||||||
|
|
||||||
|
import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1]
|
||||||
|
|
||||||
|
def type, do: :string
|
||||||
|
|
||||||
|
def cast(language) when is_binary(language) do
|
||||||
|
if is_good_locale_code?(language) do
|
||||||
|
{:ok, language}
|
||||||
|
else
|
||||||
|
{:error, :invalid_language}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cast(_), do: :error
|
||||||
|
|
||||||
|
def dump(data), do: {:ok, data}
|
||||||
|
|
||||||
|
def load(data), do: {:ok, data}
|
||||||
|
end
|
|
@ -57,7 +57,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFields do
|
||||||
field(:replies_count, :integer, default: 0)
|
field(:replies_count, :integer, default: 0)
|
||||||
field(:like_count, :integer, default: 0)
|
field(:like_count, :integer, default: 0)
|
||||||
field(:announcement_count, :integer, default: 0)
|
field(:announcement_count, :integer, default: 0)
|
||||||
field(:language, :string)
|
field(:language, ObjectValidators.LanguageCode)
|
||||||
field(:inReplyTo, ObjectValidators.ObjectID)
|
field(:inReplyTo, ObjectValidators.ObjectID)
|
||||||
field(:url, ObjectValidators.BareUri)
|
field(:url, ObjectValidators.BareUri)
|
||||||
|
|
||||||
|
|
|
@ -9,17 +9,17 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.BareUriTest do
|
||||||
|
|
||||||
test "diaspora://" do
|
test "diaspora://" do
|
||||||
text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef"
|
text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef"
|
||||||
assert {:ok, text} = BareUri.cast(text)
|
assert {:ok, ^text} = BareUri.cast(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "nostr:" do
|
test "nostr:" do
|
||||||
text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
|
text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
|
||||||
assert {:ok, text} = BareUri.cast(text)
|
assert {:ok, ^text} = BareUri.cast(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "errors for non-URIs" do
|
test "errors for non-URIs" do
|
||||||
assert :error == SafeText.cast(1)
|
assert :error == BareUri.cast(1)
|
||||||
assert :error == SafeText.cast("foo")
|
assert :error == BareUri.cast("foo")
|
||||||
assert :error == SafeText.cast("foo bar")
|
assert :error == BareUri.cast("foo bar")
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCodeTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators.LanguageCode
|
||||||
|
|
||||||
|
test "it accepts language code" do
|
||||||
|
text = "pl"
|
||||||
|
assert {:ok, ^text} = LanguageCode.cast(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it accepts language code with region" do
|
||||||
|
text = "pl-PL"
|
||||||
|
assert {:ok, ^text} = LanguageCode.cast(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "errors for invalid language code" do
|
||||||
|
assert {:error, :invalid_language} = LanguageCode.cast("ru_RU")
|
||||||
|
assert {:error, :invalid_language} = LanguageCode.cast(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "errors for non-text" do
|
||||||
|
assert :error == LanguageCode.cast(42)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue