Applying constraints to embedded union types
I have a resource when an embedded array of union types:
When doing a create or update on the parent (MyApi.Package) with an option that has an invalid key (see constraint), it's not returning
%Ash.Error.Invalid{}
as expected. I've tried applying a validation in lieu of an attribute constraint, but the validation is never called.
Additionally, the identity is not enforced.9 Replies
So if you provide a record with an invalid key what happens? Also yeah I think you're going to need to validate the uniqueness yourself
or we're going to need to add explicit support for unique checking unions if they all have a key and all have an identity on it
Hm….something seems strange there
Oh
You’re providing a structure there
It assumes you got structs by casting them
To test the casting you’d want to provide something like
[%{key: “not a valid key”, type: :boolean, value: false}]
Wow, ok. Using a map instead of a struct, the constraints and the validations are applied.
Are the validations supposed to be skipped if there's no casting?
Yeah, the idea is that you provided an already valid value if providing structs (because the outside world can’t provide a struct, like over an api)
Got it. That makes sense now. For other reasons, I've not been able to get forms working and I haven't tested the apis
Yeah, forms against unions are currently something the framework doesn’t handle for you. We can, just need to add code to
ash_phoenix
And so what you end up needing to do is essentially roll your own, switching the form behavior based on the union type.I can switch the behaviour for the union type pretty easily, but I'm not sure I'm adding forms correctly.
I'm trying to get it working with just one type to start.
The union raises the struct error, but adding the form for one of the variants leads to other errors
I think what you might need to do is do it without auto forms entirely and manage the nested forms yourself. That’s without us adding special support for it in ash_phoenix. I’m currently trying to think of a better way, and/or how we’d be able to support a dynamic config for nested forms.
I’ll have to tackle it when I’m back from vacation. In the meantime, you should be able to “roll your own” here by looping over the current values and storing a list of forms in the socket assigns, and handling validation that way.
Actually if you just don’t add a form config for it, you should just be able to loop over the field value.
And create a new form on the fly