fastify / fastify-cli

Run a Fastify application with one command!
MIT License
657 stars 163 forks source link

Can't add schema to route #686

Closed tjapa closed 9 months ago

tjapa commented 9 months ago

Prerequisites

Fastify version

4.25.2

Plugin version

No response

Node.js version

20.10.0

Operating system

Linux

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

6.6.6-arch1-1

Description

I started the project with the command fastify generate my-project --lang=ts

Then I changed the file root.ts:

import { FastifyPluginAsync } from 'fastify'

const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
  fastify.get(
    '/',
    {
      schema: {
        body: { type: 'object', properties: { x: { type: 'string' } } },
      },
    },
    async function(request, reply) {
      return { root: true }
    },
  )
}

export default root

After that I get this error:

[TypeScript] 19:15:37 - Found 0 errors. Watching for file changes.
[App] FastifyError [Error]: Body validation schema for GET:/ route is not supported!
[App]     at validateSchemaBodyOption (/tmp/teste/teste/node_modules/fastify/lib/route.js:569:11)
[App]     at Object.route (/tmp/teste/teste/node_modules/fastify/lib/route.js:210:7)
[App]     at Object.prepareRoute (/tmp/teste/teste/node_modules/fastify/lib/route.js:167:18)
[App]     at Object._get [as get] (/tmp/teste/teste/node_modules/fastify/fastify.js:259:34)
[App]     at root (/tmp/teste/teste/dist/routes/root.js:4:13)
[App]     at Plugin.exec (/tmp/teste/teste/node_modules/avvio/plugin.js:130:19)
[App]     at Boot.loadPlugin (/tmp/teste/teste/node_modules/avvio/plugin.js:272:10)
[App]     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[App]   code: 'FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED',
[App]   statusCode: 500
[App] }

Other bug happens when I run fastify eject --lang:ts The commands generate a .js file.

Steps to Reproduce

Follow the description.

Expected Behavior

No response

tjapa commented 9 months ago

Sorry. The get method doesn't have body. My mistake.