Morphs & More
Sometimes, data at the boundaries of your code requires more than validation before it's ready to use.
Morphs allow you to arbitrarily transform the shape and format of your data.
Morphs can be piped before, after or between validators and even chained to other morphs.
This is a good start, but there are still a couple major issues with our morph.
What happens if we pass a string that isn't valid JSON?
Despite what JSON.parse
might have you believe, throwing exceptions and returning any
are not very good ways to parse a string. By default, ArkType assumes that if one of your morphs or narrows throws, you intend to crash.
If you do happen to find yourself at the mercy of an unsafe API, you might consider wrapping your function body in a try...catch
.
Luckily, there is a built-in API for wrapping pipe
d functions you don't trust:
The best part about pipe
is that since any Type
is root-invokable, Type
s themselves are already morphs! This means validating out parsed output is as easy as adding another pipe:
At this point, our implementation is starting to look pretty clean, but in many cases like this one, we can skip straight to the punch line with one of ArkType's many built-in aliases for validation and parsing, string.json.parse
:
If you've made it this far, congratulations! You should have all the fundamental intuitions you need to bring your types to runtime ⛵
Our remaining docs will help you understand the trade offs between ArkType's most important APIs so that no matter the application, you can find a solution that feels great to write, great to read, and great to run.