fastify-type-provider-json-schema-to-ts icon indicating copy to clipboard operation
fastify-type-provider-json-schema-to-ts copied to clipboard

this package adds non-existing props to the generated type

Open its-dibo opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] 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

its-dibo avatar Oct 12 '23 12:10 its-dibo

What happens if you do additionalProperties: false

Uzlopak avatar Oct 12 '23 12:10 Uzlopak

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

its-dibo avatar Oct 12 '23 13:10 its-dibo