在 Joi 中接受查询字符串中的“#”

Accepting '#' in query string in Joi

我有一个接受用户名作为查询字符串的端点,但这个用户名开头可能有#,所以在 Joi 中我得到了这个

{
    "code": 1021,
    "message": "Request data model validation has failed",
    "id": "276ba99a-2abc-4505-b66a-dad2a8ger5",
    "explanation": [
        "`username` is not allowed to be empty"
    ]
}

如何使用 Joi 让我的查询字符串在开始时接受 #?

您必须先对参数进行编码,然后再将其添加到 URL。

const url = 'https://example.com?username=#allia';
const response = await fetch(encodeURIComponent(url));

或者您可以将 # 替换为 %23