fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.67k stars 170 forks source link

[v0.31.0-rc.5] `Invalid type: Expected Object but received Object` because of `edge` runtime #602

Closed ammarriq closed 1 month ago

ammarriq commented 1 month ago

Valibot object schema doesn't seem to work with edge runtime

Steps to reproduce;

  1. install nextjs with valibot v31
  2. paste the code in page.tsx

    const schema = object({
        name: string(),
    })
    parse(schema, { name: 'Ammar' })
    1. add the statement at the top of file; export const runtime='edge'

    it will throw an error; Error: Invalid type: Expected Object but received Object

    it should also throw an error for other frameworks optiong into edge runtime

fabian-hiller commented 1 month ago

Thanks for reporting and thanks for the reproduction steps. I will have a look at it later.

fabian-hiller commented 1 month ago

I have submitted a bug report: https://github.com/vercel/edge-runtime/issues/899

There are other problems with the current plain object check, for example in #587, but I am not sure if Valibot is the problem. Normally, this should not be a problem. We could remove the input.constructor === Object check to make it less strict, but then any object that is not null will be accepted. In practice, this is usually not a problem because we never return the original object. So, strictly speaking, the validation is still safe. Therefore, I might make the validation less strict so as not to have to deal with special JS runtime behavior that we can't control.

One problematic case that just came to my mind that could lead to unexpected behavior is our looseObject and objectWithRest schemas, as they can add unexpected data to the output if the wrong object types are passed.

fabian-hiller commented 1 month ago

This is fixed in v0.31.0-rc.6