@@ -3,7 +3,10 @@ defmodule Helper.Converter.EditorToHTML.Validator do
33
44 alias Helper . { Utils , ValidateBySchema }
55
6- @ valid_header_level [ 1 , 2 , 3 ]
6+ alias Helper.Converter.EditorToHTML.Validator.Schema
7+
8+ # blocks with no children items
9+ @ simple_blocks [ "header" , "paragraph" ]
710
811 @ valid_list_mode [ "checklist" , "order_list" , "unorder_list" ]
912 @ valid_list_label_type [ "success" , "done" , "todo" ]
@@ -45,33 +48,9 @@ defmodule Helper.Converter.EditorToHTML.Validator do
4548 { :ok , :pass }
4649 end
4750
48- defp validate_block ( % { "type" => "paragraph" , "data" => % { "text" => text } = data } ) do
49- schema = % { "text" => [ :string ] }
50-
51- case ValidateBySchema . cast ( schema , data ) do
52- { :error , errors } ->
53- format_parse_error ( "paragraph" , errors )
54-
55- _ ->
56- { :ok , :pass }
57- end
58- end
59-
60- defp validate_block ( % { "type" => "header" , "data" => % { "text" => text , "level" => level } = data } ) do
61- schema = % {
62- "text" => [ :string ] ,
63- "level" => [ enum: @ valid_header_level ] ,
64- "eyebrowTitle" => [ :string , required: false ] ,
65- "footerTitle" => [ :string , required: false ]
66- }
67-
68- case ValidateBySchema . cast ( schema , data ) do
69- { :error , errors } ->
70- format_parse_error ( "header" , errors )
71-
72- _ ->
73- { :ok , :pass }
74- end
51+ # validate block which have no nested items
52+ defp validate_block ( % { "type" => type , "data" => data } ) when type in @ simple_blocks do
53+ validate_with ( type , Schema . get ( type ) , data )
7554 end
7655
7756 defp validate_block ( % { "type" => "list" , "data" => % { "mode" => mode , "items" => items } = data } )
@@ -117,6 +96,17 @@ defmodule Helper.Converter.EditorToHTML.Validator do
11796 defp validate_block ( % { "type" => type } ) , do: raise ( "undown #{ type } block" )
11897 defp validate_block ( _ ) , do: raise ( "undown block" )
11998
99+ # validate with given schema
100+ defp validate_with ( block , schema , data ) do
101+ case ValidateBySchema . cast ( schema , data ) do
102+ { :error , errors } ->
103+ format_parse_error ( block , errors )
104+
105+ _ ->
106+ { :ok , :pass }
107+ end
108+ end
109+
120110 # check if the given map has the right key-value fmt of the editorjs structure
121111 defp is_valid_editorjs_fmt ( map ) when is_map ( map ) do
122112 Map . has_key? ( map , "time" ) and
0 commit comments