Skip to content
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

Zod not working correcly in V3 schema #2927

Open
Phu1237 opened this issue Dec 23, 2024 · 4 comments
Open

Zod not working correcly in V3 schema #2927

Phu1237 opened this issue Dec 23, 2024 · 4 comments

Comments

@Phu1237
Copy link

Phu1237 commented Dec 23, 2024

Environment

Nuxt project info: 11:18:23 AM



Reproduction

  1. Using the config below: content.config.ts
import {defineCollection, defineContentConfig, z} from '@nuxt/content'

export default defineContentConfig({
    collections: {
        blog: defineCollection({
            // Load every file inside the `content` directory
            source: 'blog/**',
            // Specify the type of content in this collection
            type: 'page',
            schema: z.object({
                badge: z.string(),
                image: z.string(),
                tags: z.array(z.string()),
                published: z.boolean(),
                createdAt: z.string().datetime(),
                updatedAt: z.string().datetime(),
            })
        }),
    }
})
  1. Code
        if (options.published !== undefined) {
            // zod use 1 and 0 as boolean
            const published = options.published ? 1 : 0;
            query = query.where("published", '=', published);
        }
        if (options.latest !== undefined) {
            query = query.order('updatedAt', "DESC")
        }

Image

  1. Markdown
---
title: Example title
description: This is example description
badge:
image: /assets/images/coming-soon.jpg
tags:
  - Example
published: false
createdAt: 2025-01-01 00:00
updatedAt: 2025-01-01 00:00
---

## Example title

Nothing here yet. Comeback later.

Bugs:
1. order() not able to use the field in schemas
2. z.boolean() result to numberr 0/1 not boolean false/ true

Describe the bug

1. Bug 1: order() not able to use the field in schemas
When using order with field in schemas it show the below error

Argument of type 'string' is not assignable to parameter of type 'keyof Collections[T]'.
Type '"updatedAt"' is not assignable to type '"extension" | "path" | "title" | "description" | "seo" | "body" | "navigation" | "id" | "stem" | "meta"'.
Image

2. Bug 2: z.boolean() result to numberr 0/1 not boolean false/ true
Image

Additional context

I also see that nuxt/content don't have any total attribute when get from collection so that I can't create paginate and skip() will be useless because it can't count and paginate

Logs

@farnabaz
Copy link
Member

Do you mind providing a simple project to reproduce it? a Github project would be great

@Phu1237
Copy link
Author

Phu1237 commented Dec 23, 2024

@farnabaz
Thank you for your response.
I will do it and send it to you later in this issue in 1-3 days from now.

@Phu1237
Copy link
Author

Phu1237 commented Dec 23, 2024

@farnabaz
Hi!
I want to re-clarify some information:

- Bug 1: NOT A BUG
I found out that because I've created a composable and not all defined collections have the updatedAt attribute
=> The error is correct. The field must be in all collections to use

- Bug 2: I have created a project to reproduce it https://github.com/Phu1237/nuxt-content-demo
Output when using console.log("published", data.value?.published, typeof data.value?.published);
Image

Please help me to check it
Thank you!

@Phu1237
Copy link
Author

Phu1237 commented Dec 23, 2024

@farnabaz
While research, I've found out something more

1. The field in where() is not recommend correctly
Image
I think this may be a missing type update: /src/types/query.ts at Line 14 and Line 20
It is where(field: string...)
Other places are PageCollections[T] | string already

2. Sometimes, the last line of markdown frontmatter will have \r
I have added it to the reproducing project: https://github.com/Phu1237/nuxt-content-demo

---
title: Example title
description: This is example description
badge:
image: /assets/images/coming-soon.jpg
tags:
  - Example
published: true
createdAt: 2025-01-01 00:00
updatedAt: 2025-01-01 00:00
---

Image

3. When reading the code, I saw that you have added the count() function to count but it is not on the document yet
I don't know if you have a plan to add it or not but I hope you will add this to the document.
This will help people like me when researching the pagination function (without the need to read the library code)

Thank you for your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants