Current section

Files

Jump to
valguard src valguard@val.erl
Raw

src/valguard@val.erl

-module(valguard@val).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/valguard/val.gleam").
-export([int_required/1, int_min/2, int_max/2, float_required/1, float_min/2, float_max/2, string_required/1, string_min/2, string_max/2, date_is_valid/1, email_is_valid/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/valguard/val.gleam", 15).
?DOC(" Requires a int to be less than or greater than 0 to be considered required\n").
-spec int_required(integer()) -> {ok, nil} | {error, binary()}.
int_required(Value) ->
case Value /= 0 of
true ->
{ok, nil};
false ->
{error, <<"This field is required"/utf8>>}
end.
-file("src/valguard/val.gleam", 24).
?DOC(" Validates an int is at least a minimum value. Returns an error if value is less than the minimum\n").
-spec int_min(integer(), integer()) -> {ok, nil} | {error, binary()}.
int_min(Value, Minimum) ->
case Value < Minimum of
true ->
{error,
<<"Value must be a minimum of "/utf8,
(erlang:integer_to_binary(Minimum))/binary>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 33).
?DOC(" Validates an int is at most a maximum value. Returns an error if value is greater than the maximum\n").
-spec int_max(integer(), integer()) -> {ok, nil} | {error, binary()}.
int_max(Value, Maximum) ->
case Value > Maximum of
true ->
{error,
<<"Value must be a maximum of "/utf8,
(erlang:integer_to_binary(Maximum))/binary>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 42).
?DOC(" Requires a float to be less than or greater than 0.0 to be considered required\n").
-spec float_required(float()) -> {ok, nil} | {error, binary()}.
float_required(Value) ->
case (Value > +0.0) orelse (Value < +0.0) of
true ->
{ok, nil};
false ->
{error, <<"This field is required"/utf8>>}
end.
-file("src/valguard/val.gleam", 51).
?DOC(" Validates an float is at least a minimum value. Returns an error if value is less than the minimum\n").
-spec float_min(float(), float()) -> {ok, nil} | {error, binary()}.
float_min(Value, Minimum) ->
case Value < Minimum of
true ->
{error,
<<"Value must be a minimum of "/utf8,
(gleam_stdlib:float_to_string(Minimum))/binary>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 60).
?DOC(" Validates an float is at most a maximum value. Returns an error if value is greater than the maximum\n").
-spec float_max(float(), float()) -> {ok, nil} | {error, binary()}.
float_max(Value, Maximum) ->
case Value > Maximum of
true ->
{error,
<<"Value must be a maximum of "/utf8,
(gleam_stdlib:float_to_string(Maximum))/binary>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 69).
?DOC(" Requires a string to not be empty to be considered required\n").
-spec string_required(binary()) -> {ok, nil} | {error, binary()}.
string_required(Value) ->
case gleam@string:is_empty(Value) of
false ->
{ok, nil};
true ->
{error, <<"This field is required"/utf8>>}
end.
-file("src/valguard/val.gleam", 78).
?DOC(" Validates a string is a minimum length\n").
-spec string_min(binary(), integer()) -> {ok, nil} | {error, binary()}.
string_min(Value, Minimum) ->
case string:length(Value) < Minimum of
true ->
{error,
<<<<"Value must be a minimum of "/utf8,
(erlang:integer_to_binary(Minimum))/binary>>/binary,
" characters"/utf8>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 90).
?DOC(" Validates a string is at most a maximum length\n").
-spec string_max(binary(), integer()) -> {ok, nil} | {error, binary()}.
string_max(Value, Maximum) ->
case string:length(Value) > Maximum of
true ->
{error,
<<<<"Value must be a maximum of "/utf8,
(erlang:integer_to_binary(Maximum))/binary>>/binary,
" characters"/utf8>>};
false ->
{ok, nil}
end.
-file("src/valguard/val.gleam", 118).
?DOC(" Validates that a date is valid by checking it can be parsed correctly\n").
-spec date_is_valid(binary()) -> {ok, nil} | {error, binary()}.
date_is_valid(Datetime) ->
case gleam@time@timestamp:parse_rfc3339(Datetime) of
{ok, _} ->
{ok, nil};
{error, _} ->
{error, <<"A valid date is required"/utf8>>}
end.
-file("src/valguard/val.gleam", 102).
?DOC(" Validates if entered email is a valid email address\n").
-spec email_is_valid(binary()) -> {ok, nil} | {error, binary()}.
email_is_valid(Email) ->
Error = <<"Email address is not valid"/utf8>>,
gleam@result:'try'(
begin
_pipe = gleam@regexp:from_string(
<<"^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"/utf8>>
),
gleam@result:replace_error(_pipe, Error)
end,
fun(Re) -> case gleam@regexp:check(Re, Email) of
false ->
{error, Error};
true ->
{ok, nil}
end end
).