Skip to content

Improve error message on attribute that violates one_of constraint#139

Merged
jimsynz merged 5 commits into
ash-project:mainfrom
travissimon:main
Feb 19, 2026
Merged

Improve error message on attribute that violates one_of constraint#139
jimsynz merged 5 commits into
ash-project:mainfrom
travissimon:main

Conversation

@travissimon
Copy link
Copy Markdown
Contributor

This commit updates the error message when a one_of constraint is violated to include the value that caused the error. This can make debugging easier when resources have a large number of states.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

I did not create a test case because this happens when the code will not compile and I was not sure how to create a test for that situation =/.

This commit updates the error message when a one_of constraint is
violated to include the value that caused the error. This can make
debugging easier when resources have a large number of states.
Sorry, I forgot that nobody told the Elixir community that the correct answer is 'tabs'. ;-)
Copy link
Copy Markdown
Contributor

@jimsynz jimsynz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR — the motivation is solid. Showing more information in these error messages definitely helps when debugging resources with many states.

I have a few concerns with the current implementation:

1. The diff calculation shows the wrong thing

all_states -- constraints[:one_of] shows states that exist in the state machine config but are missing from the attribute's one_of. It doesn't show extra values in one_of that aren't valid states. The "unexpected value" framing suggests it's showing values that shouldn't be there, but it's actually showing values that are absent.

2. Grammar nit

has a 'one_of' constraints — should be singular (constraint) or drop the article (has 'one_of' constraints).

3. Inconsistency with the enum error just below

Lines 100–107 handle the same kind of mismatch for Ash.Type.Enum and use an "expected / got" pattern showing both lists. I think matching that pattern here would be clearer and more consistent:

message: """
Expected the attribute #{attribute.name} to have the `one_of` constraint with the following values:
#{inspect(Enum.sort(all_states))}

Got

#{inspect(Enum.sort(constraints[:one_of] || []))}
"""

This shows everything needed to spot the mismatch without computing a diff that might be confusing.

4. "Unexpected value" framing is misleading

This error fires at compile time when the DSL config doesn't match the attribute constraints — it's not a runtime value error. The original "Expected the attribute to have..." phrasing was more accurate about the nature of the problem.

@jimsynz jimsynz merged commit 98f19d6 into ash-project:main Feb 19, 2026
23 checks passed
@jimsynz
Copy link
Copy Markdown
Contributor

jimsynz commented Feb 19, 2026

Thanks for the contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants