简体   繁体   中英

Joi validation - not allowed empty object

Through an HTTP request, I receive from client side the following body:

{
    a: string,
    b: string,
    c: string
}

I want to validate them with joi, so I do:

const MySchema = Joi.Object<MyModel>().keys({
    a: Joi.string().alfanum().min(1).max(150).optional(),
    b: Joi.string().alfanum().min(1).max(150).optional(),
    c: Joi.string().alfanum().min(1).max(150).optional(),
}).required()

This allows empy objects.

How can I say to joi to not allow empty object? I want the body request to have at least one of those keys. For now I haven't found the solution.

I know that I can handle it in the API but I don't want to write useless code.

Thank you!

I solved it with

Joi.object<MyModel>.keys({/*My keys*/}).required().min(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM