···
defmodule Atex.Lexicon.Validators.String do
alias Atex.Lexicon.Validators
5
+
{:format, String.t()}
| {:min_length, non_neg_integer()}
| {:max_length, non_neg_integer()}
| {:min_graphemes, non_neg_integer()}
···
@record_key_re ~r"^[a-zA-Z0-9.-_:~]$"
34
-
# TODO: probably should go into a different module, one with general lexicon -> validator gen conversions
35
-
@spec format_to_atom(String.t()) :: format()
36
-
def format_to_atom(format) do
38
-
"at-identifier" -> :at_identifier
41
-
"datetime" -> :datetime
46
-
"record-key" -> :record_key
48
-
"language" -> :language
49
-
_ -> raise "Unknown lexicon string format `#{format}`"
@spec validate(term(), list(option())) :: Peri.validation_result()
def validate(value, options) when is_binary(value) do
···
defp validate_option(_value, {option, nil}) when option in @option_keys, do: :ok
77
-
defp validate_option(value, {:format, :at_identifier}),
45
+
defp validate_option(value, {:format, "at-identifier"}),
Validators.boolean_validate(
Atex.DID.match?(value) or Atex.Handle.match?(value),
"should be a valid DID or handle"
84
-
defp validate_option(value, {:format, :at_uri}),
52
+
defp validate_option(value, {:format, "at-uri"}),
do: Validators.boolean_validate(Atex.AtURI.match?(value), "should be a valid at:// URI")
87
-
defp validate_option(value, {:format, :cid}) do
55
+
defp validate_option(value, {:format, "cid"}) do
# TODO: is there a regex provided by the lexicon docs/somewhere?
Multiformats.CID.decode(value)
···
96
-
defp validate_option(value, {:format, :datetime}) do
64
+
defp validate_option(value, {:format, "datetime"}) do
# NaiveDateTime is used over DateTime because the result isn't actually
# being used, so we don't need to include a calendar library just for this.
case NaiveDateTime.from_iso8601(value) do
···
105
-
defp validate_option(value, {:format, :did}),
73
+
defp validate_option(value, {:format, "did"}),
do: Validators.boolean_validate(Atex.DID.match?(value), "should be a valid DID")
108
-
defp validate_option(value, {:format, :handle}),
76
+
defp validate_option(value, {:format, "handle"}),
do: Validators.boolean_validate(Atex.Handle.match?(value), "should be a valid handle")
111
-
defp validate_option(value, {:format, :nsid}),
79
+
defp validate_option(value, {:format, "nsid"}),
do: Validators.boolean_validate(Atex.NSID.match?(value), "should be a valid NSID")
114
-
defp validate_option(value, {:format, :tid}),
82
+
defp validate_option(value, {:format, "tid"}),
do: Validators.boolean_validate(Atex.TID.match?(value), "should be a valid TID")
117
-
defp validate_option(value, {:format, :record_key}),
85
+
defp validate_option(value, {:format, "record-key"}),
Validators.boolean_validate(
Regex.match?(@record_key_re, value),
"should be a valid record key"
124
-
defp validate_option(value, {:format, :uri}) do
92
+
defp validate_option(value, {:format, "uri"}) do
{:error, _} -> {:error, "should be a valid URI", []}
131
-
defp validate_option(value, {:format, :language}) do
99
+
defp validate_option(value, {:format, "language"}) do
case Cldr.LanguageTag.parse(value) do
{:error, _} -> {:error, "should be a valid BCP 47 language tag", []}