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

Request body is typed but not checked at runtime #902

Open
GauBen opened this issue Dec 6, 2023 · 6 comments
Open

Request body is typed but not checked at runtime #902

GauBen opened this issue Dec 6, 2023 · 6 comments

Comments

@GauBen
Copy link

GauBen commented Dec 6, 2023

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

@m4rvr
Copy link

m4rvr commented Dec 6, 2023

I'm pretty sure it's related to #799 and #896

@ardatan
Copy link
Owner

ardatan commented Dec 6, 2023

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

@m4rvr
Copy link

m4rvr commented Dec 6, 2023

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

@MrOrz
Copy link

MrOrz commented Nov 17, 2024

Still reproducible on 0.8.3. User can input anything without getting Bad Request errors.

@ardatan
Copy link
Owner

ardatan commented Nov 17, 2024

As I said earlier, if you don't use TypeBox to build the schema, runtime validations won't be available. We did not want to embed an extta validation system like AJV which will considerably increase the bundle size. So if you want to have this, you need to use TypeBox.

@MrOrz
Copy link

MrOrz commented Nov 17, 2024

Thanks @ardatan for the prompt response! I inserted a few console.log and found that my requests fail the Typebox type guards. So in order to use validation, it seems that Typebox is a must.

It would be great if such constraint is included in the Type-Safety & Validation document. The page only mentions about the validation and bad requests, but the description can hardly connect to TypeBox.

MrOrz added a commit to cofacts/rumors-api that referenced this issue Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants