arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

Compatebility with Prisma and prisma-joi-generator #240

Closed NicolaiVdS closed 1 year ago

NicolaiVdS commented 1 year ago

My joi schema looks like this:

Schema is generated with prisma-joi-generator

Example UserCreateSchema: ```javascript import Joi from 'joi'; import { UserCreateInputSchemaObject } from './objects'; export const UserCreateSchema = Joi.object() .keys({ data: Joi.object().keys(UserCreateInputSchemaObject) }) .required(); ``` UserCreateInputSchemaObject: ```javascript // @ts-nocheck import Joi from 'joi'; import { AddressCreateNestedManyWithoutUserInputSchemaObject } from './AddressCreateNestedManyWithoutUserInput.schema'; export const UserCreateInputSchemaObject = { email: Joi.string().required(), password: Joi.string().required(), first_name: Joi.alternatives().try(Joi.string()), last_name: Joi.alternatives().try(Joi.string()), phone: Joi.alternatives().try(Joi.string()), addresses: Joi.object().keys(AddressCreateNestedManyWithoutUserInputSchemaObject), created_at: Joi.date(), }; ```

The issue I am having with celebrate is:

when using a schema generated with prisma-joi-generator as followed router.post('/create', celebrate(UserCreateSchema), controller.createUser); i'm getting following error

src/routes/User.ts:9:34 - error TS2559: Type 'ObjectSchema<any>' has no properties in common with type 'SchemaOptions'.

is there a way i can use the generated schemas or do i need to make my own ?

arb commented 1 year ago

I'm not sure what prisma-joi-generator has to do with this question since I don't see it used in your code and I am unfamiliar with the library 🤔 That being said, I don't think the arguments you are passing into celebrate are correct. celebrate accepts an object, not a Joi schema. What happens if you do:

router.post('/create', celebrate({body: UserCreateSchema}), controller.createUser);
arb commented 1 year ago

@NicolaiVdS are you all set? If so, can you please close this issue?