fastify / fastify-type-provider-json-schema-to-ts

A Type Provider for json-schema-to-ts
MIT License
35 stars 8 forks source link

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

Open its-dibo opened 11 months ago

its-dibo commented 11 months ago

Prerequisites

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 commented 11 months ago

What happens if you do additionalProperties: false

its-dibo commented 11 months ago

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