How to validate all properties without bailing early?
I am using Arktype to validate forms. I would like to show all problems with a form. I am using unions to show some form elements conditionally.
that's a real world example of what i am building. the problem is when i don't have
includedHow
set, it will not report errors for address
etc.5 Replies
Generally it is pretty difficult to control exactly how union errors are reported. Note for example that in TypeScript, it will choose arbitrarily a single branch and a single problem to report, which may or may not be realistic or helpful.
In ArkType, we try to discriminate the branches first, then report errors on the resulting branch, but generally you can imagine how convoluted it would be if we reported every possible error for every possible branch.
If you need this level of granular control over your errors, your best bet is probably to do something like what we talked about before in terms of narrowing a wider object or write your own internal logic that will check the properties you want to determine which branch you're checking, then delegate to arktype to do the full validation on that branch.
Yes I understand, however, in this example every branch shares the top fields. Wouldn't it be possible to check all common fields without going down a branch?
If that is not an option, what would a realistic example look like where I can still delegate most of the validation logic to arktype?
here's an initial go at it, however, it won't validate e.g.
arrangement
or otherPurpose
fields if there are validation errors in the left hand side of the pipe()
call:
i could get it to work like this for now:
Yeah, something like this. The key is that pipe allows you to control what parts of the object get evaluated sequentially and where you want things to stop if one of the previous piped validations fails.
The biggest recommendation I'd have is to:
1. Define the piped type separately so it doesn't get instantiated every time your object is validated.
2. Pipe to the type directly like
.pipe(outputType)
so you can avoid creating unnecessary wrappers. It also makes the output of the type introspectable at runtime.
You can see an example of this kind of chained validation in the intro here:
https://arktype.io/docs/intro/morphs-and-moreI am struggling to see how to apply the recommendations in my scenario because i have to validate different fields based on input. what am i missing?