@@ -9,37 +9,9 @@ defmodule Helper.Converter.EditorToHTML.Validator do
99 @ valid_list_label_type [ "success" , "done" , "todo" ]
1010 @ valid_list_indent [ 0 , 1 , 2 , 3 , 4 ]
1111
12- # atoms dynamically and atoms are not
13- # garbage-collected. Therefore, string should not be an untrusted value, such as
14- # input received from a socket or during a web request. Consider using
15- # to_existing_atom/1 instead
16- # keys_to_atoms is using to_existing_atom under the hook, so we have to pre-define the
17- # trusted atoms
18- tursted_atoms = [
19- # common
20- :text ,
21- :items ,
22- # header
23- :level ,
24- :eyebrowTitle ,
25- :footerTitle ,
26- # list
27- :hideLabel ,
28- :labelType ,
29- :indent ,
30- :checked ,
31- :label ,
32- # code
33- :lang
34- ]
35-
36- Enum . each ( tursted_atoms , fn atom -> _ = atom end )
37-
3812 def is_valid ( map ) when is_map ( map ) do
39- with atom_map <- Utils . keys_to_atoms ( map ) ,
40- true <- is_valid_editorjs_fmt ( atom_map ) do
41- blocks = atom_map . blocks
42- # validate_blocks(blocks)
13+ with true <- is_valid_editorjs_fmt ( map ) do
14+ blocks = map [ "blocks" ]
4315
4416 try do
4517 validate_blocks ( blocks )
@@ -73,8 +45,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
7345 { :ok , :pass }
7446 end
7547
76- defp validate_block ( % { type: "paragraph" , data: % { text: text } = data } ) do
77- schema = % { text: [ :string ] }
48+ defp validate_block ( % { " type" => "paragraph" , " data" => % { " text" => text } = data } ) do
49+ schema = % { " text" => [ :string ] }
7850
7951 case ValidateBySchema . cast ( schema , data ) do
8052 { :error , errors } ->
@@ -85,12 +57,12 @@ defmodule Helper.Converter.EditorToHTML.Validator do
8557 end
8658 end
8759
88- defp validate_block ( % { type: "header" , data: % { text: text , level: level } = data } ) do
60+ defp validate_block ( % { " type" => "header" , " data" => % { " text" => text , " level" => level } = data } ) do
8961 schema = % {
90- text: [ :string ] ,
91- level: [ enum: @ valid_header_level ] ,
92- eyebrowTitle: [ :string , required: false ] ,
93- footerTitle: [ :string , required: false ]
62+ " text" => [ :string ] ,
63+ " level" => [ enum: @ valid_header_level ] ,
64+ " eyebrowTitle" => [ :string , required: false ] ,
65+ " footerTitle" => [ :string , required: false ]
9466 }
9567
9668 case ValidateBySchema . cast ( schema , data ) do
@@ -102,18 +74,18 @@ defmodule Helper.Converter.EditorToHTML.Validator do
10274 end
10375 end
10476
105- defp validate_block ( % { type: "list" , data: % { mode: mode , items: items } = data } )
77+ defp validate_block ( % { " type" => "list" , " data" => % { " mode" => mode , " items" => items } = data } )
10678 when mode in @ valid_list_mode and is_list ( items ) do
10779 # mode_schema = %{mode: [enum: @valid_list_mode]}
10880 # {:ok, _} = ValidateBySchema.cast(mode_schema, data)
10981
11082 item_schema = % {
111- checked: [ :boolean ] ,
112- hideLabel: [ :boolean ] ,
113- label: [ :string ] ,
114- labelType: [ enum: @ valid_list_label_type ] ,
115- indent: [ enum: @ valid_list_indent ] ,
116- text: [ :string ]
83+ " checked" => [ :boolean ] ,
84+ " hideLabel" => [ :boolean ] ,
85+ " label" => [ :string ] ,
86+ " labelType" => [ enum: @ valid_list_label_type ] ,
87+ " indent" => [ enum: @ valid_list_indent ] ,
88+ " text" => [ :string ]
11789 }
11890
11991 Enum . each ( items , fn item ->
@@ -130,7 +102,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
130102 { :ok , :pass }
131103 end
132104
133- defp validate_block ( % { type: "code" } ) do
105+ defp validate_block ( % { " type" => "code" } ) do
134106 # schema = %{text: [:string]}
135107 # case ValidateBySchema.cast(schema, data) do
136108 # {:error, errors} ->
@@ -142,17 +114,17 @@ defmodule Helper.Converter.EditorToHTML.Validator do
142114 { :ok , :pass }
143115 end
144116
145- defp validate_block ( % { type: type } ) , do: raise ( "undown #{ type } block" )
117+ defp validate_block ( % { " type" => type } ) , do: raise ( "undown #{ type } block" )
146118 defp validate_block ( _ ) , do: raise ( "undown block" )
147119
148120 # check if the given map has the right key-value fmt of the editorjs structure
149121 defp is_valid_editorjs_fmt ( map ) when is_map ( map ) do
150- Map . has_key? ( map , : time) and
151- Map . has_key? ( map , : version) and
152- Map . has_key? ( map , : blocks) and
153- is_list ( map . blocks ) and
154- is_binary ( map . version ) and
155- is_integer ( map . time )
122+ Map . has_key? ( map , " time" ) and
123+ Map . has_key? ( map , " version" ) and
124+ Map . has_key? ( map , " blocks" ) and
125+ is_list ( map [ " blocks" ] ) and
126+ is_binary ( map [ " version" ] ) and
127+ is_integer ( map [ " time" ] )
156128 end
157129
158130 defp format_parse_error ( type , error_list ) when is_list ( error_list ) do
0 commit comments