Skip to content

Commit 6b42c6a

Browse files
committed
add none raising validate function.
1 parent ecd7bcd commit 6b42c6a

File tree

2 files changed

+139
-34
lines changed

2 files changed

+139
-34
lines changed

lib/scenic/themes.ex

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ defmodule Scenic.Themes do
111111
end
112112
end
113113

114-
def validate(theme)
115-
def validate({lib, theme_name} = lib_theme) when is_atom(theme_name) do
114+
def validate!(theme)
115+
def validate!({lib, theme_name} = lib_theme) when is_atom(theme_name) do
116116
themes = module().library()
117117
case Map.get(themes, lib) do
118118
{themes, schema} ->
@@ -151,7 +151,7 @@ defmodule Scenic.Themes do
151151
end
152152
end
153153

154-
def validate(theme_name) when is_atom(theme_name) do
154+
def validate!(theme_name) when is_atom(theme_name) do
155155
lib = module().library()
156156
{themes, schema} = Map.get(lib, :scenic)
157157
case Map.get(themes, theme_name) do
@@ -175,7 +175,7 @@ defmodule Scenic.Themes do
175175
end
176176
end
177177

178-
def validate(
178+
def validate!(
179179
%{
180180
text: _,
181181
background: _,
@@ -202,7 +202,7 @@ defmodule Scenic.Themes do
202202
end)
203203
end
204204

205-
def validate(%{} = map) do
205+
def validate!(%{} = map) do
206206
{
207207
:error,
208208
"""
@@ -218,7 +218,7 @@ defmodule Scenic.Themes do
218218
}
219219
end
220220

221-
def validate(data) do
221+
def validate!(data) do
222222
{
223223
:error,
224224
"""
@@ -239,6 +239,107 @@ defmodule Scenic.Themes do
239239
}
240240
end
241241

242+
def validate!(
243+
theme,
244+
schema
245+
) do
246+
# we have the schema so we can validate against it.
247+
schema
248+
|> Enum.reduce({:ok, theme}, fn
249+
_, {:error, msg} ->
250+
{:error, msg}
251+
key, {:ok, _} = acc ->
252+
case Map.has_key?(theme, key) do
253+
true -> acc
254+
false -> err_key(key, theme)
255+
end
256+
end)
257+
end
258+
259+
def validate(theme)
260+
def validate({lib, theme_name} = lib_theme) when is_atom(theme_name) do
261+
themes = module().library()
262+
case Map.get(themes, lib) do
263+
{themes, schema} ->
264+
# validate against the schema
265+
case Map.get(themes, theme_name) do
266+
nil ->
267+
{
268+
:error,
269+
:not_found
270+
}
271+
theme ->
272+
case validate(theme, schema) do
273+
{:ok, _} -> {:ok, lib_theme}
274+
error -> error
275+
end
276+
end
277+
nil ->
278+
{
279+
:error,
280+
:not_found
281+
}
282+
end
283+
end
284+
285+
def validate(theme_name) when is_atom(theme_name) do
286+
lib = module().library()
287+
{themes, schema} = Map.get(lib, :scenic)
288+
case Map.get(themes, theme_name) do
289+
nil ->
290+
{
291+
:error,
292+
:not_found
293+
}
294+
theme ->
295+
case validate(theme, schema) do
296+
{:ok, _} -> {:ok, theme_name}
297+
error -> error
298+
end
299+
end
300+
end
301+
302+
def validate(
303+
%{
304+
text: _,
305+
background: _,
306+
border: _,
307+
active: _,
308+
thumb: _,
309+
focus: _
310+
} = theme
311+
) do
312+
# we dont have the schema so validate against the default,
313+
# this is not ideal, but should be fine for now.
314+
# we know all the required colors are there.
315+
# now make sure they are all valid colors, including any custom added ones.
316+
theme
317+
|> Enum.reduce({:ok, theme}, fn
318+
_, {:error, msg} ->
319+
{:error, msg}
320+
321+
{key, color}, {:ok, _} = acc ->
322+
case Color.validate(color) do
323+
{:ok, _} -> acc
324+
{:error, msg} -> {:error, :invalid_color}
325+
end
326+
end)
327+
end
328+
329+
def validate(%{} = map) do
330+
{
331+
:error,
332+
:missing_key
333+
}
334+
end
335+
336+
def validate(data) do
337+
{
338+
:error,
339+
:invalid
340+
}
341+
end
342+
242343
def validate(
243344
theme,
244345
schema

test/scenic/themes_test.exs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,43 @@ defmodule Scenic.ThemesTest do
7474
assert Themes.normalize(:dark) == @theme_dark
7575
end
7676

77-
test "custom validate method accepts custom named themes" do
78-
assert Themes.validate({:custom_scenic, :custom_dark}) == {:ok, {:custom_scenic, :custom_dark}}
79-
assert Themes.validate({:custom_scenic, :custom_light}) == {:ok, {:custom_scenic, :custom_light}}
80-
assert Themes.validate({:custom_scenic, :custom_primary}) == {:ok, {:custom_scenic, :custom_primary}}
81-
assert Themes.validate({:custom_scenic, :custom_secondary}) == {:ok, {:custom_scenic, :custom_secondary}}
82-
assert Themes.validate({:custom_scenic, :custom_success}) == {:ok, {:custom_scenic, :custom_success}}
83-
assert Themes.validate({:custom_scenic, :custom_danger}) == {:ok, {:custom_scenic, :custom_danger}}
84-
assert Themes.validate({:custom_scenic, :custom_warning}) == {:ok, {:custom_scenic, :custom_warning}}
85-
assert Themes.validate({:custom_scenic, :custom_info}) == {:ok, {:custom_scenic, :custom_info}}
86-
assert Themes.validate({:custom_scenic, :custom_text}) == {:ok, {:custom_scenic, :custom_text}}
77+
test "custom validate! method accepts custom named themes" do
78+
assert Themes.validate!({:custom_scenic, :custom_dark}) == {:ok, {:custom_scenic, :custom_dark}}
79+
assert Themes.validate!({:custom_scenic, :custom_light}) == {:ok, {:custom_scenic, :custom_light}}
80+
assert Themes.validate!({:custom_scenic, :custom_primary}) == {:ok, {:custom_scenic, :custom_primary}}
81+
assert Themes.validate!({:custom_scenic, :custom_secondary}) == {:ok, {:custom_scenic, :custom_secondary}}
82+
assert Themes.validate!({:custom_scenic, :custom_success}) == {:ok, {:custom_scenic, :custom_success}}
83+
assert Themes.validate!({:custom_scenic, :custom_danger}) == {:ok, {:custom_scenic, :custom_danger}}
84+
assert Themes.validate!({:custom_scenic, :custom_warning}) == {:ok, {:custom_scenic, :custom_warning}}
85+
assert Themes.validate!({:custom_scenic, :custom_info}) == {:ok, {:custom_scenic, :custom_info}}
86+
assert Themes.validate!({:custom_scenic, :custom_text}) == {:ok, {:custom_scenic, :custom_text}}
8787
end
8888

8989
test "custom validate method rejects map without custom standard color" do
90-
{:error, msg} = Themes.validate({:custom_scenic, :custom_invalid})
90+
{:error, msg} = Themes.validate!({:custom_scenic, :custom_invalid})
9191
assert msg =~ "Invalid theme specification"
9292
assert msg =~ "Map entry: :surface"
9393
end
9494

9595
test "validate accepts the named themes" do
96-
assert Themes.validate({:scenic, :dark}) == {:ok, {:scenic, :dark}}
97-
assert Themes.validate({:scenic, :light}) == {:ok, {:scenic, :light}}
98-
assert Themes.validate({:scenic, :primary}) == {:ok, {:scenic, :primary}}
99-
assert Themes.validate({:scenic, :secondary}) == {:ok, {:scenic, :secondary}}
100-
assert Themes.validate({:scenic, :success}) == {:ok, {:scenic, :success}}
101-
assert Themes.validate({:scenic, :danger}) == {:ok, {:scenic, :danger}}
102-
assert Themes.validate({:scenic, :warning}) == {:ok, {:scenic, :warning}}
103-
assert Themes.validate({:scenic, :info}) == {:ok, {:scenic, :info}}
104-
assert Themes.validate({:scenic, :text}) == {:ok, {:scenic, :text}}
96+
assert Themes.validate!({:scenic, :dark}) == {:ok, {:scenic, :dark}}
97+
assert Themes.validate!({:scenic, :light}) == {:ok, {:scenic, :light}}
98+
assert Themes.validate!({:scenic, :primary}) == {:ok, {:scenic, :primary}}
99+
assert Themes.validate!({:scenic, :secondary}) == {:ok, {:scenic, :secondary}}
100+
assert Themes.validate!({:scenic, :success}) == {:ok, {:scenic, :success}}
101+
assert Themes.validate!({:scenic, :danger}) == {:ok, {:scenic, :danger}}
102+
assert Themes.validate!({:scenic, :warning}) == {:ok, {:scenic, :warning}}
103+
assert Themes.validate!({:scenic, :info}) == {:ok, {:scenic, :info}}
104+
assert Themes.validate!({:scenic, :text}) == {:ok, {:scenic, :text}}
105105
end
106106

107107
test "validate rejects invalid theme names" do
108-
{:error, msg} = Themes.validate(:invalid)
108+
{:error, msg} = Themes.validate!(:invalid)
109109
assert msg =~ "The theme could not be found in library"
110110
end
111111

112112
test "validate defaults to the scenic library when an atom is passed" do
113-
assert Themes.validate(:primary) == {:ok, :primary}
113+
assert Themes.validate!(:primary) == {:ok, :primary}
114114
end
115115

116116
test "validate accepts maps of colors" do
@@ -124,7 +124,7 @@ defmodule Scenic.ThemesTest do
124124
my_color: :black
125125
}
126126

127-
assert Themes.validate(color_map) == {:ok, color_map}
127+
assert Themes.validate!(color_map) == {:ok, color_map}
128128
end
129129

130130
test "validate rejects maps with invalid colors" do
@@ -138,19 +138,23 @@ defmodule Scenic.ThemesTest do
138138
my_color: :black
139139
}
140140

141-
{:error, msg} = Themes.validate(color_map)
141+
{:error, msg} = Themes.validate!(color_map)
142142
assert msg =~ "Map entry: :border"
143143
assert msg =~ "Invalid Color specification: :invalid"
144144
end
145145

146-
test "verify rejects maps without the standard colors" do
146+
test "validate! rejects maps without the standard colors" do
147147
color_map = %{some_name: :red}
148-
{:error, msg} = Themes.validate(color_map)
148+
{:error, msg} = Themes.validate!(color_map)
149149
assert msg =~ "didn't include all the required color"
150150
end
151151

152-
test "verify rejects invalid values" do
153-
{:error, _msg} = Themes.validate("totally wrong")
152+
test "validate rejects invalid values" do
153+
{:error, _msg} = Themes.validate!("totally wrong")
154+
end
155+
156+
test "validate rejects invalid values and does not raise" do
157+
{:error, :invalid} = Themes.validate("totally wrong")
154158
end
155159

156160
@default_schema [:text, :background, :border, :active, :thumb, :focus]

0 commit comments

Comments
 (0)