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 · 13 comments
Open

Zod not working correcly in V3 schema #2927

Phu1237 opened this issue Dec 23, 2024 · 13 comments

Comments

@Phu1237
Copy link

Phu1237 commented Dec 23, 2024

Environment

Nuxt project info: 11:18:23 AM


  • Operating System: Linux
  • Node Version: v20.12.2
  • Nuxt Version: 3.14.1592
  • CLI Version: 3.17.1
  • Nitro Version: 2.10.4
  • Package Manager: yarn@1.22.22
  • Builder: -
  • User Config: default
  • Runtime Modules: @nuxt/ui-pro@3.0.0-alpha.10, @nuxtjs/i18n@9.1.1, @nuxt/content@3.0.0-alpha.8
  • Build Modules: -

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!

@farnabaz
Copy link
Member

farnabaz commented Jan 7, 2025

@Phu1237 The issue with \r at the end of the last yaml field should be resolved in the latest commit release. You can check with

npm i https://pkg.pr.new/@nuxt/content@bedfbc1

@Phu1237
Copy link
Author

Phu1237 commented Jan 7, 2025

@farnabaz
Thank you for your hard work!
First I want to verify this bug is fixed in the new version

As tested using https://pkg.pr.new/@nuxt/content@bedfbc1
I have met the below error when using my Windows 10 PC
TypeError: Cannot open database because the directory does not exist

Environment


  • Operating System: Windows_NT
  • Node Version: v22.11.0
  • Nuxt Version: 3.15.0
  • CLI Version: 3.17.2
  • Nitro Version: 2.10.4
  • Package Manager: yarn@1.22.22
  • Builder: -
  • User Config: default
  • Runtime Modules: @nuxt/ui-pro@3.0.0-alpha.10, @nuxtjs/i18n@9.1.1, @nuxt/content@3.0.0-alpha.8
  • Build Modules: -

Reproduction

I have tested each commit and found out that it starts to happen from 8a9af69

The below result is tested by using https://pkg.pr.new/@nuxt/content@bedfbc1
I have been using console.log to log the file name from dist\runtime\internal\sqlite.js

Image

Image


My temporary fix

Replace the code back to db = new Database(filename);

Please help me to check this
Thank you!

@Phu1237
Copy link
Author

Phu1237 commented Jan 7, 2025

@farnabaz
To prevent this issue is hard to read. I want to summary this issue till now:

Bugs:

  • z.boolean() result to number 0/1 not boolean false/ true
  • The field in where() is not recommend correctly
  • Error when using yarn dev in Windows 10 PC TypeError: Cannot open database because the directory does not exist (New) (Zod not working correcly in V3 schema #2927 (comment))
  • The nuxt/content not working with bun when using docker bun image to build (The fixing PR is not fixing this yet and I will create a new issue to reproduce for this later)

Bugs (Fixed):

  • Sometimes, the last line of markdown frontmatter will have \r

Good if have:

  • When reading the code, I saw that you have added the count() function to count but it is not on the document yet

Not a bugs:

  • order() not able to use the field in schemas

Thank you for your hard work!

@farnabaz
Copy link
Member

farnabaz commented Jan 7, 2025

Thanks for summarizing the issue.

Error when using yarn dev in Windows 10 PC TypeError: Cannot open database because the directory does not exist (New) (#2927 (comment))

Does this happen in dev mode or the built version? If it's in dev mode could you try with the built version?

@Phu1237
Copy link
Author

Phu1237 commented Jan 8, 2025

@farnabaz
I have re-tried both yarn dev and yarn build

  • Dev mode
    Image

  • built version
    Image

Both throw the same error

Copy link
Member

farnabaz commented Jan 8, 2025

@Phu1237 Should be fixed in latest commit release npm i https://pkg.pr.new/@nuxt/content@85010c1

Copy link
Member

farnabaz commented Jan 8, 2025

@Phu1237 regarding Bun in docker could you provide a reproduction repo for it? 🙏

@Phu1237
Copy link
Author

Phu1237 commented Jan 8, 2025

@farnabaz
I will provide a reproduction repo tomorrow because it is late now in my place and I have some problem when trying to install it

But I can provide you with this information first and I hope it will provide some useful information before I can provide a fully reproduction repo

Below is my screenshot

Steps to reproduce (it error on both wsl and docker so I will provide this first):

  1. Step 1: Using Windows
  2. Step 2: Install Nuxt using bun
  3. Step 3: bun add @nuxt/content@next (This is success on Windows and project is working)
  4. Step 4: Copy to WSL
  5. Step 5: bun install

Install directly using WSL from the beginning failed when running command at Step 3 as the same error below
Docker shows the same too but I will provide a reproduced repo tomorow

Image

It seems like there is some problem with bun Linux only

Refer:


Error when using yarn dev in Windows 10 PC TypeError: Cannot open database because the directory does not exist

I also verified this bug is fixed in new version
Thank you for your work!

@Phu1237
Copy link
Author

Phu1237 commented Jan 10, 2025

@farnabaz

regarding Bun in docker could you provide a reproduction repo for it? 🙏

Hi! I have create a reproduce repo for this: https://github.com/Phu1237/nuxt-content-docker
Please help me to check it
Thank you!

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