@@ -7,6 +7,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
7
7
8
8
# blocks with no children items
9
9
@ simple_blocks [ "header" , "paragraph" ]
10
+ # blocks with "mode" and "items" fields
11
+ @ complex_blocks [ "list" ]
10
12
11
13
@ valid_list_mode [ "checklist" , "order_list" , "unorder_list" ]
12
14
@ valid_list_label_type [ "success" , "done" , "todo" ]
@@ -53,32 +55,11 @@ defmodule Helper.Converter.EditorToHTML.Validator do
53
55
validate_with ( type , Schema . get ( type ) , data )
54
56
end
55
57
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 )
82
63
end
83
64
84
65
defp validate_block ( % { "type" => "code" } ) do
@@ -107,6 +88,31 @@ defmodule Helper.Converter.EditorToHTML.Validator do
107
88
end
108
89
end
109
90
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
+
110
116
# check if the given map has the right key-value fmt of the editorjs structure
111
117
defp is_valid_editorjs_fmt ( map ) when is_map ( map ) do
112
118
Map . has_key? ( map , "time" ) and