gpt4 book ai didi

javascript - Joi.object() 和 Joi.object().keys() 有什么区别?

转载 作者:行者123 更新时间:2023-11-30 06:11:17 24 4
gpt4 key购买 nike

根据 Joi 文档,您可以像这样使用 Joi.object():

const object = Joi.object({
a: Joi.number().min(1).max(10).integer(),
b: Joi.any()
});

但您也可以使用 Joi.object().keys() 编写等效代码,如下所示:

const object = Joi.object().keys({
a: Joi.number().min(1).max(10).integer(),
b: Joi.any()
});

两者有什么区别?

最佳答案

如果您只编写一次架构,则无需使用 .keys()。正如他们的文档所说,在向对象添加更多行(键)时使用 .keys() 是“有用的”。

Joi.object().keys([schema]) notation

This is basically the same as Joi.object([schema]), but using Joi.object().keys([schema]) is more useful when you want to add more keys (e.g. call keys() multiple times). If you are only adding one set of keys, you can skip the keys() method and just use object() directly.

Some people like to use keys() to make the code more explicit (this is style only).

取自:https://github.com/hapijs/joi/blob/v8.0.3/API.md#joiobjectkeysschema-notation


我还发现了这个:

There are many ways to use joi. The hapi docs can't show everything. Calling keys() is only recommended when adding keys to an object as it creates another schema

取自:https://github.com/hapijs/hapi/issues/2222

关于javascript - Joi.object() 和 Joi.object().keys() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58811509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com