Closed MOZGIII closed 9 months ago
Maps are not handled properly
it is not clear how to represent them
Pick one :)
It doesn't matter that it has a preprocess if it can't be guaranteed to be used all the time.
Pick one :)
Ok, fair.
It doesn't matter that it has a preprocess if it can't be guaranteed to be used all the time.
I'd be fine with an opt-in, since either way we build schema explicitly - maybe we could just have a configuration option?
I ended up using this hack:
const coercedMap = <K extends ZodTypeAny, V extends ZodTypeAny>(
keySchema: K,
valueSchema: V,
) =>
z.preprocess(
(arg, ctx) =>
arg === undefined
? new Map()
: new Map(
Object.entries(arg as {}).map(([key, val]) => [
keySchema.parse(key, {
path: [...ctx.path, key, "<key>"],
}),
val,
]),
),
// For JSON Schemas pretend we are an object rather than a map.
tweaks.jsonSchemaHack
? (z.record(keySchema, valueSchema) as never)
: z.map(keySchema, valueSchema),
);
tweaks.jsonSchemaHack
is a global that is toggled on when we export schemas, but is off for everything else.
What I'm looking for is to get rid of the tweaks.jsonSchemaHack
- if it was possible to just tell zod-to-json-schema
I am using z.map
but I want it to look like a z.record
that would be great.
Sure, wth.
Done in master
Sweet! Thanks!
Map
s are not handled properly. They can't be parsed directly from JSON, so it is not clear how to represent them. Currently, they are represented as[K, V][]
, but it would be great to have the ability to use{ [K]: V }
instead. It matters not thatK
must always be astring
with this representation, sincezod
has a neatz.preprocess
feature to fix it before parsing.