Current section
Files
Jump to
Current section
Files
src/kitazith@internal@validation@component.erl
-module(kitazith@internal@validation@component).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/kitazith/internal/validation/component.gleam").
-export([validate_components/3, components_require_v2_flag/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.
?MODULEDOC(false).
-file("src/kitazith/internal/validation/component.gleam", 352).
?DOC(false).
-spec validate_file(
binary(),
kitazith@component@file:file(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_file(Path, File, Attachment_filenames) ->
case kitazith@internal@validation@attachment:attachment_reference_filename(
erlang:element(2, erlang:element(3, File))
) of
{some, _} ->
kitazith@internal@validation@attachment:validate_attachment_reference(
kitazith@internal@validation@common:join_path(
Path,
<<"file.url"/utf8>>
),
erlang:element(2, erlang:element(3, File)),
Attachment_filenames
);
none ->
[kitazith@internal@validation@common:error(
kitazith@internal@validation@common:join_path(
Path,
<<"file.url"/utf8>>
),
attachment_reference_required
)]
end.
-file("src/kitazith/internal/validation/component.gleam", 328).
?DOC(false).
-spec validate_media_gallery_item(
binary(),
kitazith@component@media_gallery:media_gallery_item(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_media_gallery_item(Path, Item, Attachment_filenames) ->
lists:append([case erlang:element(3, Item) of
{some, Description} ->
kitazith@internal@validation@common:validate_string_max_length(
kitazith@internal@validation@common:join_path(
Path,
<<"description"/utf8>>
),
Description,
1024
);
none ->
[]
end, kitazith@internal@validation@attachment:validate_attachment_reference(
kitazith@internal@validation@common:join_path(
Path,
<<"media.url"/utf8>>
),
erlang:element(2, erlang:element(2, Item)),
Attachment_filenames
)]).
-file("src/kitazith/internal/validation/component.gleam", 294).
?DOC(false).
-spec validate_media_gallery(
binary(),
kitazith@component@media_gallery:media_gallery(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_media_gallery(Path, Media_gallery, Attachment_filenames) ->
Item_count = erlang:length(erlang:element(3, Media_gallery)),
lists:append([case (Item_count >= 1) andalso (Item_count =< 10) of
true ->
[];
false ->
[kitazith@internal@validation@common:error(
kitazith@internal@validation@common:join_path(
Path,
<<"items"/utf8>>
),
{component_count_out_of_range,
1,
10,
Item_count,
<<"media gallery items"/utf8>>}
)]
end, begin
_pipe = erlang:element(3, Media_gallery),
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Item, Index) ->
validate_media_gallery_item(
kitazith@internal@validation@common:indexed_path(
kitazith@internal@validation@common:join_path(
Path,
<<"items"/utf8>>
),
Index
),
Item,
Attachment_filenames
)
end
),
lists:append(_pipe@1)
end]).
-file("src/kitazith/internal/validation/component.gleam", 282).
?DOC(false).
-spec validate_thumbnail(
binary(),
kitazith@component@section:thumbnail(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_thumbnail(Path, Thumbnail, Attachment_filenames) ->
kitazith@internal@validation@attachment:validate_attachment_reference(
kitazith@internal@validation@common:join_path(
Path,
<<"media.url"/utf8>>
),
erlang:element(2, erlang:element(3, Thumbnail)),
Attachment_filenames
).
-file("src/kitazith/internal/validation/component.gleam", 237).
?DOC(false).
-spec validate_text_display(
binary(),
kitazith@component@text_display:text_display()
) -> list(kitazith@validation:validation_error()).
validate_text_display(_, _) ->
[].
-file("src/kitazith/internal/validation/component.gleam", 244).
?DOC(false).
-spec validate_section(
binary(),
kitazith@component@section:section(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_section(Path, Section, Attachment_filenames) ->
Component_count = erlang:length(erlang:element(3, Section)),
lists:append([case (Component_count >= 1) andalso (Component_count =< 3) of
true ->
[];
false ->
[kitazith@internal@validation@common:error(
kitazith@internal@validation@common:join_path(
Path,
<<"components"/utf8>>
),
{component_count_out_of_range,
1,
3,
Component_count,
<<"text display components"/utf8>>}
)]
end, begin
_pipe = erlang:element(3, Section),
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Text_display, Index) ->
validate_text_display(
kitazith@internal@validation@common:indexed_path(
kitazith@internal@validation@common:join_path(
Path,
<<"components"/utf8>>
),
Index
),
Text_display
)
end
),
lists:append(_pipe@1)
end, validate_thumbnail(
kitazith@internal@validation@common:join_path(
Path,
<<"accessory"/utf8>>
),
erlang:element(4, Section),
Attachment_filenames
)]).
-file("src/kitazith/internal/validation/component.gleam", 411).
?DOC(false).
-spec validate_container_child(
binary(),
kitazith@component@container:container_child(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_container_child(Path, Child, Attachment_filenames) ->
case Child of
{container_text_display, Text_display} ->
validate_text_display(Path, Text_display);
{container_section, Section} ->
validate_section(Path, Section, Attachment_filenames);
{container_media_gallery, Media_gallery} ->
validate_media_gallery(Path, Media_gallery, Attachment_filenames);
{container_file, File} ->
validate_file(Path, File, Attachment_filenames);
{container_separator, _} ->
[]
end.
-file("src/kitazith/internal/validation/component.gleam", 374).
?DOC(false).
-spec validate_container(
binary(),
kitazith@component@container:container(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_container(Path, Container, Attachment_filenames) ->
lists:append([case erlang:element(4, Container) of
{some, Accent_color} ->
case (Accent_color >= 0) andalso (Accent_color =< 16#FFFFFF) of
true ->
[];
false ->
[kitazith@internal@validation@common:error(
kitazith@internal@validation@common:join_path(
Path,
<<"accent_color"/utf8>>
),
{numeric_out_of_range,
0,
16#FFFFFF,
Accent_color,
<<"RGB values"/utf8>>}
)]
end;
none ->
[]
end, begin
_pipe = erlang:element(3, Container),
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Child, Index) ->
validate_container_child(
kitazith@internal@validation@common:indexed_path(
kitazith@internal@validation@common:join_path(
Path,
<<"components"/utf8>>
),
Index
),
Child,
Attachment_filenames
)
end
),
lists:append(_pipe@1)
end]).
-file("src/kitazith/internal/validation/component.gleam", 204).
?DOC(false).
-spec validate_component(
binary(),
kitazith@component:component(),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_component(Path, Component, Attachment_filenames) ->
case Component of
{component, _} ->
[];
{text_display_component, Text_display} ->
validate_text_display(Path, Text_display);
{section_component, Section} ->
validate_section(Path, Section, Attachment_filenames);
{media_gallery_component, Media_gallery} ->
validate_media_gallery(Path, Media_gallery, Attachment_filenames);
{file_component, File} ->
validate_file(Path, File, Attachment_filenames);
{separator_component, _} ->
[];
{container_component, Container} ->
validate_container(Path, Container, Attachment_filenames)
end.
-file("src/kitazith/internal/validation/component.gleam", 194).
?DOC(false).
-spec id_occurrence(gleam@option:option(integer()), binary()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
id_occurrence(Id, Path) ->
case Id of
{some, Id@1} when Id@1 =/= 0 ->
[{occurrence, Id@1, Path}];
_ ->
[]
end.
-file("src/kitazith/internal/validation/component.gleam", 153).
?DOC(false).
-spec collect_separator_ids(binary(), kitazith@component@separator:separator()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_separator_ids(Path, Separator) ->
id_occurrence(
erlang:element(2, Separator),
kitazith@internal@validation@common:join_path(Path, <<"id"/utf8>>)
).
-file("src/kitazith/internal/validation/component.gleam", 146).
?DOC(false).
-spec collect_file_ids(binary(), kitazith@component@file:file()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_file_ids(Path, File) ->
id_occurrence(
erlang:element(2, File),
kitazith@internal@validation@common:join_path(Path, <<"id"/utf8>>)
).
-file("src/kitazith/internal/validation/component.gleam", 139).
?DOC(false).
-spec collect_media_gallery_ids(
binary(),
kitazith@component@media_gallery:media_gallery()
) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_media_gallery_ids(Path, Media_gallery) ->
id_occurrence(
erlang:element(2, Media_gallery),
kitazith@internal@validation@common:join_path(Path, <<"id"/utf8>>)
).
-file("src/kitazith/internal/validation/component.gleam", 132).
?DOC(false).
-spec collect_thumbnail_ids(binary(), kitazith@component@section:thumbnail()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_thumbnail_ids(Path, Thumbnail) ->
id_occurrence(
erlang:element(2, Thumbnail),
kitazith@internal@validation@common:join_path(Path, <<"id"/utf8>>)
).
-file("src/kitazith/internal/validation/component.gleam", 104).
?DOC(false).
-spec collect_text_display_ids(
binary(),
kitazith@component@text_display:text_display()
) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_text_display_ids(Path, Text_display) ->
id_occurrence(
erlang:element(2, Text_display),
kitazith@internal@validation@common:join_path(Path, <<"id"/utf8>>)
).
-file("src/kitazith/internal/validation/component.gleam", 111).
?DOC(false).
-spec collect_section_ids(binary(), kitazith@component@section:section()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_section_ids(Path, Section) ->
lists:append(
[id_occurrence(
erlang:element(2, Section),
kitazith@internal@validation@common:join_path(
Path,
<<"id"/utf8>>
)
),
begin
_pipe = erlang:element(3, Section),
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Text_display, Index) ->
collect_text_display_ids(
kitazith@internal@validation@common:indexed_path(
kitazith@internal@validation@common:join_path(
Path,
<<"components"/utf8>>
),
Index
),
Text_display
)
end
),
lists:append(_pipe@1)
end,
collect_thumbnail_ids(
kitazith@internal@validation@common:join_path(
Path,
<<"accessory"/utf8>>
),
erlang:element(4, Section)
)]
).
-file("src/kitazith/internal/validation/component.gleam", 177).
?DOC(false).
-spec collect_container_child_ids(
binary(),
kitazith@component@container:container_child()
) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_container_child_ids(Path, Child) ->
case Child of
{container_text_display, Text_display} ->
collect_text_display_ids(Path, Text_display);
{container_section, Section} ->
collect_section_ids(Path, Section);
{container_media_gallery, Media_gallery} ->
collect_media_gallery_ids(Path, Media_gallery);
{container_file, File} ->
collect_file_ids(Path, File);
{container_separator, Separator} ->
collect_separator_ids(Path, Separator)
end.
-file("src/kitazith/internal/validation/component.gleam", 160).
?DOC(false).
-spec collect_container_ids(binary(), kitazith@component@container:container()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_container_ids(Path, Container) ->
lists:append(
[id_occurrence(
erlang:element(2, Container),
kitazith@internal@validation@common:join_path(
Path,
<<"id"/utf8>>
)
),
begin
_pipe = erlang:element(3, Container),
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Child, Index) ->
collect_container_child_ids(
kitazith@internal@validation@common:indexed_path(
kitazith@internal@validation@common:join_path(
Path,
<<"components"/utf8>>
),
Index
),
Child
)
end
),
lists:append(_pipe@1)
end]
).
-file("src/kitazith/internal/validation/component.gleam", 85).
?DOC(false).
-spec collect_component_ids(binary(), kitazith@component:component()) -> list(kitazith@internal@validation@duplicate:occurrence(integer(), binary())).
collect_component_ids(Path, Component) ->
case Component of
{component, _} ->
[];
{text_display_component, Text_display} ->
collect_text_display_ids(Path, Text_display);
{section_component, Section} ->
collect_section_ids(Path, Section);
{media_gallery_component, Media_gallery} ->
collect_media_gallery_ids(Path, Media_gallery);
{file_component, File} ->
collect_file_ids(Path, File);
{separator_component, Separator} ->
collect_separator_ids(Path, Separator);
{container_component, Container} ->
collect_container_ids(Path, Container)
end.
-file("src/kitazith/internal/validation/component.gleam", 64).
?DOC(false).
-spec duplicate_component_id_errors(
binary(),
list(kitazith@component:component())
) -> list(kitazith@validation:validation_error()).
duplicate_component_id_errors(Path, Components) ->
_pipe = Components,
_pipe@1 = gleam@list:index_map(
_pipe,
fun(Component, Index) ->
collect_component_ids(
kitazith@internal@validation@common:indexed_path(Path, Index),
Component
)
end
),
_pipe@2 = lists:append(_pipe@1),
_pipe@3 = kitazith@internal@validation@duplicate:find(_pipe@2),
gleam@list:map(
_pipe@3,
fun(Duplicate) ->
kitazith@internal@validation@common:error(
Path,
{duplicate_component_id,
erlang:element(2, Duplicate),
erlang:element(3, Duplicate)}
)
end
).
-file("src/kitazith/internal/validation/component.gleam", 456).
?DOC(false).
-spec container_child_count(kitazith@component@container:container_child()) -> integer().
container_child_count(Child) ->
case Child of
{container_text_display, _} ->
1;
{container_section, Section} ->
(1 + erlang:length(erlang:element(3, Section))) + 1;
{container_media_gallery, _} ->
1;
{container_file, _} ->
1;
{container_separator, _} ->
1
end.
-file("src/kitazith/internal/validation/component.gleam", 437).
?DOC(false).
-spec component_count(kitazith@component:component()) -> integer().
component_count(Component) ->
case Component of
{component, _} ->
1;
{text_display_component, _} ->
1;
{section_component, Section} ->
(1 + erlang:length(erlang:element(3, Section))) + 1;
{media_gallery_component, _} ->
1;
{file_component, _} ->
1;
{separator_component, _} ->
1;
{container_component, Container} ->
1 + begin
_pipe = erlang:element(3, Container),
_pipe@1 = gleam@list:map(_pipe, fun container_child_count/1),
gleam@list:fold(
_pipe@1,
0,
fun(Total, Count) -> Total + Count end
)
end
end.
-file("src/kitazith/internal/validation/component.gleam", 18).
?DOC(false).
-spec validate_components(
binary(),
list(kitazith@component:component()),
gleam@option:option(list(binary()))
) -> list(kitazith@validation:validation_error()).
validate_components(Path, Components, Attachment_filenames) ->
Total_component_count = begin
_pipe = Components,
_pipe@1 = gleam@list:map(_pipe, fun component_count/1),
gleam@list:fold(_pipe@1, 0, fun(Total, Count) -> Total + Count end)
end,
lists:append([case Total_component_count =< 40 of
true ->
[];
false ->
[kitazith@internal@validation@common:error(
Path,
{aggregate_component_limit_exceeded,
40,
Total_component_count}
)]
end, duplicate_component_id_errors(Path, Components), begin
_pipe@2 = Components,
_pipe@3 = gleam@list:index_map(
_pipe@2,
fun(Component, Index) ->
validate_component(
kitazith@internal@validation@common:indexed_path(
Path,
Index
),
Component,
Attachment_filenames
)
end
),
lists:append(_pipe@3)
end]).
-file("src/kitazith/internal/validation/component.gleam", 495).
?DOC(false).
-spec is_v2_component_type(integer()) -> boolean().
is_v2_component_type(Component_type) ->
case Component_type of
9 ->
true;
10 ->
true;
11 ->
true;
12 ->
true;
13 ->
true;
14 ->
true;
17 ->
true;
_ ->
false
end.
-file("src/kitazith/internal/validation/component.gleam", 479).
?DOC(false).
-spec raw_component_type(gleam@json:json()) -> gleam@option:option(integer()).
raw_component_type(Raw) ->
Decoder = begin
gleam@dynamic@decode:optional_field(
<<"type"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_int/1}
),
fun(Component_type) ->
gleam@dynamic@decode:success(Component_type)
end
)
end,
case gleam@json:parse(gleam@json:to_string(Raw), Decoder) of
{ok, Component_type@1} ->
Component_type@1;
{error, _} ->
none
end.
-file("src/kitazith/internal/validation/component.gleam", 467).
?DOC(false).
-spec component_requires_v2_flag(kitazith@component:component()) -> boolean().
component_requires_v2_flag(Component) ->
case Component of
{component, Raw} ->
case raw_component_type(Raw) of
{some, Component_type} ->
is_v2_component_type(Component_type);
none ->
false
end;
_ ->
true
end.
-file("src/kitazith/internal/validation/component.gleam", 54).
?DOC(false).
-spec components_require_v2_flag(list(kitazith@component:component())) -> boolean().
components_require_v2_flag(Components) ->
case Components of
[] ->
false;
[Component | Rest] ->
component_requires_v2_flag(Component) orelse components_require_v2_flag(
Rest
)
end.