fastify / help

Need help with Fastify? File an Issue here.
https://www.fastify.io/
65 stars 8 forks source link

Help: FST_ERR_SCH_VALIDATION_BUILD using sinclayr/typebox in TypeScript #870

Closed moises-ph closed 1 year ago

moises-ph commented 1 year ago

I was trying to add a schema validation for post route in fastify like this:

import { getAllOffers, postOffer } from "../handlers/offers.handler";
import { validateToken } from "../utils/tokenValidator";
import { OfferSchema, OfferType } from "../validations/offers.validation";
import { FastifyInstance } from "fastify";

const routes = {
    getAllOffers :{
        handler : getAllOffers
    },
    postOffer : {
        handler : postOffer,
        preHandler : validateToken,
        schema : {
            body : OfferSchema
        }
    }
}

export const OffersRoutes = (fastify : FastifyInstance, options : any, done : any) => {
    fastify.get(options.url, routes.getAllOffers);

    fastify.post<{ Body : OfferType }>(options.url, routes.postOffer);

    done();
}

When I run build for running the server I get this:

FastifyError [Error]: Failed building the validation schema for POST: /offers, due to error strict mode: unknown keyword: "instanceOf"
    at Boot.<anonymous> (D:\Proyecto\PROYECTO NODENS PERRO\Nodens_Backend\Offers\Service\node_modules\fastify\lib\route.js:366:21)
    at Object.onceWrapper (node:events:627:28)
    at Boot.emit (node:events:525:35)
    at D:\Proyecto\PROYECTO NODENS PERRO\Nodens_Backend\Offers\Service\node_modules\avvio\boot.js:160:12
    at D:\Proyecto\PROYECTO NODENS PERRO\Nodens_Backend\Offers\Service\node_modules\avvio\plugin.js:275:7
    at done (D:\Proyecto\PROYECTO NODENS PERRO\Nodens_Backend\Offers\Service\node_modules\avvio\plugin.js:200:5)
    at check (D:\Proyecto\PROYECTO NODENS PERRO\Nodens_Backend\Offers\Service\node_modules\avvio\plugin.js:224:9)
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8) {
  code: 'FST_ERR_SCH_VALIDATION_BUILD',
  statusCode: 500
}

But I have made an aplication before that is very similar to this one, only schema and some business logic changes. In that aplication I used this way to add schema validation to the routes and It works!. But I do the same in this one and I don't understand why is that error.

The Schema is this:

import { Static, Type } from "@sinclair/typebox";

export const OfferSchema = Type.Object({
    Title : Type.String(),
    Description : Type.String(),
    Creation_Date : Type.Date(),
    Event_Date : Type.Date(),
    Payment : Type.Number(),
    OrganizerId : Type.Number(), // The Id for the Organizer is the Auth Id, not mongo Id. 
    Event_Ubication : Type.Optional(Type.Object({
        Career : Type.String(),
        Street : Type.String(),
        City : Type.String(),
        SiteNumber : Type.String(),
        Town : Type.String()
    })),
    Applicants : Type.Optional(Type.Array(Type.Object({
        ApplicantId : Type.Number(), // The Id for the Applicants is the Auth Id, not mongo Id. 
        PostulationDate : Type.Date()
    }))),
    Img : (Type.String()),
    Requeriments : Type.Array(Type.Object({
        Description : (Type.String())
    })),
    Vacants : Type.Number(),
    isAvailable : Type.Boolean()
});

export type OfferType = Static<typeof OfferSchema>;

Hope you can help me. Thanks :)

mcollina commented 1 year ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

moises-ph commented 1 year ago

Sure, I solved the problem using JSON Schema instead of sinclair validation. But you can tell if there's a solution for that. The code is Here in my repo

mcollina commented 1 year ago

I don't see the issue on the code linked. Can we close this then?