Packages
caffeine_lang
2.0.1
6.3.1
6.3.0
6.2.2
6.2.1
6.2.0
6.1.2
6.1.1
6.1.0
6.0.0
5.6.0
5.5.0
5.4.4
5.4.3
5.4.2
5.4.1
5.4.0
5.3.0
5.2.0
5.1.1
5.1.0
5.0.12
5.0.11
5.0.10
5.0.8
5.0.7
5.0.6
5.0.5
5.0.4
5.0.1
5.0.0
4.10.0
4.9.0
4.8.3
4.8.2
4.8.1
4.8.0
4.7.9
4.7.8
4.7.7
4.7.6
4.7.5
4.6.7
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.7
4.3.6
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.0.2
1.0.1
0.1.0
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.2
0.0.1
A compiler for generating reliability artifacts from service expectation definitions.
Current section
Files
Jump to
Current section
Files
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.