fastify / fastify-type-provider-typebox

A Type Provider for Typebox
MIT License
154 stars 27 forks source link

Fastify fails to work with FormatRegistry from TypeBox #132

Closed gjstreicher closed 8 months ago

gjstreicher commented 8 months ago

Prerequisites

Fastify version

4.26.2

Plugin version

No response

Node.js version

21.7.1

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04.4 TLS x86_64

Description

TypeBox provides FormatRegstry which allows extending its string validation to custom formats.

When attempting to use a custom FormatRegistry registered string format in the schema of a Fastify endpoint, Fastify fails to run.

I'm not sure if this is a fastify or fastify-type-provider-typebox issue, but I suspect the issue lies with the type provider.

Steps to Reproduce

I've created a reproduction of this as a Git repo here.

The README contains the steps to reproduce the issue.

Expected Behavior

Fastify shouldn't throw a runtime error of FastifyError [Error]: Failed building the validation schema for POST: /asdf, due to error unknown format "foo" ignored in schema at path "#/properties/bar"

gjstreicher commented 8 months ago

I've just found a solution to the above problem.

When creating the fastify app I need to register the typebox validator compiler:

import fastify from 'fastify';

import {
  TypeBoxTypeProvider, Type, FormatRegistry, TypeBoxValidatorCompiler,
} from '@fastify/type-provider-typebox';

const app = fastify({ logger: true })
  .setValidatorCompiler(TypeBoxValidatorCompiler)
  .withTypeProvider<TypeBoxTypeProvider>();

  ...