Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!


1 Answers

I solved it with

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!