How can I configure fastify schema for multipart formdata

71 views Asked by At

I have the following files //schema.ts

import { FastifySchema } from 'fastify';
import { _MODULES } from '~utils/modules';
import {
  createFeedbackBodyRequest,
} from '~src/modules/feedback/schemas/template';
import { createFeedbackResponse } from '~src/modules/feedback/schemas/reference';
 
export const createFeedbackSchema: FastifySchema = {
  summary: 'Feedback Creation',
  description: 'Feedback creation',
  operationId: 'createFeedback',
  tags: [_MODULES.Feedback.key],
  body: createFeedbackBodyRequest,
  consumes: ['multipart/form-data'],
  // produces: ['multipart/form-data'],
  response: createFeedbackResponse,
};

//template.ts

export const createFeedbackBodyRequest = {
  type: 'object',
  required: ['email_address', 'title'],
  properties: {
    email_address: {
      type: 'string',
      format: 'email',
      nullable: false,
      minLength: 1,
      description: 'Email address',
    },
    title: {
      type: 'string',
      nullable: false,
      minLength: 1,
      description: 'Feedback title',
    },
    custom_title: {
      type: 'string',
      nullable: true,
      minLength: 1,
      description: 'Feedback custom title',
    },
    additional_details: {
      type: 'string',
      nullable: true,
      minLength: 1,
      description: 'Feedback additional details',
    },
    attachments: {
      type: 'array',
      nullable: true,
      description: 'Feedback attachment files',
      items: {
        isFile: true,
      },
    },
  },
};

I have a problem with multipart, when I am configuring it like this

limits: {
    fieldNameSize: 100, // Max field name size in bytes
    fieldSize: 10000, // Max field value size in bytes
    fields: 5, // Max number of non-file fields
    fileSize: 1000000, // For multipart forms, the max file size in bytes
    files: 5, // Max number of file fields
    headerPairs: 2000, // Max number of header key=>value pairs
    parts: 10, // For multipart forms, the max number of parts (fields + files)
  },
  addToBody: true,
  attachFieldsToBody: true,

I have the following error when I am sending a request "message": "body/email_address must be string"

How can I make it works? I have tried without addFieldsToBody, but I am getting an error that body must be an object. So the problem happens with file, don't know what to do already.

0

There are 0 answers