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

Make messages configurable #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/validators/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule EctoCommons.DateValidator do
defp wrong_date(%Date{} = value, is, nil, opts) do
case Date.compare(value, is) do
:eq -> nil
_ -> {message(opts, "should be %{is}."), validation: :date, kind: :is, is: is}
_ -> {message(opts, :message, "should be %{is}."), validation: :date, kind: :is, is: is}
end
end

Expand All @@ -114,7 +114,7 @@ defmodule EctoCommons.DateValidator do
_ ->
case abs(Date.diff(value, is)) do
val when val > delta ->
{message(opts, "should be %{is}."), validation: :date, kind: :is, is: is}
{message(opts, :message, "should be %{is}."), validation: :date, kind: :is, is: is}

_ ->
nil
Expand All @@ -127,7 +127,7 @@ defmodule EctoCommons.DateValidator do
defp too_soon(%Date{} = value, afterr, opts) do
case Date.compare(value, afterr) do
:gt -> nil
_ -> {message(opts, "should be after %{after}."), validation: :date, kind: :after, after: afterr}
_ -> {message(opts, :message, "should be after %{after}."), validation: :date, kind: :after, after: afterr}
end
end

Expand All @@ -136,7 +136,7 @@ defmodule EctoCommons.DateValidator do
defp too_late(%Date{} = value, before, opts) do
case Date.compare(value, before) do
:lt -> nil
_ -> {message(opts, "should be before %{before}."), validation: :date, kind: :before, before: before}
_ -> {message(opts, :message, "should be before %{before}."), validation: :date, kind: :before, before: before}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/validators/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule EctoCommons.DateTimeValidator do
defp wrong_datetime(%DateTime{} = value, is, nil, opts) do
case DateTime.compare(value, is) do
:eq -> nil
_ -> {message(opts, "should be %{is}."), validation: :datetime, kind: :is}
_ -> {message(opts, :message, "should be %{is}."), validation: :datetime, kind: :is}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/validators/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule EctoCommons.EmailValidator do
Enum.reduce(checks, [], fn check, errors ->
case do_validate_email(value, check) do
:ok -> errors
{:error, msg} -> [{field, {message(opts, msg), [validation: :email]}} | errors]
{:error, msg} -> [{field, {message(opts, :message, msg), [validation: :email]}} | errors]
end
end)
|> List.flatten()
Expand Down
2 changes: 1 addition & 1 deletion lib/validators/phone_number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule EctoCommons.PhoneNumberValidator do
[]
else
_err ->
[{field, {message(opts, "is not a valid phone number"), [validation: :phone_number]}}]
[{field, {message(opts, :message, "is not a valid phone number"), [validation: :phone_number]}}]
end
end)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/validators/postal_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule EctoCommons.PostalCodeValidator do
[]

false ->
[{field, {message(opts, "is not a valid postal code"), [validation: :postal_code]}}]
[{field, {message(opts, :message, "is not a valid postal code"), [validation: :postal_code]}}]
end
end)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/validators/social_security.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule EctoCommons.SocialSecurityValidator do
false ->
[
{field,
{message(opts, "is not a valid social security number"),
{message(opts, :message, "is not a valid social security number"),
[validation: :social_security]}}
]

Expand All @@ -69,7 +69,7 @@ defmodule EctoCommons.SocialSecurityValidator do
_e ->
[
{field,
{message(opts, "is not a valid social security number"),
{message(opts, :message, "is not a valid social security number"),
[validation: :social_security]}}
]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/validators/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ defmodule EctoCommons.StringValidator do

defp wrong_prefix(value, prefix, opts) do
unless String.starts_with?(value, prefix) do
{message(opts, "is not prefixed by %{prefix}."), validation: :has_prefix}
{message(opts, :message, "is not prefixed by %{prefix}."), validation: :has_prefix}
else
nil
end
Expand Down
8 changes: 4 additions & 4 deletions lib/validators/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule EctoCommons.TimeValidator do
defp wrong_time(%Time{} = value, is, nil, opts) do
case Time.compare(value, is) do
:eq -> nil
_ -> {message(opts, "should be %{is}."), validation: :time, kind: :is}
_ -> {message(opts, :message, "should be %{is}."), validation: :time, kind: :is}
end
end

Expand All @@ -114,7 +114,7 @@ defmodule EctoCommons.TimeValidator do
_ ->
case abs(Time.diff(value, is)) do
val when val > delta ->
{message(opts, "should be %{is}."), validation: :time, kind: :is}
{message(opts, :message, "should be %{is}."), validation: :time, kind: :is}

_ ->
nil
Expand All @@ -127,7 +127,7 @@ defmodule EctoCommons.TimeValidator do
defp too_soon(%Time{} = value, afterr, opts) do
case Time.compare(value, afterr) do
:gt -> nil
_ -> {message(opts, "should be after %{after}."), validation: :time, kind: :after}
_ -> {message(opts, :message, "should be after %{after}."), validation: :time, kind: :after}
end
end

Expand All @@ -136,7 +136,7 @@ defmodule EctoCommons.TimeValidator do
defp too_late(%Time{} = value, before, opts) do
case Time.compare(value, before) do
:lt -> nil
_ -> {message(opts, "should be before %{before}."), validation: :time, kind: :before}
_ -> {message(opts, :message, "should be before %{before}."), validation: :time, kind: :before}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/validators/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule EctoCommons.URLValidator do

case do_validate_url(value, parsed, checks) do
:ok -> []
:error -> [{field, {message(opts, "is not a valid url"), [validation: :url]}}]
:error -> [{field, {message(opts, :message, "is not a valid url"), [validation: :url]}}]
end
end)
end
Expand Down