# SPDX-FileCopyrightText: 2019 ash contributors # # SPDX-License-Identifier: MIT defmodule Ash.Resource.Validation.DataOneOf do @moduledoc false use Ash.Resource.Validation import Ash.Gettext alias Ash.Error.Changes.InvalidAttribute import Ash.Expr @opt_schema [ values: [ type: {:wrap_list, :any}, required: true ], attribute: [ type: :atom, required: true, hide: true ] ] opt_schema = @opt_schema defmodule Opts do @moduledoc false use Spark.Options.Validator, schema: opt_schema end @impl true def init(opts) do case Opts.validate(opts) do {:ok, opts} -> {:ok, Opts.to_options(opts)} {:error, error} -> {:error, Exception.message(error)} end end @impl true def supports(_opts), do: [Ash.Changeset] @impl true def validate(subject, opts, _context) do value = Ash.Changeset.get_data(subject, opts[:attribute]) if Enum.any?( opts[:values], &Comp.equal?(&1, value) ) do :ok else {:error, [ value: Ash.Resource.Validation.maybe_redact(subject, opts[:attribute], value), field: opts[:attribute] ] |> with_description(opts) |> InvalidAttribute.exception()} end end @impl true def atomic(changeset, opts, context) do value = expr(^ref(opts[:attribute])) error_value = if Ash.Resource.Validation.should_redact?(changeset, opts[:attribute]) do Ash.Helpers.redact(nil) else value end {:atomic, [opts[:attribute]], expr(^value not in ^opts[:values]), expr( error( Ash.Error.Changes.InvalidAttribute, %{ field: ^opts[:attribute], value: ^error_value, message: ^(context.message || error_message("expected one of %{values}")), vars: %{values: ^Enum.map_join(opts[:values], ", ", &to_string/1)} } ) )} end @impl true def describe(opts) do [ message: error_message("expected one of %{values}"), vars: [values: Enum.map_join(opts[:values], ", ", &to_string/1)] ] end end