使用验证时如何修复错误?

How do I fix the error when using Validation?

我正在尝试为我的 API 添加验证。编译项目时出现以下错误。

(node:13800) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Invalid schema content:

代码如下:

    const Joi = require('joi');

    async function response(request) 
    {
        let email = request.payload.email;
        let userRecord = await User.find();
        userRecord = userRecord.find(x => x.email == email);

        if (!userRecord)
        {
            throw Boom.unauthorized();
        }

        if (!userRecord.checkPassword(request.payload.password))
        {
           throw Boom.unauthorized();
        }
    
        let token = await CreateUpdateToken(userRecord);

        return {
         bearerToken: [token.access_token]
        };
    }
   
    module.exports = {
        method: 'POST',
        path: '/api/v1/auth',
        handler: response,
        options: {
            validate: {
                payload: {
                    password: Joi.string()
                }
            }
        }
    }

注意:如果我删除“密码:Joi.string()”,错误就会消失

您需要一个 object 架构来验证 hapi 中的负载:

validate: {
  payload: Joi.object({
    password: Joi.string().trim().required()
  })
}