Improve error message on attribute that violates one_of constraint#139
Conversation
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'. ;-)
jimsynz
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the contribution! 🚀 |
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 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 =/.