Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema validation breaks when attachFieldsToBody is set to 'keyValues' #489

Closed
2 tasks done
smaspe opened this issue Oct 11, 2023 · 2 comments · Fixed by #492
Closed
2 tasks done

Schema validation breaks when attachFieldsToBody is set to 'keyValues' #489

smaspe opened this issue Oct 11, 2023 · 2 comments · Fixed by #492
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@smaspe
Copy link

smaspe commented Oct 11, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the regression has not already been reported

Last working version

7.7.3

Stopped working in version

8.0.0

Node.js version

18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.6

💥 Regression Report

When setting {attachFieldsToBody: 'keyValues'}, the content of the file is attached to the body, using the name of the field.

According to the documentation here: https://github.com/fastify/fastify-multipart#json-schema-body-validation, we'd expect a string.

However, since version 8.0.0 (specifically, this particular change here: c195d5f#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346L176, the object is attached as a Buffer.

Using JSON Schema, as far as I know, it is not possible to represent a Buffer object.

Steps to Reproduce

  • Install version 8.0.0
  • Try to define a body schema that validates a buffer?

Here is a short example of a server:

import fastifyMultipart from "@fastify/multipart";
import fastify from "fastify";

const app = fastify({ ignoreTrailingSlash: true });
app.register(fastifyMultipart, {
  attachFieldsToBody: "keyValues",
});

app.post(
  "/test",
  {
    schema: {
      body: {
        type: "object",
        properties: {
          myFile: { type: "string" },
        },
      },
    },
  },
  async (request, reply) => {
    reply.send();
  }
);

app.listen({ port: 3000 });

It works fine in 7.7.3. With 8.0.0, myFile is now a Buffer, and this server returns this:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "body/myFile must be string"
}

Expected Behavior

  • body.myFile should be a string, or there is a documented clear way of defining a Buffer in the JSON Schema validation
@gurgunday
Copy link
Member

Good spot, the docs shouldn’t say that

@gurgunday gurgunday added the good first issue Good for newcomers label Oct 21, 2023
@gurgunday
Copy link
Member

You can use the object type for buffers

@gurgunday gurgunday added the documentation Improvements or additions to documentation label Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants