#Collection Schema

8 messages · Page 1 of 1 (latest)

frank sequoia
#
const noticeboardCollection = defineCollection({
  type: "content",
  schema: ({ image }) =>
    z.object({
      draft: z.boolean(),
      title: z.string(),
      published: z.date(),
      bannerImg: z
        .object({
          discriminant: z.boolean(),
          value: z
            .object({
              src: image(),
              alt: z.string(),
            })
            .optional(),
        })
        .refine(
          (bannerImg) =>
            bannerImg.discriminant ? bannerImg.value !== undefined : true,
          {
            message: "Banner image must be provided",
          },
        ),
      category: z.array(reference("categories")).default([]),
      tag: z.array(z.string()),
      postedBy: z.string().default("Admin"),
    }),
});

This is my collection schema .
This is my markdoc file

---
draft: false
title: Examination 2nd Semester 2023-27
published: 2024-10-20T20:40:00.000Z
bannerImg:
  discriminant: false
category:
  - news
tag:
  - exam
  - 2nd semester
  - finals
postedBy: admin
---
# Final Exams 2nd Semester

This is the error I'm getting

noticeboard → examination-2nd-semester-2023-27.mdoc frontmatter does not match collection schema.
bannerImg.value: Required
gleaming fractalBOT
#

Hi @frank sequoia. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message you're seeing is due to the refine method in your bannerImg schema. This method is used to add custom validation logic. In your case, it checks if discriminant is true, then value must not be undefined.

In your markdown file, you have discriminant set to false, which means value can be undefined according to your schema. However, the error message suggests that value is required, which contradicts your schema.

This could be due to a misunderstanding of how the refine method works. The refine method returns true if the validation passes and false otherwise. In your case, you're returning true when bannerImg.discriminant is false or bannerImg.value is not undefined. This means that if bannerImg.discriminant is true and bannerImg.value is undefined, the validation fails, which is why you're seeing the error message.

If you want value to be optional when discriminant is false, you should update your refine method to return true when bannerImg.discriminant is false or bannerImg.value is not undefined. Here's how you can do it:

bannerImg: z
  .object({
    discriminant: z.boolean(),
    value: z
      .object({
        src: image(),
        alt: z.string(),
      })
      .optional(),
  })
  .refine(
    (bannerImg) =>
      !bannerImg.discriminant || (bannerImg.discriminant && bannerImg.value !== undefined),
    {
      message: "Banner image must be provided",
    },
  ),

This way, if discriminant is false, the validation will pass regardless of the value of value. If discriminant is true, value must not be undefined for the validation to pass.

#

@frank sequoia Kapa.ai is still learning and improving, please let me know how I did by reacting below.

frank sequoia
#

@gleaming fractal

        .refine(
          (bannerImg) =>
            bannerImg.discriminant === true
              ? bannerImg.value !== undefined
              : true,
          {
            message: "Banner image must be provided",
          },
        ),

I tried this one why this one doen't work?

gleaming fractalBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The code you provided should work as expected. It checks if bannerImg.discriminant is true, then bannerImg.value must not be undefined. If bannerImg.discriminant is not true (i.e., it's false), then the validation passes regardless of the value of bannerImg.value.

However, the error message you're seeing suggests that bannerImg.value is required, which means it's undefined in your markdown file when bannerImg.discriminant is true.

Please double-check your markdown file and ensure that bannerImg.value is provided when bannerImg.discriminant is true. If bannerImg.discriminant is false, bannerImg.value can be undefined according to your schema.

If you're still seeing the error after checking your markdown file, there might be a bug in Astro or in your code elsewhere. In that case, I would recommend reaching out to the Astro community on Discord (https://astro.build/chat) or filing a bug report on the Astro GitHub repository (https://github.com/withastro/astro/issues/).

For more information on defining a collection schema in Astro, you can refer to the Astro documentation (https://docs.astro.build/en/guides/content-collections/#defining-a-collection-schema).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: