How can I serialize and validate a request body that contains bigint in fastify with typebox?

183 views Asked by At

I want to serialize and validate a request body that contains a bigint item with @sinclair/typebox as type provider.

For example:

const bodySchema = Type.Object({
    currency: Type.String(),
    amount: Type.BigInt(),
  });

type BodyType = Static<typeof bodySchema>;

fastify.post<{ Body: BodyType }>(
    '/sendMoney',
    sendMoneyOpts,
    async (request, reply) => {
      return reply.status(200).send({
        amount: request.body.amount,
      });
    }
  );

But I get this error:

Failed building the validation schema for POST: /sendMoney, due to error schema is invalid: data/properties/amount/type must be equal to one of the allowed values, data/properties/amount/type must be array, data/properties/amount/type must match a schema in anyOf

I if I use Type.Integer() instead, request.body.amount's type will become number.

Do you have any idea how can I parse bigint from request and also get the bigint type with TypeScript for request.body.amount ?

0

There are 0 answers