-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Content Collections] Allow Zod unions, objects, and transforms as schemas #5770
Conversation
|
Why do we have to require the use of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely in favor of supporting this but I'm not convinced we need to deprecate the current raw object support?
if ( | ||
typeof collectionConfig.schema === 'object' && | ||
!('safeParseAsync' in collectionConfig.schema) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we could allow POJOs (plain ole' JS objects) here and automatically wrap them with z.object
if necessary? Small convenience if you don't need any of the advanced z.object
features.
@matthewp @natemoo-re Alright, reasons I'm in favor of deprecating the raw object: Requiring const schema1 = {...}
const schema2 = {...}
const blog = defineCollection({
schema: { ...schema1, ...schema2, moreProperties: true },
}); Now, say schema1 needs a transform. schema1 would need to change from a POJO to an object: const schema1 = z.object({ ... }).transform(...)
const schema2 = {...}
const blog = defineCollection({
schema: ???,
}); ...and now schema1 and schema2 are difficult to combine. This problem is more pronounced when schemas come from third party libraries. Requiring schemas to be Zod types, we can always document and encourage a const schema1 = z.object({ ... }).transform(...)
const schema2 = z.object({ ... })
const blog = defineCollection({
schema: z.union([schema1, schema2, z.object({ moreProperties: true })]),
}); I'm also thinking about how much we'd explain if we document transforms or unions. Allowing POJOs, we'd need an "if you're using an object, change to a Zod object first" warning that'd be nice to avoid. These are fairly minor complaints I know! I just prefer a single standard for doing things to make features discoverable and easy-to-document. |
+1 to a single consistent API here. Omitting the |
z.object(...)
wrapper for schemasThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been convinced! It's a bit boilerplate-y for the simple use case, but I agree that increasing the documentation complexity isn't worth it.
Changes
transform
results as schemasz.object(...)
wrapper is missingWhy?
By enforcing the
z.object(...)
wrapper, we open the door for:transform
to add and modify frontmatter propertiestype
property:Testing
Add a test for Zod unions
Docs
withastro/docs#2335