Home > Enterprise >  Error when trying to build Fastify-Zod validation schema
Error when trying to build Fastify-Zod validation schema

Time:09-26

I am new to the typescript ecosystem. I am trying to use zod to validate my fastify request/response, but I am not able to build the schemas using fastify-zod plugin as it throws a compile error. This is my user.schema.ts

import { z } from 'zod'
import { buildJsonSchemas } from 'fastify-zod';

export enum Role {
    Agent = 'Agent',
    Admin = 'Admin',
}

const createUserSchema = z.object({
    email: z.string({
        required_error: "Email is required",
        invalid_type_error: "Email must be a string"
    }).email(),
    role: z.nativeEnum(Role, {
        required_error: "Role is required"
    }),
    teamId: z.number({
        required_error: "Team id is required"
    })
});

export type CreateUserInput = z.infer<typeof createUserSchema>;

export const { schemas: userSchemas, $ref } = buildJsonSchemas({
    createUserSchema     //error here (compile)
});

I am getting an error at createUserSchema and this is the error:

(property) createUserSchema: z.ZodObject<{
    email: z.ZodString;
    role: z.ZodNativeEnum<typeof Role>;
    teamId: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
    email: string;
    role: Role;
    teamId: number;
}, {
    email: string;
    role: Role;
    teamId: number;
}>
Property 'brand' is missing in type 'ZodObject<{ email: ZodString; role: ZodNativeEnum<typeof Role>; teamId: ZodNumber; }, "strip", ZodTypeAny, { email: string; role: Role; teamId: number; }, { ...; }>' but required in type 'ZodType<unknown, ZodTypeDef, unknown>'.ts(2741)
    types.d.ts(78, 5): 'brand' is declared here.
    Models.d.ts(2, 59): The expected type comes from property 'createUserSchema' which is declared here on type 'Models<string>'

No idea where I am getting this from:

Property 'brand' is missing in type

Any idea what I maybe missing here?

Edit:

This is my package.json dependency

  "dependencies": {
    "@firebase/app-types": "^0.7.0",
    "@prisma/client": "^4.3.1",
    "dotenv": "^16.0.2",
    "fastify": "^4.6.0",
    "fastify-cors": "^6.1.0",
    "fastify-zod": "^1.2.0",
    "firebase": "^9.9.4",
    "firebase-admin": "^11.0.1",
    "zod": "3.18.0",
    "typescript": "4.8.2"
  },

I updated the zod version to 3.18.0 from 3.17.2 and I am getting:

Type instantiation is excessively deep and possibly infinite.ts(2589)

CodePudding user response:

When using zod together with fastify-zod, outdated package versions can cause a few obscure errors.

Combining [email protected] and [email protected] may yield errors mentioning missing 'brand' properties:

Property 'brand' is missing in type 'ZodObject; teamId: ZodNumber; }, "strip", ZodTypeAny, { email: string; role: Role; teamId: number; }, { ...; }>' but required in type 'ZodType'.ts(2741)
    types.d.ts(78, 5): 'brand' is declared here.
    Models.d.ts(2, 59): The expected type comes from property 'createUserSchema' which is declared here on type 'Models'

Using [email protected] and [email protected], or having a TypeScript version lower than v4.1, may yield excessively-deep instantiation errors:

Type instantiation is excessively deep and possibly infinite.ts(2589)

With the latest versions of both packages (currently [email protected] and [email protected]), everything works as expected.

TypeScript playground

  • Related