zce / velite

Turns Markdown / MDX, YAML, JSON, or others into app's data layer with Zod schema.
http://velite.js.org
MIT License
476 stars 23 forks source link

Bundle MDX example causes "TypeError: Cannot read properties of undefined (reading 'path')" #204

Closed polynook closed 2 months ago

polynook commented 2 months ago

Steps to reproduce:

import { mdxBundle } from './mdxBundle'

...

const pages = defineCollection({
  name: 'Page',
  pattern: 'pages/**/*.mdx',
  schema: s
    .object({
      title: s.string().max(99),
      slug: s.slug('global', ['admin', 'login']),
      // body: mdx(),
      body: mdxBundle()
    })
    .transform(data => ({ ...data, permalink: `/${data.slug}` }))
})

This results in the following error:

file:///E:/new/velite/examples/nextjs/node_modules/.velite.config.compiled.mjs?t=1723124996883:57
var mdxBundle = (options2 = {}) => custom().transform(async (value, { meta: { path, content, config }, addIssue }) => {
                                                                              ^

TypeError: Cannot read properties of undefined (reading 'path')
    at Object.transform (file:///E:/new/velite/examples/nextjs/node_modules/.velite.config.compiled.mjs?t=1723124996883:57:79)
    at file:///E:/new/velite/examples/nextjs/node_modules/zod/lib/index.mjs:3736:51

I removed the destructuring of meta and console-log'ed both value and meta and both of them are undefined.

zce commented 2 months ago

Show me the reproducible repo

polynook commented 2 months ago

Here it is: https://github.com/polynook/velite-mdx-bundle-error

zce commented 2 months ago

If you want to use the meta object in the schema transform, you must use the s provided by velite.

import { s } from `velite`

s.custom()

instead of

import { custom } from 'zod'

custom()

because the meta is not built-in feature in zod