Skip to content

Commit 868fa8b

Browse files
committed
Validate extra fields
1 parent 9ace7e7 commit 868fa8b

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lib/ex_doc/formatter.ex

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,20 @@ defmodule ExDoc.Formatter do
172172

173173
defp build_extra({input, %{url: _} = input_options}, groups, _lang, _auto, _url_pattern) do
174174
input = to_string(input)
175-
title = input_options[:title] || input
176-
group = GroupMatcher.match_extra(groups, input_options[:url])
177-
178-
%{group: group, id: Utils.text_to_id(title), title: title, url: input_options[:url]}
175+
title = validate_extra_string!(input_options, :title) || input
176+
url = validate_extra_string!(input_options, :url)
177+
group = GroupMatcher.match_extra(groups, url)
178+
%{group: group, id: Utils.text_to_id(title), title: title, url: url}
179179
end
180180

181181
defp build_extra({input, input_options}, groups, language, autolink_opts, source_url_pattern) do
182182
input = to_string(input)
183-
id = input_options[:filename] || input |> filename_to_title() |> Utils.text_to_id()
184-
source_file = input_options[:source] || input
183+
184+
id =
185+
validate_extra_string!(input_options, :filename) ||
186+
input |> filename_to_title() |> Utils.text_to_id()
187+
188+
source_file = validate_extra_string!(input_options, :source) || input
185189
opts = [file: source_file, line: 1]
186190

187191
{extension, source, ast} =
@@ -213,11 +217,13 @@ defmodule ExDoc.Formatter do
213217
:error -> {nil, nil, ast}
214218
end
215219

216-
title = input_options[:title] || title_text || filename_to_title(input)
220+
title =
221+
validate_extra_string!(input_options, :title) || title_text || filename_to_title(input)
222+
217223
group = GroupMatcher.match_extra(groups, input)
218224
source_path = source_file |> Path.relative_to(File.cwd!()) |> String.replace_leading("./", "")
219225
source_url = source_url_pattern.(source_path, 1)
220-
search_data = normalize_search_data!(input_options[:search_data])
226+
search_data = validate_search_data!(input_options[:search_data])
221227

222228
%{
223229
type: extra_type(extension),
@@ -233,28 +239,42 @@ defmodule ExDoc.Formatter do
233239
}
234240
end
235241

236-
defp normalize_search_data!(nil), do: nil
242+
defp validate_extra_string!(input_options, key) do
243+
case input_options[key] do
244+
nil ->
245+
nil
237246

238-
defp normalize_search_data!(search_data) when is_list(search_data) do
239-
search_data_keys = [:anchor, :body, :title, :type]
247+
binary when is_binary(binary) ->
248+
binary
240249

250+
other ->
251+
raise ArgumentError,
252+
"extra field #{inspect(key)} must be a string, got: #{inspect(other)}"
253+
end
254+
end
255+
256+
@search_data_keys [:anchor, :body, :title, :type]
257+
258+
defp validate_search_data!(nil), do: nil
259+
260+
defp validate_search_data!(search_data) when is_list(search_data) do
241261
Enum.each(search_data, fn search_data ->
242262
has_keys = Map.keys(search_data)
243263

244-
if Enum.sort(has_keys) != search_data_keys do
264+
if Enum.sort(has_keys) != @search_data_keys do
245265
raise ArgumentError,
246-
"Expected search data to be a list of maps with the keys: #{inspect(search_data_keys)}, found keys: #{inspect(has_keys)}"
266+
"expected search data to be a list of maps with the keys: #{inspect(@search_data_keys)}, " <>
267+
"found keys: #{inspect(has_keys)}"
247268
end
248269
end)
249270

250271
search_data
251272
end
252273

253-
defp normalize_search_data!(search_data) do
254-
search_data_keys = [:anchor, :body, :title, :type]
255-
274+
defp validate_search_data!(search_data) do
256275
raise ArgumentError,
257-
"Expected search data to be a list of maps with the keys: #{inspect(search_data_keys)}, found: #{inspect(search_data)}"
276+
"expected search data to be a list of maps with the keys: #{inspect(@search_data_keys)}, " <>
277+
"found: #{inspect(search_data)}"
258278
end
259279

260280
defp extension_name(input) do

0 commit comments

Comments
 (0)