Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type validations for default input value #77

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/kino/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Kino.Input do
"""
@spec text(String.t(), keyword()) :: t()
def text(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, "")
default = opts |> Keyword.get(:default, "") |> to_string()
new(%{type: :text, label: label, default: default})
end

Expand All @@ -68,7 +68,7 @@ defmodule Kino.Input do
"""
@spec textarea(String.t(), keyword()) :: t()
def textarea(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, "")
default = opts |> Keyword.get(:default, "") |> to_string()
new(%{type: :textarea, label: label, default: default})
end

Expand All @@ -84,7 +84,7 @@ defmodule Kino.Input do
"""
@spec password(String.t(), keyword()) :: t()
def password(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, "")
default = opts |> Keyword.get(:default, "") |> to_string()
new(%{type: :password, label: label, default: default})
end

Expand All @@ -100,9 +100,20 @@ defmodule Kino.Input do
@spec number(String.t(), keyword()) :: t()
def number(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, nil)

assert_default_value!(default, "be either number or nil", fn value ->
is_nil(value) or is_number(value)
end)

new(%{type: :number, label: label, default: default})
end

defp assert_default_value!(value, message, check) do
unless check.(value) do
raise ArgumentError, "expected :default to #{message}, got: #{inspect(value)}"
end
end

@doc """
Creates a new URL input.

Expand All @@ -115,6 +126,11 @@ defmodule Kino.Input do
@spec url(String.t(), keyword()) :: t()
def url(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, nil)

assert_default_value!(default, "be either string or nil", fn value ->
is_nil(value) or is_binary(value)
end)

new(%{type: :url, label: label, default: default})
end

Expand Down Expand Up @@ -167,6 +183,7 @@ defmodule Kino.Input do
@spec checkbox(String.t(), keyword()) :: t()
def checkbox(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, false)
assert_default_value!(default, "be a boolean", &is_boolean/1)
new(%{type: :checkbox, label: label, default: default})
end

Expand Down Expand Up @@ -202,6 +219,8 @@ defmodule Kino.Input do
raise ArgumentError, "expected :step to be positive, got: #{inspect(step)}"
end

assert_default_value!(default, "be a number", &is_number/1)

if default < min or default > max do
raise ArgumentError,
"expected :default to be between :min and :max, got: #{inspect(default)}"
Expand Down Expand Up @@ -229,6 +248,7 @@ defmodule Kino.Input do
@spec color(String.t(), keyword()) :: t()
def color(label, opts \\ []) when is_binary(label) and is_list(opts) do
default = Keyword.get(opts, :default, "#6583FF")
assert_default_value!(default, "be a string", &is_binary/1)
new(%{type: :color, label: label, default: default})
end

Expand Down
16 changes: 16 additions & 0 deletions test/kino/input_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
defmodule Kino.InputTest do
use ExUnit.Case, async: true

describe "text/2" do
test "converts the default value to string" do
assert %{attrs: %{default: "10"}} = Kino.Input.text("Message", default: 10)
end
end

describe "number/2" do
test "raises an error on invalid default value" do
assert_raise ArgumentError,
~s/expected :default to be either number or nil, got: "10"/,
fn ->
Kino.Input.number("Message", default: "10")
end
end
end

describe "select/3" do
test "raises an error for empty option list" do
assert_raise ArgumentError, "expected at least one option, got: []", fn ->
Expand Down