0

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 !

Divyanshu Rawat
  • 4,421
  • 2
  • 37
  • 53

1 Answers1

0

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."
      }
   },
  },
};
Manish Balodia
  • 1,863
  • 2
  • 23
  • 37