A
arktype•3mo ago
log1st

ArkErrors serialization/deserialization

I'm implementing a very simple NestJS + ArkType bridge (like nestjs-zod or so). Currently I can't find a pretty way to serialize and deserialize ArkErrors object to use it in RPC transports for further use. Is there any "in-a-box" way? Looks like storing whole Traversal context and source data inside serialized object and monkey-patching methods doesn't look good.
4 Replies
TizzySaurus
TizzySaurus•3mo ago
There's a basic version of this in type("string").toJsonSchema() But I don't think it works once you add custom morphs etc.
log1st
log1stOP•3mo ago
It does serialize typing schema but what about ArkErrors? Or am I that blind? šŸ™‚
TizzySaurus
TizzySaurus•3mo ago
Ah yeah, sorry, I misread your message. I'm not aware of a built-in serialisation method for ArkErrors There's ArkErrors.summary which will convert it to a string, but you lose the ctx and can't go back to the ArkErrors
log1st
log1stOP•3mo ago
yup, that's not what i'm looking for. my goal is to serialize and deserialize whole ArkErrors object. As i see there's ArkError:input field in each object and it depends on the rule that failed validation. eg. "string > 2" in case of failure contains
input = {
code: 'minLength',
description: 'at least length 2',
meta: {},
rule: 2
}
input = {
code: 'minLength',
description: 'at least length 2',
meta: {},
rule: 2
}
So, theoretically, I can use error.path and error.input for storing data during serialization. As long as the Traversal config is the same between the serializer and deserializer, I see that these fields, along with data, can be removed from the object. The only problem is that I’m not sure how to properly deserialize it back while keeping all the internal logic working as expected.

Did you find this page helpful?