feTS icon indicating copy to clipboard operation
feTS copied to clipboard

Request body is typed but not checked at runtime

Open GauBen opened this issue 2 years ago • 6 comments

Describe the bug

It seems that the JSON schema given to .route() is purely informational despite what the documentation suggests:

const router = createRouter().route({
  method: 'POST',
  path: '/greetings',
  schemas: {
    request: {
      json: {
        type: 'object',
        properties: {
          name: { type: 'string' },
        },
        required: ['name'],
      },
    },
  },
  handler: async (request) => {
    const { name } = await request.json();
    console.log(typeof name); // Should print "string" according to TypeScript
    return Response.json({ hello: name });
  },
});

Running the following code will not return a server error:

fetch('http://localhost:3000/greetings', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: '{}',
})

To Reproduce Steps to reproduce the behavior:

https://stackblitz.com/edit/stackblitz-starters-zhnkw4?file=index.ts

Run yarn fetch in another terminal :

image

Expected behavior

I'm expecting the request to fail with a 400 Bad Request error

Environment:

  • fets: 0.6.6

Additional context

GauBen avatar Dec 06 '23 15:12 GauBen

I'm pretty sure it's related to #799 and https://github.com/ardatan/feTS/discussions/896

m4rvr avatar Dec 06 '23 18:12 m4rvr

I don't think this is related to those issues. It seems TypeBox validation doesn't work with regular JSON schemas.

ardatan avatar Dec 06 '23 18:12 ardatan

But I'm getting the same issues, even when using the 1:1 JSON schema for params from the docs: CleanShot 2023-12-06 at 19 24 04 CleanShot 2023-12-06 at 19 24 33

m4rvr avatar Dec 06 '23 18:12 m4rvr