简体   繁体   中英

How to change the HTTP error about the password length in loopback 4?

I am using loopback 4 authentication and I have this credential schema. In my register method I am getting this http error response when giving a password shorter than 8 characters. I'm trying to avoid error message in the response that the password should be shorter than 8 characters, instead respond with an http error response without that details. How can I replace this default error with a custom one?

I tried something like this

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

in the register method but it did not work. I could delete the minLength limit 8 in the schema but I want that error response in that case.

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
    }
  }]
 }
}

Thank's for your help !

You can do this like following way:

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."
      }
   },
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM