turkerdev / fastify-type-provider-zod

MIT License
337 stars 21 forks source link

Asynchronous refinements not working #22

Closed erwinstone closed 1 year ago

erwinstone commented 1 year ago

Schema:

export const loginBody = z.object({
  username: user.shape.username.min(2).refine(async (val) => {
    // verify that username exists in database
    return true
  }),
  password: user.shape.password.min(4),
})

Response:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Unexpected token A in JSON at position 0"
}
turkerdev commented 1 year ago

We do parse the schema as sync. Therefore, you cannot use async schema.

Async should not be used as part of the first validation strategy. This option is used to access Databases and reading them during the validation process may lead to Denial of Service Attacks to your application. If you need to run async tasks, use Fastify's hooks instead after validation completes, such as preHandler.
Fastify Docs