Skip to content
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
9 changes: 5 additions & 4 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ defmodule Ash.Changeset do

require Ash.Tracer
import Ash.Expr
import Ash.Gettext
require Logger

defmodule OriginalDataNotAvailable do
Expand Down Expand Up @@ -3491,7 +3492,7 @@ defmodule Ash.Changeset do
changeset,
InvalidAttribute.exception(
field: key,
message: "cannot be changed",
message: error_message("cannot be changed"),
value: changeset.attributes[key]
)
)
Expand Down Expand Up @@ -5927,7 +5928,7 @@ defmodule Ash.Changeset do
error =
InvalidRelationship.exception(
relationship: relationship.name,
message: "relationship is not editable"
message: error_message("relationship is not editable")
)

add_error(changeset, error)
Expand All @@ -5936,7 +5937,7 @@ defmodule Ash.Changeset do
error =
InvalidRelationship.exception(
relationship: relationship.name,
message: "cannot manage a manual relationship"
message: error_message("cannot manage a manual relationship")
)

add_error(changeset, error)
Expand Down Expand Up @@ -6026,7 +6027,7 @@ defmodule Ash.Changeset do
changeset,
InvalidRelationship.exception(
relationship: relationship.name,
message: "cannot provide structs that don't match the destination"
message: error_message("cannot provide structs that don't match the destination")
)
)
else
Expand Down
3 changes: 2 additions & 1 deletion lib/ash/resource/change/cascade_destroy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do

"""
use Ash.Resource.Change
import Ash.Gettext
require Ash.Query

@doc false
Expand Down Expand Up @@ -245,7 +246,7 @@ defmodule Ash.Resource.Change.CascadeDestroy do
{:error,
Ash.Error.Changes.InvalidRelationship.exception(
relationship: opts.relationship,
message: "Relationship doesn't exist."
message: error_message("Relationship doesn't exist.")
)}

relationship ->
Expand Down
3 changes: 2 additions & 1 deletion lib/ash/resource/change/cascade_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ defmodule Ash.Resource.Change.CascadeUpdate do

"""
use Ash.Resource.Change
import Ash.Gettext
require Ash.Query

@doc false
Expand Down Expand Up @@ -177,7 +178,7 @@ defmodule Ash.Resource.Change.CascadeUpdate do
{:error,
Ash.Error.Changes.InvalidRelationship.exception(
relationship: opts.relationship,
message: "Relationship doesn't exist."
message: error_message("Relationship doesn't exist.")
)}

{_, false} ->
Expand Down
6 changes: 5 additions & 1 deletion lib/ash/resource/change/relate_actor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
defmodule Ash.Resource.Change.RelateActor do
@moduledoc false
use Ash.Resource.Change
import Ash.Gettext
alias Ash.Changeset
alias Ash.Error.Changes.InvalidRelationship

Expand Down Expand Up @@ -61,7 +62,10 @@ defmodule Ash.Resource.Change.RelateActor do
changeset,
InvalidRelationship.exception(
relationship: relationship.name,
message: "could not relate to actor, as no actor was found (and :allow_nil? is false)"
message:
error_message(
"could not relate to actor, as no actor was found (and :allow_nil? is false)"
)
)
)

Expand Down
3 changes: 2 additions & 1 deletion lib/ash/type/atom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule Ash.Type.Atom do
#{Spark.Options.docs(@constraints)}
"""
use Ash.Type
import Ash.Gettext

@impl true
def storage_type(_), do: :string
Expand Down Expand Up @@ -55,7 +56,7 @@ defmodule Ash.Type.Atom do
else
[
[
message: "atom must be one of %{atom_list}, got: %{value}",
message: error_message("atom must be one of %{atom_list}, got: %{value}"),
atom_list: Enum.join(atom_list, ", "),
value: value
]
Expand Down
19 changes: 16 additions & 3 deletions lib/ash/type/ci_string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Ash.Type.CiString do
#{Spark.Options.docs(@constraints)}
"""
use Ash.Type
import Ash.Gettext

