Current section
Files
Jump to
Current section
Files
gen/src/validator@list.erl
-module(validator@list).
-compile(no_auto_import).
-export([is_not_empty/1, min_length/2, max_length/2, every/1]).
is_not_empty_check(Value) ->
case gleam@list:is_empty(Value) of
true ->
none;
false ->
{some, Value}
end.
is_not_empty(Error) ->
validator@common:custom(Error, fun is_not_empty_check/1).
min_length_check(Min) ->
fun(Value) -> case gleam@list:length(Value) < Min of
true ->
none;
false ->
{some, Value}
end end.
min_length(Error, Min) ->
validator@common:custom(Error, min_length_check(Min)).
max_length_check(Max) ->
fun(Value) -> case gleam@list:length(Value) > Max of
true ->
none;
false ->
{some, Value}
end end.
max_length(Error, Max) ->
validator@common:custom(Error, max_length_check(Max)).
every(Validator) ->
fun(Items) -> Results = gleam@list:map(Items, Validator),
Errors = gleam@list:flatten(
gleam@list:map(Results, fun(Result) -> case Result of
{ok, _} ->
[];
{error, {First, Rest}} ->
Rest
end end)
),
OkItems = gleam@list:filter_map(Results, fun gleam@function:identity/1),
case gleam@list:head(Errors) of
{error, nil} ->
{ok, OkItems};
{ok, Head} ->
{error, {Head, Errors}}
end end.