@@ -9,37 +9,9 @@ defmodule Helper.Converter.EditorToHTML.Validator do
9
9
@ valid_list_label_type [ "success" , "done" , "todo" ]
10
10
@ valid_list_indent [ 0 , 1 , 2 , 3 , 4 ]
11
11
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
-
38
12
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" ]
43
15
44
16
try do
45
17
validate_blocks ( blocks )
@@ -73,8 +45,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
73
45
{ :ok , :pass }
74
46
end
75
47
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 ] }
78
50
79
51
case ValidateBySchema . cast ( schema , data ) do
80
52
{ :error , errors } ->
@@ -85,12 +57,12 @@ defmodule Helper.Converter.EditorToHTML.Validator do
85
57
end
86
58
end
87
59
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
89
61
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 ]
94
66
}
95
67
96
68
case ValidateBySchema . cast ( schema , data ) do
@@ -102,18 +74,18 @@ defmodule Helper.Converter.EditorToHTML.Validator do
102
74
end
103
75
end
104
76
105
- defp validate_block ( % { type: "list" , data: % { mode: mode , items: items } = data } )
77
+ defp validate_block ( % { " type" => "list" , " data" => % { " mode" => mode , " items" => items } = data } )
106
78
when mode in @ valid_list_mode and is_list ( items ) do
107
79
# mode_schema = %{mode: [enum: @valid_list_mode]}
108
80
# {:ok, _} = ValidateBySchema.cast(mode_schema, data)
109
81
110
82
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 ]
117
89
}
118
90
119
91
Enum . each ( items , fn item ->
@@ -130,7 +102,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
130
102
{ :ok , :pass }
131
103
end
132
104
133
- defp validate_block ( % { type: "code" } ) do
105
+ defp validate_block ( % { " type" => "code" } ) do
134
106
# schema = %{text: [:string]}
135
107
# case ValidateBySchema.cast(schema, data) do
136
108
# {:error, errors} ->
@@ -142,17 +114,17 @@ defmodule Helper.Converter.EditorToHTML.Validator do
142
114
{ :ok , :pass }
143
115
end
144
116
145
- defp validate_block ( % { type: type } ) , do: raise ( "undown#{ type } block" )
117
+ defp validate_block ( % { " type" => type } ) , do: raise ( "undown#{ type } block" )
146
118
defp validate_block ( _ ) , do: raise ( "undown block" )
147
119
148
120
# check if the given map has the right key-value fmt of the editorjs structure
149
121
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" ] )
156
128
end
157
129
158
130
defp format_parse_error ( type , error_list ) when is_list ( error_list ) do