@impl true
def storage_type(_), do: :ci_string
Expand Down Expand Up @@ -172,15 +173,24 @@ defmodule Ash.Type.CiString do
Enum.reduce(constraints, [], fn
{:max_length, max_length}, errors ->
if String.length(value) > max_length do
[[message: "length must be less than or equal to %{max}", max: max_length] | errors]
[
[
message: error_message("length must be less than or equal to %{max}"),
max: max_length
]
| errors
]
else
errors
end

{:min_length, min_length}, errors ->
if String.length(value) < min_length do
[
[message: "length must be greater than or equal to %{min}", min: min_length]
[
message: error_message("length must be greater than or equal to %{min}"),
min: min_length
]
| errors
]
else
Expand All @@ -200,7 +210,10 @@ defmodule Ash.Type.CiString do
if String.match?(value, regex) do
errors
else
[[message: "must match the pattern %{regex}", regex: inspect(regex)] | errors]
[
[message: error_message("must match the pattern %{regex}"), regex: inspect(regex)]
| errors
]
end

_, errors ->
Expand Down
33 changes: 23 additions & 10 deletions lib/ash/type/decimal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule Ash.Type.Decimal do
]

import Ash.Expr
import Ash.Gettext

@moduledoc """
Represents a decimal.
Expand Down Expand Up @@ -131,7 +132,7 @@ defmodule Ash.Type.Decimal do
if ^expr > ^max do
error(
Ash.Error.Changes.InvalidChanges,
message: "must be less than or equal to %{max}",
message: ^error_message("must be less than or equal to %{max}"),
vars: %{max: ^max}
)
else
Expand All @@ -144,7 +145,7 @@ defmodule Ash.Type.Decimal do
if ^expr < ^min do
error(
Ash.Error.Changes.InvalidChanges,
message: "must be greater than or equal to %{min}",
message: ^error_message("must be greater than or equal to %{min}"),
vars: %{min: ^min}
)
else
Expand All @@ -159,7 +160,7 @@ defmodule Ash.Type.Decimal do
else
error(
Ash.Error.Changes.InvalidChanges,
message: "must be less than %{less_than}",
message: ^error_message("must be less than %{less_than}"),
vars: %{less_than: ^less_than}
)
end
Expand All @@ -172,7 +173,7 @@ defmodule Ash.Type.Decimal do
else
error(
Ash.Error.Changes.InvalidChanges,
message: "must be greater than %{greater_than}",
message: ^error_message("must be greater than %{greater_than}"),
vars: %{greater_than: ^greater_than}
)
end
Expand All @@ -198,7 +199,7 @@ defmodule Ash.Type.Decimal do
if count_significant_digits(value) > precision do
[
[
message: "must have no more than %{precision} significant digits",
message: error_message("must have no more than %{precision} significant digits"),
precision: precision
]
| errors
Expand All @@ -214,7 +215,7 @@ defmodule Ash.Type.Decimal do
if Decimal.scale(value) > scale do
[
[
message: "must have no more than %{scale} decimal places",
message: error_message("must have no more than %{scale} decimal places"),
scale: scale
]
| errors
Expand All @@ -225,14 +226,17 @@ defmodule Ash.Type.Decimal do

{:max, max}, errors ->
if Decimal.compare(value, max) == :gt do
[[message: "must be less than or equal to %{max}", max: max] | errors]
[[message: error_message("must be less than or equal to %{max}"), max: max] | errors]
else
errors
end

{:min, min}, errors ->
if Decimal.compare(value, min) == :lt do
[[message: "must be more than or equal to %{min}", min: min] | errors]
[
[message: error_message("must be greater than or equal to %{min}"), min: min]
| errors
]
else
errors
end
Expand All @@ -241,14 +245,23 @@ defmodule Ash.Type.Decimal do
if Decimal.compare(value, less_than) == :lt do
errors
else
[[message: "must be less than %{less_than}", less_than: less_than] | errors]
[
[message: error_message("must be less than %{less_than}"), less_than: less_than]
| errors
]
end

{:greater_than, greater_than}, errors ->
if Decimal.compare(value, greater_than) == :gt do
errors
else
[[message: "must be more than %{greater_than}", greater_than: greater_than] | errors]
[
[
message: error_message("must be greater than %{greater_than}"),
greater_than: greater_than
]
| errors
]
end
end)

Expand Down
5 changes: 3 additions & 2 deletions lib/ash/type/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ defmodule Ash.Type.Enum do
defmacro __using__(opts) do
quote location: :keep, generated: true, bind_quoted: [opts: opts] do
use Ash.Type
import Ash.Gettext

require Ash.Expr

Expand Down Expand Up @@ -277,7 +278,7 @@ defmodule Ash.Type.Enum do
Ash.Expr.expr(
error(
Ash.Error.Changes.InvalidChanges,
message: "must be one of %{values}",
message: ^error_message("must be one of %{values}"),
vars: %{values: ^Enum.join(@values, ", ")}
)
)
Expand All @@ -302,7 +303,7 @@ defmodule Ash.Type.Enum do
else
error(
Ash.Error.Changes.InvalidChanges,
message: "must be one of %{values}",
message: ^error_message("must be one of %{values}"),
vars: %{values: ^Enum.join(@values, ", ")}
)
end
Expand Down
29 changes: 21 additions & 8 deletions lib/ash/type/float.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Ash.Type.Float do
]

import Ash.Expr
import Ash.Gettext

@moduledoc """
Represents a float (floating point number)
Expand Down Expand Up @@ -77,14 +78,17 @@ defmodule Ash.Type.Float do
Enum.reduce(constraints, [], fn
{:max, max}, errors ->
if value > max do
[[message: "must be less than or equal to %{max}", max: max] | errors]
[[message: error_message("must be less than or equal to %{max}"), max: max] | errors]
else
errors
end

{:min, min}, errors ->
if value < min do
[[message: "must be more than or equal to %{min}", min: min] | errors]
[
[message: error_message("must be greater than or equal to %{min}"), min: min]
| errors
]
else
errors
end
Expand All @@ -93,14 +97,23 @@ defmodule Ash.Type.Float do
if value < less_than do
errors
else
[[message: "must be less than %{less_than}", less_than: less_than] | errors]
[
[message: error_message("must be less than %{less_than}"), less_than: less_than]
| errors
]
end

{:greater_than, greater_than}, errors ->
if value > greater_than do
errors
else
[[message: "must be more than %{greater_than}", greater_than: greater_than] | errors]
[
[
message: error_message("must be greater than %{greater_than}"),
greater_than: greater_than
]
| errors
]
end
end)

Expand Down Expand Up @@ -137,7 +150,7 @@ defmodule Ash.Type.Float do
if ^expr > ^max do
error(
Ash.Error.Changes.InvalidChanges,
message: "must be less than or equal to %{max}",
message: ^error_message("must be less than or equal to %{max}"),
vars: %{max: ^max}
)
else
Expand All @@ -150,7 +163,7 @@ defmodule Ash.Type.Float do
if ^expr < ^min do
error(
Ash.Error.Changes.InvalidChanges,
message: "must be greater than or equal to %{min}",
message: ^error_message("must be greater than or equal to %{min}"),
vars: %{min: ^min}
)
else
Expand All @@ -165,7 +178,7 @@ defmodule Ash.Type.Float do
else
error(
Ash.Error.Changes.InvalidChanges,
message: "must be greater than %{less_than}",
message: ^error_message("must be less than %{less_than}"),
vars: %{less_than: ^less_than}
)
end
Expand All @@ -178,7 +191,7 @@ defmodule Ash.Type.Float do
else
error(
Ash.Error.Changes.InvalidChanges,
message: "must be greater than %{greater_than}",
message: ^error_message("must be greater than %{greater_than}"),
vars: %{greater_than: ^greater_than}
)
end
Expand Down
Loading
Loading