gpt4 book ai didi

javascript - Hapi/Joi 验证

转载 作者:行者123 更新时间:2023-11-30 14:39:59 26 4
gpt4 key购买 nike

这些 with() 和 without() 函数在 Joi 验证中做了什么?

const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
access_token: [Joi.string(), Joi.number()],
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');

最佳答案

取自hapijs API Reference :

object.with(key, peers)

Requires the presence of other keys whenever the specified key is present where:

key - the reference key.

peers - the required peer key names that must appear together with key. peers can be a single string value or an array of string values.

翻译成您的示例,意思是“当键 username 存在时,键 birthyear 也必须存在”。

object.without(key, peers)

Forbids the presence of other keys whenever the specified is present where:

key - the reference key.

peers - the forbidden peer key names that must not appear together with key. peers can be a single string value or an array of string values.

翻译成您的示例,意思是“当 key password 存在时, key access_token 也不允许存在”。

关于javascript - Hapi/Joi 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825500/

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