-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", 92). ?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", 137). ?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", 99). ?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", 183). ?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", 149). ?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", 207). ?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", 266). ?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", 229). ?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", 59). ?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", 311). ?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", 292). ?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", 16). ?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, 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", 334). ?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", 350). ?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", 322). ?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", 51). ?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.