0

我正在使用环回 4 身份验证,并且我有这个凭证架构。在我的注册方法中,当我输入的密码少于 8 个字符时,我收到了这个 http 错误响应。我试图避免在响应中显示密码应短于 8 个字符的错误消息,而是使用没有该详细信息的 http 错误响应进行响应。如何用自定义错误替换此默认错误?

我尝试过这样的事情

if (credential.password.length < 8) {
      throw new HttpErrors.NotFound;
} 

在注册方法中,但它不起作用。我可以删除架构中的 minLength 限制 8 但在这种情况下我想要那个错误响应。

export const CredentialsSchema = {
  type: 'object',
  required: ['email', 'password'],
  properties: {
    email: {
      type: 'string',
      format: 'email',
   },
   password: {
     type: 'string',
     minLength: 8,
   },
  },
};


async register(
@requestBody() credential: CredentialPassword,
): Promise<AccessToken> {
return this.userRegistration(credential);
}


{
"error": {
"statusCode": 422,
"name": "UnprocessableEntityError",
"message": "The request body is invalid. See error object `details` property for more info.",
"code": "VALIDATION_FAILED",
"details": [
  {
    "path": ".password",
    "code": "minLength",
    "message": "should NOT be shorter than 8 characters",
    "info": {
      "limit": 8
    }
  }]
 }
}

谢谢你的帮助 !

4

1 回答 1

0

您可以通过以下方式执行此操作:

export const CredentialsSchema = {
  type: 'object',
  required: ['email', 'password'],
  properties: {
    email: {
      type: 'string',
      format: 'email',
   },
   password: {
     type: 'string',
     minLength: 8,
     errors: {
        minLength: "Should be at least 8 characters long."
      }
   },
  },
};
于 2019-10-31T08:54:40.330 回答