Current section

Files

Jump to
caffeine_lang src caffeine_lang@common@validations.erl
Raw

src/caffeine_lang@common@validations.erl

-module(caffeine_lang@common@validations).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/common/validations.gleam").
-export([validate_value_type/3, inputs_validator/3, validate_relevant_uniqueness/3, validate_inputs_for_collection/4, check_collection_key_overshadowing/3]).
-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/caffeine_lang/common/validations.gleam", 15).
?DOC(false).
-spec validate_value_type(
gleam@dynamic:dynamic_(),
caffeine_lang@common@accepted_types:accepted_types(),
binary()
) -> {ok, gleam@dynamic:dynamic_()} |
{error, caffeine_lang@common@errors:compilation_error()}.
validate_value_type(Value, Expected_type, Type_key_identifier) ->
_pipe = caffeine_lang@common@accepted_types:validate_value(
Expected_type,
Value
),
gleam@result:map_error(
_pipe,
fun(Err) ->
{parser_json_parser_error,
caffeine_lang@common@errors:format_decode_error_message(
Err,
{some, Type_key_identifier}
)}
end
).
-file("src/caffeine_lang/common/validations.gleam", 33).
?DOC(false).
-spec inputs_validator(
gleam@dict:dict(binary(), caffeine_lang@common@accepted_types:accepted_types()),
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
boolean()
) -> {ok, boolean()} | {error, binary()}.
inputs_validator(Params, Inputs, Missing_inputs_ok) ->
Required_params = begin
_pipe = Params,
gleam@dict:filter(_pipe, fun(_, Typ) -> case Typ of
{modifier_type, {optional, _}} ->
false;
{modifier_type, {defaulted, _, _}} ->
false;
_ ->
true
end end)
end,
Required_param_keys = begin
_pipe@1 = Required_params,
_pipe@2 = maps:keys(_pipe@1),
gleam@set:from_list(_pipe@2)
end,
Param_keys = begin
_pipe@3 = Params,
_pipe@4 = maps:keys(_pipe@3),
gleam@set:from_list(_pipe@4)
end,
Input_keys = begin
_pipe@5 = Inputs,
_pipe@6 = maps:keys(_pipe@5),
gleam@set:from_list(_pipe@6)
end,
Missing_required_keys = begin
_pipe@7 = gleam@set:difference(Required_param_keys, Input_keys),
gleam@set:to_list(_pipe@7)
end,
Keys_only_in_inputs = begin
_pipe@8 = gleam@set:difference(Input_keys, Param_keys),
gleam@set:to_list(_pipe@8)
end,
gleam@result:'try'(
case {Missing_required_keys, Keys_only_in_inputs, Missing_inputs_ok} of
{[], [], _} ->
{ok, true};
{_, [], true} ->
{ok, true};
{_, [], false} ->
{error,
<<"Missing keys in input: "/utf8,
(begin
_pipe@9 = Missing_required_keys,
gleam@string:join(_pipe@9, <<", "/utf8>>)
end)/binary>>};
{[], _, _} ->
{error,
<<"Extra keys in input: "/utf8,
(begin
_pipe@10 = Keys_only_in_inputs,
gleam@string:join(_pipe@10, <<", "/utf8>>)
end)/binary>>};
{_, _, true} ->
{error,
<<"Extra keys in input: "/utf8,
(begin
_pipe@11 = Keys_only_in_inputs,
gleam@string:join(_pipe@11, <<", "/utf8>>)
end)/binary>>};
{_, _, false} ->
{error,
<<<<<<"Extra keys in input: "/utf8,
(begin
_pipe@12 = Keys_only_in_inputs,
gleam@string:join(_pipe@12, <<", "/utf8>>)
end)/binary>>/binary,
" and missing keys in input: "/utf8>>/binary,
(begin
_pipe@13 = Missing_required_keys,
gleam@string:join(_pipe@13, <<", "/utf8>>)
end)/binary>>}
end,
fun(_) ->
Type_validation_errors = begin
_pipe@14 = Inputs,
_pipe@15 = maps:to_list(_pipe@14),
_pipe@17 = gleam@list:filter_map(
_pipe@15,
fun(Pair) ->
{Key, Value} = Pair,
case begin
_pipe@16 = Params,
gleam_stdlib:map_get(_pipe@16, Key)
end of
{error, nil} ->
{error, nil};
{ok, Expected_type} ->
case validate_value_type(
Value,
Expected_type,
Key
) of
{ok, _} ->
{error, nil};
{error, Errs} ->
{ok, Errs}
end
end
end
),
_pipe@18 = gleam@list:map(
_pipe@17,
fun(Err) -> erlang:element(2, Err) end
),
gleam@string:join(_pipe@18, <<", "/utf8>>)
end,
case Type_validation_errors of
<<""/utf8>> ->
{ok, true};
_ ->
{error, Type_validation_errors}
end
end
).
-file("src/caffeine_lang/common/validations.gleam", 118).
?DOC(false).
-spec validate_relevant_uniqueness(list(HWA), fun((HWA) -> binary()), binary()) -> {ok,
boolean()} |
{error, caffeine_lang@common@errors:compilation_error()}.
validate_relevant_uniqueness(
Things_to_validate_uniqueness_for,
Fetch_property,
Thing_label
) ->
Dupe_names = begin
_pipe = Things_to_validate_uniqueness_for,
_pipe@1 = gleam@list:group(
_pipe,
fun(Thing) -> Fetch_property(Thing) end
),
_pipe@2 = gleam@dict:filter(
_pipe@1,
fun(_, Occurrences) -> erlang:length(Occurrences) > 1 end
),
maps:keys(_pipe@2)
end,
case Dupe_names of
[] ->
{ok, true};
_ ->
{error,
{parser_duplicate_error,
<<<<<<"Duplicate "/utf8, Thing_label/binary>>/binary,
": "/utf8>>/binary,
(begin
_pipe@3 = Dupe_names,
gleam@string:join(_pipe@3, <<", "/utf8>>)
end)/binary>>}}
end.
-file("src/caffeine_lang/common/validations.gleam", 144).
?DOC(false).
-spec validate_inputs_for_collection(
list({HWE, HWF}),
fun((HWE) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_())),
fun((HWF) -> gleam@dict:dict(binary(), caffeine_lang@common@accepted_types:accepted_types())),
boolean()
) -> {ok, boolean()} | {error, caffeine_lang@common@errors:compilation_error()}.
validate_inputs_for_collection(
Input_param_collections,
Get_inputs,
Get_params,
Missing_inputs_ok
) ->
Validation_errors = begin
_pipe = Input_param_collections,
_pipe@1 = gleam@list:filter_map(
_pipe,
fun(Collection) ->
{Input_collection, Param_collection} = Collection,
case inputs_validator(
Get_params(Param_collection),
Get_inputs(Input_collection),
Missing_inputs_ok
) of
{ok, _} ->
{error, nil};
{error, Msg} ->
{ok, Msg}
end
end
),
gleam@string:join(_pipe@1, <<", "/utf8>>)
end,
case Validation_errors of
<<""/utf8>> ->
{ok, true};
_ ->
{error,
{parser_json_parser_error,
<<"Input validation errors: "/utf8,
Validation_errors/binary>>}}
end.
-file("src/caffeine_lang/common/validations.gleam", 176).
?DOC(false).
-spec check_collection_key_overshadowing(
gleam@dict:dict(binary(), any()),
gleam@dict:dict(binary(), any()),
binary()
) -> {ok, boolean()} | {error, binary()}.
check_collection_key_overshadowing(
Reference_collection,
Referrer_collection,
Error_msg
) ->
Reference_names = begin
_pipe = Reference_collection,
_pipe@1 = maps:keys(_pipe),
gleam@set:from_list(_pipe@1)
end,
Referrer_names = begin
_pipe@2 = Referrer_collection,
_pipe@3 = maps:keys(_pipe@2),
gleam@set:from_list(_pipe@3)
end,
Overshadowing_params = begin
_pipe@4 = gleam@set:intersection(Reference_names, Referrer_names),
gleam@set:to_list(_pipe@4)
end,
case Overshadowing_params of
[] ->
{ok, true};
_ ->
{error,
<<Error_msg/binary,
(begin
_pipe@5 = Overshadowing_params,
gleam@string:join(_pipe@5, <<", "/utf8>>)
end)/binary>>}
end.