简体   繁体   English

Joi 验证 - 不允许为空 object

[英]Joi validation - not allowed empty object

Through an HTTP request, I receive from client side the following body:通过 HTTP 请求,我从客户端收到以下正文:

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

I want to validate them with joi, so I do:我想用 joi 验证它们,所以我这样做:

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?我怎么能说 joi 不允许空的 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.我知道我可以在 API 中处理它,但我不想编写无用的代码。

Thank you!谢谢!

I solved it with我解决了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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