z.codec
for freely converting between isomorphic data structures
#4102
stefan-wullems
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey there, love v4 version. I was just looking for a zod plugin that would convert a zod schema to json schema for defining RxDB collections. I felt very lucky to come across the new beta update which includes exactly that.
My issue
I want to do something very basic, this is my schema
Now what I want to do specifically has to do with the topics field. It's now as
z.array(z.string())
, which is fine, but ideally I want it to bez.set(z.string()
because it simplifies my domain logic in a trivial way, but still I want it 👀 . However,z.set(z.string())
is not supported by.toJSONSchema
because it is "unrepresentable".I tried doing
z.array(z.string()).transform((topics) => new Set(topics))
to start with a type that is representable and then do the conversion, but then when I pass this schema to.toJSONSchema
I get the following error "Transforms cannot be represented in JSON Schema".I can surpress these errors by calling
.toJSONSchema
with the{ unrepresentable: "any"}
option, but then I'd lose my type info.It makes sense why it doesn't work. If the validation is used to validate data coming in from the database, such a transform would be fine, but then once I try to store it again, the transform doesn't make sense anymore. I wonder if this is the rationale behind it.
Of course I can solve this problem by having two separate schema's, one storage schema and one domain schema. The storage schema follows the rules that
.toJSONSchema
imposes while the domain schema can parse data to a more convenient format for business logic. This seems like a lot of repeating myself however.What I want
Anyway, what I would really want is to have a schema that has codec like behavior (e.g. elm-codec, that allows me to convert freely between isomorphic data representations. Meaning I can use it as both a storage schema and a domain schema at the same time. Specifying how I want the two to map to each other.
An example would be something like this:
This would be useful to me in other scenarios as well.
Beta Was this translation helpful? Give feedback.
All reactions