Current section

Files

Jump to
gleam_validator gen src validator@string.erl
Raw

gen/src/validator@string.erl

-module(validator@string).
-compile(no_auto_import).
-export([is_not_empty/1, is_int/1, is_float/1, is_email/1, min_length/2, max_length/2]).
is_not_empty_check(Value) ->
case gleam@string:is_empty(Value) of
true ->
none;
false ->
{some, Value}
end.
is_not_empty(Error) ->
validator@common:custom(Error, fun is_not_empty_check/1).
is_int(Error) ->
validator@common:custom(
Error,
gleam@function:compose(
fun gleam@int:parse/1,
fun gleam@option:from_result/1
)
).
is_float(Error) ->
validator@common:custom(
Error,
gleam@function:compose(
fun gleam@float:parse/1,
fun gleam@option:from_result/1
)
).
is_email_check(Value) ->
{ok, Re} = gleam@regex:from_string(<<"^[\\w\\d]+@[\\w\\d\\.]+$"/utf8>>),
case gleam@regex:check(Re, Value) of
true ->
{some, Value};
false ->
none
end.
is_email(Error) ->
validator@common:custom(Error, fun is_email_check/1).
min_length_check(Min) ->
fun(Value) -> Len = gleam@string:length(Value),
case Len < 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) -> Len = gleam@string:length(Value),
case Len > Max of
true ->
none;
false ->
{some, Value}
end end.
max_length(Error, Max) ->
validator@common:custom(Error, max_length_check(Max)).