@@ -7,6 +7,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
77
88 # blocks with no children items
99 @ simple_blocks [ "header" , "paragraph" ]
10+ # blocks with "mode" and "items" fields
11+ @ complex_blocks [ "list" ]
1012
1113 @ valid_list_mode [ "checklist" , "order_list" , "unorder_list" ]
1214 @ valid_list_label_type [ "success" , "done" , "todo" ]
@@ -53,32 +55,11 @@ defmodule Helper.Converter.EditorToHTML.Validator do
5355 validate_with ( type , Schema . get ( type ) , data )
5456 end
5557
56- defp validate_block ( % { "type" => "list" , "data" => % { "mode" => mode , "items" => items } = data } )
57- when mode in @ valid_list_mode and is_list ( items ) do
58- # mode_schema = %{mode: [enum: @valid_list_mode]}
59- # {:ok, _} = ValidateBySchema.cast(mode_schema, data)
60-
61- item_schema = % {
62- "checked" => [ :boolean ] ,
63- "hideLabel" => [ :boolean ] ,
64- "label" => [ :string ] ,
65- "labelType" => [ enum: @ valid_list_label_type ] ,
66- "indent" => [ enum: @ valid_list_indent ] ,
67- "text" => [ :string ]
68- }
69-
70- Enum . each ( items , fn item ->
71- case ValidateBySchema . cast ( item_schema , item ) do
72- { :error , errors } ->
73- { :error , message } = format_parse_error ( "list(#{ mode } )" , errors )
74- raise % MatchError { term: { :error , message } }
75-
76- _ ->
77- { :ok , :pass }
78- end
79- end )
80-
81- { :ok , :pass }
58+ # validate block which has mode and items
59+ defp validate_block ( % { "type" => type , "data" => % { "mode" => mode , "items" => items } = data } )
60+ when type in @ complex_blocks do
61+ [ parent: parent_schema , item: item_schema ] = Schema . get ( type )
62+ validate_with ( type , parent_schema , item_schema , data )
8263 end
8364
8465 defp validate_block ( % { "type" => "code" } ) do
@@ -107,6 +88,31 @@ defmodule Helper.Converter.EditorToHTML.Validator do
10788 end
10889 end
10990
91+ defp validate_with ( block , parent_schema , item_schema , data ) do
92+ case ValidateBySchema . cast ( parent_schema , data ) do
93+ { :error , errors } ->
94+ format_parse_error ( block , errors )
95+
96+ _ ->
97+ { :ok , :pass }
98+ end
99+
100+ % { "mode" => mode , "items" => items } = data
101+
102+ Enum . each ( items , fn item ->
103+ case ValidateBySchema . cast ( item_schema , item ) do
104+ { :error , errors } ->
105+ { :error , message } = format_parse_error ( "#{ block } (#{ mode } )" , errors )
106+ raise % MatchError { term: { :error , message } }
107+
108+ _ ->
109+ { :ok , :pass }
110+ end
111+ end )
112+
113+ { :ok , :pass }
114+ end
115+
110116 # check if the given map has the right key-value fmt of the editorjs structure
111117 defp is_valid_editorjs_fmt ( map ) when is_map ( map ) do
112118 Map . has_key? ( map , "time" ) and
0 commit comments