-module(apollo@filter). -compile([no_auto_import, nowarn_unused_vars]). -export([custom/2, custom_string/2, length/2, min/2, max/2, regex/2]). -spec custom( apollo@t:value(), fun((apollo@util:any_type()) -> {ok, nil} | {error, binary()}) ) -> apollo@t:value(). custom(V, Validator) -> apollo_t:custom(V, Validator). -spec custom_string( apollo@t:value(), fun((binary()) -> {ok, nil} | {error, binary()}) ) -> apollo@t:value(). custom_string(V, Validator) -> apollo_t:custom(V, fun(Value) -> case Value of {string_t, T} -> Validator(T); _ -> {error, <<"Type mismatch, expecting String or Integer"/utf8>>} end end). -spec length(apollo@t:value(), integer()) -> apollo@t:value(). length(V, L) -> apollo_t:custom(V, fun(Value) -> case Value of {string_t, T} -> apollo@util:to_err( gleam@string:length(T) =:= L, <<<<<<"Length of \""/utf8, T/binary>>/binary, "\" is not equal to "/utf8>>/binary, (gleam@int:to_string(L))/binary>> ); {int_t, T@1} -> Stringed_value = gleam@int:to_string(T@1), apollo@util:to_err( gleam@string:length(Stringed_value) =:= L, <<<<<<"Length of \""/utf8, Stringed_value/binary>>/binary, "\" is not equal to "/utf8>>/binary, (gleam@int:to_string(L))/binary>> ); _ -> {error, <<"Type mismatch, expecting String or Integer"/utf8>>} end end). -spec min(apollo@t:value(), integer()) -> apollo@t:value(). min(V, L) -> apollo_t:custom(V, fun(Value) -> case Value of {string_t, T} -> apollo@util:to_err( gleam@string:length(T) >= L, <<<<<<"Length of \""/utf8, T/binary>>/binary, "\" is less than than "/utf8>>/binary, (gleam@int:to_string(L))/binary>> ); _ -> {error, <<"Type mismatch, expecting String"/utf8>>} end end). -spec max(apollo@t:value(), integer()) -> apollo@t:value(). max(V, L) -> custom_string( V, fun(T) -> apollo@util:to_err( gleam@string:length(T) =< L, <<<<<<"Length of \""/utf8, T/binary>>/binary, "\" is greater than "/utf8>>/binary, (gleam@int:to_string(L))/binary>> ) end ). -spec regex(apollo@t:value(), gleam@regex:regex()) -> apollo@t:value(). regex(V, Re) -> custom_string( V, fun(T) -> apollo@util:to_err( gleam@regex:check(Re, T), <<<<"\""/utf8, T/binary>>/binary, "\" not matched by the regular expression"/utf8>> ) end ).