typescriptfastifyzod

ZOD [Error]: Schema with id 'Schema' already declared


I am new to Fastify and Typescript. I am working on adding Zod schemas for validation and I am getting this error:

/app/node_modules/fastify/lib/schemas.js:32
    throw new FST_ERR_SCH_ALREADY_PRESENT(id)
          ^
FastifyError [Error]: Schema with id 'Schema' already declared!
    at Schemas.add (/app/node_modules/fastify/lib/schemas.js:32:11)
    at SchemaController.add (/app/node_modules/fastify/lib/schema-controller.js:58:30)
    at Object.addSchema (/app/node_modules/fastify/fastify.js:601:29)
    at /app/src/index.ts:23:12
    at Generator.next (<anonymous>)
    at /app/src/index.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/app/src/index.ts:4:12)
    at main (/app/src/index.ts:32:12)
    at Object.<anonymous> (/app/src/index.ts:63:1) {
  code: 'FST_ERR_SCH_ALREADY_PRESENT',
  statusCode: 500

From Fastify docs:

FST_ERR_SCH_ALREADY_PRESENT
A schema with the same $id already exists.

I am not sure where eaxctly is this $id being set

When I just had one schema and was adding it to fastify in index.ts it was working:

for (const schema of userSchemas) {
   server.addSchema(schema);
}

But adding another schema, throws the above error:

for (const schema of [...userSchemas, ...teamSchemas]) {
   server.addSchema(schema);
}

package.json versions

 "fastify": "^4.6.0",
 "fastify-cors": "^6.1.0",
 "fastify-zod": "^1.2.0",
 "zod": "3.19.1",
 "zod-to-json-schema": "^3.18.1"

user schema:

import { z } from 'zod';
import { buildJsonSchemas } from 'fastify-zod';
import {
  CreateUserRequest,
  UpdateUserRequest,
  UserResponse
} from '../../services/user/interface';

const createUserSchema: z.ZodSchema<CreateUserRequest> = CreateUserRequest;

const updateUserSchema: z.ZodSchema<UpdateUserRequest> = UpdateUserRequest;

const responseUserSchema: z.ZodSchema<UserResponse> = UserResponse;

export const { schemas: userSchemas, $ref } = buildJsonSchemas({
  createUserSchema,
  updateUserSchema,
  responseUserSchema
});

team schema

import { buildJsonSchemas } from 'fastify-zod';
import { z } from 'zod';
import {
  CreateTeamRequest,
  TeamResponse,
  UpdateTeamRequest
} from '../../services/team/interface';

const createTeamSchema: z.ZodSchema<CreateTeamRequest> = CreateTeamRequest;

const updateTeamSchema: z.ZodSchema<UpdateTeamRequest> = UpdateTeamRequest;

const responseTeamSchema: z.ZodSchema<TeamResponse> = TeamResponse;

export const { schemas: teamSchemas, $ref } = buildJsonSchemas({
  createTeamSchema,
  updateTeamSchema,
  responseTeamSchema
});

Looks like it was an issue last year, but has been resolved since: https://github.com/fastify/fastify/issues/2914

Any idea what I maybe be missing here.

EDIT: console.log(schema) before addSchema gives:

{
  '$id': 'Schema',
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    createUserSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    },
    updateUserSchema: {
      type: 'object',
      properties: [Object],
      additionalProperties: false
    },
    responseUserSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    }
  },
  required: [ 'createUserSchema', 'updateUserSchema', 'responseUserSchema' ],
  additionalProperties: false
}
{
  '$id': 'Schema',
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    createTeamSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    },
    updateTeamSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    },
    deleteTeamSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    },
    responseTeamSchema: {
      type: 'object',
      properties: [Object],
      required: [Array],
      additionalProperties: false
    }
  },
  required: [
    'createTeamSchema',
    'updateTeamSchema',
    'deleteTeamSchema',
    'responseTeamSchema'
  ],
  additionalProperties: false
}

Solution

  • I was able to narrow down the issue to fastify-zod By default zod schemas are given the same id i.e Schema. So explicitly adding the ids resolved the issue.

    For example:

    import { buildJsonSchemas } from 'fastify-zod';
    
    const { schemas, $ref } = buildJsonSchemas(models, { $id: "MySchema" });