sanity-io / assist

Sanity AI Assist: A plugin for Sanity Studio
MIT License
13 stars 5 forks source link

Error: Cannot read properties of undefined (reading 'map') #5

Closed hyldmo closed 11 months ago

hyldmo commented 11 months ago

Hello, my entire dashboard is crashing after installing this package. Here's the stack trace:

Error: Cannot read properties of undefined (reading 'map')
TypeError: Cannot read properties of undefined (reading 'map')
    at getBaseFields (http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:2910:87)
    at getSchemaStub (http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:2899:8)
    at http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:2866:203
    at Array.map (<anonymous>)
    at serializeSchema (http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:2866:192)
    at http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:3758:49
    at mountMemo (http://localhost:3333/node_modules/.sanity/vite/deps/chunk-XL3JYWLG.js?v=aeff8c73:13169:27)
    at Object.useMemo (http://localhost:3333/node_modules/.sanity/vite/deps/chunk-XL3JYWLG.js?v=aeff8c73:13513:24)
    at useMemo (http://localhost:3333/node_modules/.sanity/vite/deps/chunk-ZD3M55OV.js?v=aeff8c73:1091:29)
    at useRunInstructionApi (http://localhost:3333/node_modules/.sanity/vite/deps/@sanity_assist.js?v=aeff8c73:3758:42)

I suspect it is because I have some non standard list options (for instance the list parameter in my setup accepts promises for list items fetched somewhere else). Would it be possible to add a bit stricter type check on https://github.com/sanity-io/assist/blob/725164766422d2f7ff2b1c82777353ed08b70205/plugin/src/schemas/serialize/serializeSchema.ts#L86C1-L87C1 ? a simple replacement of values: type?.options?.list to values: type?.options?.list?.map or values: typeof type?.options?.list?.map === 'function' should suffice for my needs.

I apologize as I know this is non-standard/unsupported behavior, but hopefully the code change should be small enough and of the type that doesn't really introduce tech debt. Happy to submit a PR myself if that helps.

snorrees commented 11 months ago

Hey, sorry about this. Would love a PR, possibly checking for Array.isArray(type?.options?.list) as the guard?

hyldmo commented 11 months ago

Thank you for the response. I added a PR with a suggested fix now.