No validation of field until schema is complete ?
Hello,
I am an experienced developer, unfortunately not experienced in TypeScript. I am in the process of learning, so sorry if this is trivial.
I am trying to set up a form for a entity whose schema varies on some
type
field. I think I have found a way to express this in Arktype, but the validation doesn't seem to work as intended.
Here is a minimal way to reproduce:
The errors only show the fact that type
is wrong, but won't tell a thing about the name being incorrect.
Is this the wrong way to have discriminated unions ?2 Replies
Counterintuitively, this is actually a feature of discriminated unions.
What is means for a union to be discriminated is you can check a specific property and know which branch to jump to in constant time.
If that discriminating value isn't present, the only feedback we can give in constant time is that the value needs to be present so that we can further evaluate whether the other properties are correct.
You might imagine in this case, you could construct the additional error message about
name
by iterating over each branch and checking them and that context could be helpful, but the point of a discriminated union is to give concise, tailored feedback based on the actual branch you're trying to create.
In many other cases, trying to provide context like this when the discriminant doesn't exist would lead to very convoluted errors that would be much harder to understand than just that the discriminator is missing.Your answer couldn't be more clear. Thank you for taking the time, hugely appreciate it. Have a nice weekend !