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

this package adds non-existing props to the generated type #65

Open
2 tasks done
its-dibo opened this issue Oct 12, 2023 · 2 comments · May be fixed by #103
Open
2 tasks done

this package adds non-existing props to the generated type #65

its-dibo opened this issue Oct 12, 2023 · 2 comments · May be fixed by #103

Comments

@its-dibo
Copy link

its-dibo commented Oct 12, 2023

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.24.0

Plugin version

2.2.2

Node.js version

18.x

Operating system

Linux

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

22

Description

following your example:

const plugin: FastifyPluginAsyncJsonSchemaToTs = async function (
  fastify,
  _opts
) {
  fastify.get(
    "/",
    {
      schema: {
        body: {
          type: "object",
          properties: {
            x: { type: "string" },
            y: { type: "number" },
            z: { type: "boolean" },
          },
          required: ["x", "y", "z"],
        } as const,
      },
    },
    (req) => {
      /// The `x`, `y`, and `z` types are automatically inferred
      const { x, y, z } = req.body;
    }
  );
};

now req.body has the following type

{
  // extra property that allows arbitrary props that don't exist in the original schema
  [x: string]: unknown;
  x: string;
  y: string;
  z: string;
}

remove the extra prop, and you may add an option to allow arbitrary props

Steps to Reproduce

.

Expected Behavior

No response

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 12, 2023

What happens if you do additionalProperties: false

@its-dibo
Copy link
Author

What happens if you do additionalProperties: false

this works, please add this note to the docs.
also, I think it may be better to allow additional properties only if it set explicitly to trye

@bcomnes bcomnes linked a pull request Dec 18, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants