Metadata registries don't solve some problems I've used metadata for · Issue #4319 · colinhacks/zod · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The best example is declaring "versioned" schemas I can use to support different versions of a JSON file format.
Here's a basic example of a schema for a file format with two versions, where in version 2 the dataType property gets renamed to type:
constMetadataItem=z.object({tag: z.string(),// version(schema, metadata) wraps a schema with the given metadatadataType: version(z.enum(['number','string','boolean']),{until: 2}),type: version(z.enum(['number','string','boolean']),{since: 2}),})// now we want a schemaForVersion(schema, version) function such that:constMetadataItemV1=schemaForVersion(MetadataItem,1)// somehow magically lacks the `type` propertytypeMetadataItemV1=z.infer<typeofMetadataItemV1>// also somehow magically lacks the `type` propertyconstMetadataItemV2=schemaForVersion(MetadataItem,2)// somehow magically lacks the `dataType` propertytypeMetadataItemV2=z.infer<typeofMetadataItemV2>// also somehow magically lacks the `dataType` property// Now we can write a typesafe function to normalize old versions to the latest version
Before coming up with this approach I had separate Zod schemas for each version of the file and code to convert between each, but my coworkers complained that this was too tedious and they wanted to be able to only have one declaration of a given property that can be shared across versions (until it's renamed, moved, or removed in a breaking change in a new file version).
I needed to be able to build accurate TS types for each file version from this base schema to be confident about how well our code that normalizes to the latest version is type checked.
Doing something like this is only possible when Zod schemas anywhere in the tree can be wrapped with something that contains custom type information (in this case, the until/since metadata at type time).
The best example is declaring "versioned" schemas I can use to support different versions of a JSON file format.
Here's a basic example of a schema for a file format with two versions, where in version 2 the
dataType
property gets renamed totype
:Before coming up with this approach I had separate Zod schemas for each version of the file and code to convert between each, but my coworkers complained that this was too tedious and they wanted to be able to only have one declaration of a given property that can be shared across versions (until it's renamed, moved, or removed in a breaking change in a new file version).
I needed to be able to build accurate TS types for each file version from this base schema to be confident about how well our code that normalizes to the latest version is type checked.
Doing something like this is only possible when Zod schemas anywhere in the tree can be wrapped with something that contains custom type information (in this case, the
until
/since
metadata at type time).Here's the full article about the approach I came up with: https://www.jcore.io/articles/schema-versioning-with-zod
The text was updated successfully, but these errors were encountered: