#schema type
11 messages · Page 1 of 1 (latest)
Hi @sacred thunder. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can define a collection schema using the defineCollection function. If you want to add an image type to your schema, you can use the image() helper function. This function has been moved to a callback where the schema is defined in Astro 2.2. Here's an example of how you can use it:
import { defineCollection, z } from "astro:content"
const blog = defineCollection({
schema: ({ image }) =>
z.object({
image: image().refine((img) => img.width >= 200, {
message: "image too small",
}),
}),
})
In this example, the image() helper function is used to define an image type in the schema. The refine method is used to add a validation rule to the image, ensuring that its width is at least 200 pixels.
Please note that this callback-based schema assignment is currently only available with experimental assets enabled. If you are not using the image() helper, you can continue to assign the schemas directly to the schema property on the collection.
For more information, you can refer to the Astro 2.2 blog post (https://astro.build/blog/astro-220/).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@sacred thunder kapa.ai is still learning and improving, please let us know how it did by reacting below
@valid musk
return z.object({
title: z.string(),
date: z.coerce.date(),
description: z.string(),
tags: z.array(z.string()),
draft: z.boolean().default(false),
banner: image(),
imageTop: z
.object({
src: z.string(),
})
.optional(),
});
how doi pass imageTop in mdx frontmatter?
Let me check the knowledge base for you. One moment! :books:
In your MDX frontmatter, you can pass imageTop as an object with a src property. Here's an example of how you can do it:
---
title: 'My Post Title'
date: '2022-09-21'
description: 'This is a description of my post.'
tags: ['astro', 'mdx', 'frontmatter']
draft: false
banner: '/path/to/banner/image.png'
imageTop:
src: '/path/to/imageTop.png'
---
# My Post Content
This is the content of my post.
In this example, imageTop is an object with a src property that points to the location of the image. The path can be either a local path (relative to the project root) or a URL to an external image.
Please note that the path to the image should be correct and the image should exist at that location for it to be displayed correctly.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@sacred thunder kapa.ai is still learning and improving, please let us know how it did by reacting below
@valid musk and if i have alt with src?
Let me check the knowledge base for you. One moment! :books:
If you want to include an alt attribute along with src for imageTop, you can modify your schema to include alt as a string. Here's how you can do it:
return z.object({
title: z.string(),
date: z.coerce.date(),
description: z.string(),
tags: z.array(z.string()),
draft: z.boolean().default(false),
banner: image(),
imageTop: z
.object({
src: z.string(),
alt: z.string(),
})
.optional(),
});
Then, in your MDX frontmatter, you can pass imageTop as an object with src and alt properties:
---
title: 'My Post Title'
date: '2022-09-21'
description: 'This is a description of my post.'
tags: ['astro', 'mdx', 'frontmatter']
draft: false
banner: '/path/to/banner/image.png'
imageTop:
src: '/path/to/imageTop.png'
alt: 'Description of the image'
---
# My Post Content
This is the content of my post.