Detecting validation errors across different ArkType versions/instances
Hey All,
I've been using ArkType a bunch, across different packages. I've been loving it.
One problem I've run into, is sometimes I'll have a situation where I have a type living in one package that I import into another package, and I'll be doing an
instanceof type.errors and things don't work right because they aren't instances of the same imports.
In the case where the type import isn't the same (e.g. different version, different import), what's the idiomatic/canonical way to check for errors? I know I could always look through properties on the returned object (e.g. problems and summary), but is there something better?
Thanks.4 Replies
There's
instance of ArkErrors but I don't think that'll help since type.errors = ArkErrors
So problems and summary would become the canonical wayGlad to hear you're enjoying the library 🙂 There is actually a way this is handled internally via a utility called
hasArkKind that you can import from @ark/schema (a dependency of arktype). You can use it like hasArkKind(result, "errors") and it will narrow to ArkErrors.
Also if you don't want to add a dependency directly, you can check for the key " arkKind" (note the leading space) and see if it has a value of "errors".
That said, I would be cautious if you're somehow getting multiple versions of arktype across your projects since this could lead to other unexpected errors e.g. trying to compose types from different versions.Rad, thanks. Yah, it's definitely something I've been careful about, but noticed (mainly due to the pain and suffering of debugging).. the biggest offender was a package that used to be part of a monorepo, but was then split off.
Of course it was checking for validation errors before using input.. but since
instanceof type.errors wasn't true.. it would carry on its merry way.. which caused a bunch of problems.yeah that could definitely lead to some problems if your data is replaced by ArkErrors 😅