Current section

Files

Jump to
formz src formz@validation.erl
Raw

src/formz@validation.erl

-module(formz@validation).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export(['and'/2, email/1, int/1, number/1, list_item_by_index/1, non_empty_string/1, on/1, replace_error/2]).
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 28).
-spec 'and'(
fun((HSF) -> {ok, HSG} | {error, binary()}),
fun((HSG) -> {ok, HSJ} | {error, binary()})
) -> fun((HSF) -> {ok, HSJ} | {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", 61).
-spec email(binary()) -> {ok, binary()} | {error, binary()}.
email(Input) ->
case begin
_pipe = Input,
gleam@string:trim(_pipe)
end of
<<""/utf8>> ->
{error, <<"is required"/utf8>>};
Trimmed ->
case begin
_pipe@1 = Trimmed,
_pipe@2 = gleam@string:split(_pipe@1, <<"@"/utf8>>),
gleam@list:map(_pipe@2, fun gleam@string:length/1)
end of
[Before, After] when (Before > 0) andalso (After > 0) ->
{ok, Trimmed};
_ ->
{error, <<"must be an email address"/utf8>>}
end
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 117).
-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", 91).
-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", 144).
-spec list_item_by_index(list(HSU)) -> fun((binary()) -> {ok, HSU} |
{error, binary()}).
list_item_by_index(Variants) ->
fun(Str) -> _pipe = Variants,
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Val, I) -> {gleam@int:to_string(I), Val} end
),
_pipe@2 = gleam@list:key_find(_pipe@1, Str),
gleam@result:replace_error(_pipe@2, <<"must be an item in list"/utf8>>) end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 168).
-spec non_empty_string(binary()) -> {ok, binary()} | {error, binary()}.
non_empty_string(Str) ->
case gleam@string:trim(Str) of
<<""/utf8>> ->
{error, <<"is required"/utf8>>};
Trimmed ->
{ok, Trimmed}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 195).
-spec on(binary()) -> {ok, boolean()} | {error, binary()}.
on(Val) ->
case Val of
<<"on"/utf8>> ->
{ok, true};
_ ->
{error, <<"must be on"/utf8>>}
end.
-file("/Users/benjamin/Projects/formz/formz/src/formz/validation.gleam", 204).
-spec replace_error(fun((HTC) -> {ok, HTD} | {error, binary()}), binary()) -> fun((HTC) -> {ok,
HTD} |
{error, binary()}).
replace_error(Previous, Error) ->
fun(Data) -> _pipe = Previous(Data),
gleam@result:replace_error(_pipe, Error) end.