You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constcreateFormDataZodSchema=(intent: string)=>{if(intent==='generate'){returnz.object({articleOutlineMarkdown: z.string().optional(),});}if(intent==='accept'){returnz.object({articleOutlineMarkdown: z.string().min(2,{message: 'Outline must be at least 2 characters.',}).refine((value)=>{try{parseToc(value);returntrue;}catch{returnfalse;}},{message:
'Must be a valid markdown document containing a table of contents.',},),});}thrownewError('Invalid intent.');};// ...exportconstaction: ActionFunction=async({ params, request })=>{constarticleId=Number(params.id);if(!articleId){thrownewError('Article ID is required.');}constformData=awaitrequest.formData();constsubmission=parse(formData,{schema: (intent)=>{returncreateFormDataZodSchema(intent);},});if(submission.value){if(submission.intent==='accept'){returnawaitacceptArticleOutline(Number(params.id),submission.value.articleOutlineMarkdown,);}if(submission.intent==='generate'){returnjson(awaitqueueGenerateArticleOutline(articleId));}returnjson(submission);}thrownewError('Invalid submission.');};
The problem is that articleOutlineMarkdown is seen as possibly undefined, despite it being behind === 'accept' check.
It feels like the proper solution here would be to have a schema with discriminate union based on the intent, but since intent data is not available in form data it cannot be done.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This is what I am doing at the moment:
The problem is that
articleOutlineMarkdown
is seen as possibly undefined, despite it being behind=== 'accept'
check.It feels like the proper solution here would be to have a schema with discriminate union based on the intent, but since intent data is not available in form data it cannot be done.
Beta Was this translation helpful? Give feedback.
All reactions