defmodule Ash.Resource.Validation.ArgumentIn do @moduledoc false @opt_schema [ argument: [ type: :atom, required: true, doc: "The argument to check" ], list: [ type: :any, required: true, doc: "The list of values that the argument must be in" ] ] use Ash.Resource.Validation alias Ash.Error.Changes.InvalidArgument @impl true def init(opts) do case Spark.OptionsHelpers.validate(opts, @opt_schema) do {:ok, opts} -> {:ok, opts} {:error, error} -> {:error, Exception.message(error)} end end @impl true def validate(changeset, opts) do value = Ash.Changeset.get_argument(changeset, opts[:argument]) if value in opts[:list] do :ok else {:error, [value: value, field: opts[:argument]] |> with_description(opts) |> InvalidArgument.exception()} end end @impl true def describe(opts) do [ message: "must equal %{value}", vars: [field: opts[:argument], value: opts[:value]] ] end end