Current section
Files
Jump to
Current section
Files
src/formz@validation.erl
-module(formz@validation).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([string/1, email/1, int/1, must_be_longer_than/1, number/1, enum/1, enum_by_index/1, boolean/1, true/1, 'and'/2, 'or'/2, replace_error/2]).
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 8).
-spec string(binary()) -> {ok, binary()} | {error, binary()}.
string(Str) ->
case gleam@string:trim(Str) of
<<""/utf8>> ->
{error, <<"Must not be empty"/utf8>>};
Trimmed ->
{ok, Trimmed}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 15).
-spec email(binary()) -> {ok, binary()} | {error, binary()}.
email(Input) ->
case begin
_pipe = Input,
_pipe@1 = gleam@string:trim(_pipe),
gleam@string:split(_pipe@1, <<"@"/utf8>>)
end of
[_, _] ->
{ok, Input};
_ ->
{error, <<"Must be an email address"/utf8>>}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 32).
-spec int(binary()) -> {ok, integer()} | {error, binary()}.
int(Str) ->
_pipe = Str,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = gleam@int:parse(_pipe@1),
gleam@result:replace_error(_pipe@2, <<"Must be a whole number"/utf8>>).
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 23).
-spec must_be_longer_than(integer()) -> fun((binary()) -> {ok, binary()} |
{error, binary()}).
must_be_longer_than(Length) ->
fun(Input) -> case gleam@string:length(Input) > Length of
true ->
{ok, Input};
false ->
{error,
<<"Must be longer than "/utf8,
(gleam@int:to_string(Length))/binary>>}
end end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 39).
-spec number(binary()) -> {ok, float()} | {error, binary()}.
number(Str) ->
Str@1 = gleam@string:trim(Str),
_pipe@1 = case gleam@float:parse(Str@1) of
{ok, Value} ->
{ok, Value};
{error, _} ->
_pipe = gleam@int:parse(Str@1),
gleam@result:map(_pipe, fun gleam@int:to_float/1)
end,
gleam@result:replace_error(_pipe@1, <<"Must be a number"/utf8>>).
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 76).
-spec map_error_for_list(list({binary(), any()})) -> fun((any()) -> binary()).
map_error_for_list(Variants) ->
fun(_) -> case Variants of
[] ->
<<"No allowed values"/utf8>>;
[A] ->
<<"Must be "/utf8,
(begin
_pipe = A,
gleam@pair:first(_pipe)
end)/binary>>;
[_, _] ->
<<"Must be one of "/utf8,
(begin
_pipe@1 = gleam@list:map(
Variants,
fun gleam@pair:first/1
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end)/binary>>;
[_, _, _] ->
<<"Must be one of "/utf8,
(begin
_pipe@1 = gleam@list:map(
Variants,
fun gleam@pair:first/1
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end)/binary>>;
[_, _, _, _] ->
<<"Must be one of "/utf8,
(begin
_pipe@1 = gleam@list:map(
Variants,
fun gleam@pair:first/1
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end)/binary>>;
[A@1, B, C, D, _ | _] ->
<<<<"Must be one of "/utf8,
(begin
_pipe@2 = gleam@list:map(
[A@1, B, C, D],
fun gleam@pair:first/1
),
gleam@string:join(_pipe@2, <<", "/utf8>>)
end)/binary>>/binary,
"..."/utf8>>
end end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 48).
-spec enum(list({binary(), HHB})) -> fun((binary()) -> {ok, HHB} |
{error, binary()}).
enum(Variants) ->
fun(Str) -> _pipe = Variants,
_pipe@1 = gleam@list:filter_map(
_pipe,
fun(T) -> case gleam@string:inspect(erlang:element(2, T)) =:= Str of
true ->
{ok, erlang:element(2, T)};
false ->
{error, nil}
end end
),
_pipe@2 = gleam@list:first(_pipe@1),
gleam@result:map_error(_pipe@2, map_error_for_list(Variants)) end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 64).
-spec enum_by_index(list({binary(), HHF})) -> fun((binary()) -> {ok, HHF} |
{error, binary()}).
enum_by_index(Variants) ->
fun(Str) ->
Vals_indexed = gleam@list:index_map(
Variants,
fun(T, I) -> {gleam@int:to_string(I), erlang:element(2, T)} end
),
_pipe = gleam@list:key_find(Vals_indexed, Str),
gleam@result:map_error(_pipe, map_error_for_list(Variants))
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 91).
-spec boolean(binary()) -> {ok, boolean()} | {error, binary()}.
boolean(Str) ->
case gleam@string:trim(Str) of
<<"True"/utf8>> ->
{ok, true};
<<"true"/utf8>> ->
{ok, true};
<<"Yes"/utf8>> ->
{ok, true};
<<"yes"/utf8>> ->
{ok, true};
<<"On"/utf8>> ->
{ok, true};
<<"on"/utf8>> ->
{ok, true};
<<"1"/utf8>> ->
{ok, true};
<<"False"/utf8>> ->
{ok, false};
<<"false"/utf8>> ->
{ok, false};
<<"No"/utf8>> ->
{ok, false};
<<"no"/utf8>> ->
{ok, false};
<<"Off"/utf8>> ->
{ok, false};
<<"off"/utf8>> ->
{ok, false};
<<"0"/utf8>> ->
{ok, false};
<<""/utf8>> ->
{ok, false};
_ ->
{error, <<"Must be true or false"/utf8>>}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 112).
-spec true(binary()) -> {ok, boolean()} | {error, binary()}.
true(Str) ->
case boolean(Str) of
{ok, true} ->
{ok, true};
_ ->
{error, <<"Must be true"/utf8>>}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 119).
-spec 'and'(
fun((HHP) -> {ok, HHQ} | {error, binary()}),
fun((HHQ) -> {ok, HHT} | {error, binary()})
) -> fun((HHP) -> {ok, HHT} | {error, binary()}).
'and'(Previous, Next) ->
fun(Data) -> case Previous(Data) of
{ok, Value} ->
Next(Value);
{error, Error} ->
{error, Error}
end end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 131).
-spec 'or'(
fun((HHY) -> {ok, HHZ} | {error, binary()}),
fun((HHY) -> {ok, HHZ} | {error, binary()})
) -> fun((HHY) -> {ok, HHZ} | {error, binary()}).
'or'(Previous, Next) ->
fun(Data) -> case Previous(Data) of
{ok, Value} ->
{ok, Value};
{error, Err1} ->
case Next(Data) of
{ok, Value@1} ->
{ok, Value@1};
{error, Err2} ->
{error,
<<<<Err1/binary, " or "/utf8>>/binary, Err2/binary>>}
end
end end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 147).
-spec replace_error(fun((HIG) -> {ok, HIH} | {error, binary()}), binary()) -> fun((HIG) -> {ok,
HIH} |
{error, binary()}).
replace_error(Previous, Error) ->
fun(Data) -> _pipe = Previous(Data),
gleam@result:replace_error(_pipe, Error) end.