Skip to content

JSON schema validation returns wrong missing field on nested objects #4592

@Qocotzxin

Description

@Qocotzxin

Prerequisites

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

Fastify version

4.0.0

Plugin version

No response

Node.js version

18.14.1

Operating system

macOS

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

Ventura 13.2.1

Description

I'm using the following validation schema for my response:

const responseSchema = {
  type: "object",
  required: ["ok", "data", "message"],
  properties: {
    ok: {
      type: "boolean",
    },
    data: {
      type: "object",
      required: ["token"],
      properties: {
        token: {
          type: "string",
          nullable: true,
        },
      },
    },
    message: {
      type: "string",
    },
  },
};

export const getSchema: RouteShorthandOptions = {
  schema: {
    response: {
      200: responseSchema,
      500: responseSchema,
    },
  },
};

When trying to send the following response in my endpoint:

    return rep.status(500).send({
      ok: false,
      data: {},
      message: "Access Token creation failed.",
    });

It correctly fails because token is not present, but instead of showing the token field it returns the ok field:

{
    "statusCode": 500,
    "error": "Internal Server Error",
    "message": "\"ok\" is required!"
}

It is basically returning the first field that finds in an array of required elements which corresponds to the parent object (ok field), instead of returning the specific field that is failing validation (token).

Trying Fastify for the first time and so far the experience is great, just found out this little issue, thanks!

Steps to Reproduce

Copy-paste the schema provided in the description pass it to the endpoint and hit it with Postman or curl.

Expected Behavior

Should return the name of the field that is failing the validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions