Errors come up all the time. It is our duty as programmers to prevent errors, both known and unknown, from breaking our app. The very first step is adding validation to user inputs. This is achieved using adonis validator. It is a wrapper around Indicative, a validation library built by the Adonis team.

Validating inputs in AuthController.register

Our first task is to describe the rules which the inputs will be checked against.

const rules = {
  username: "required|unique:users,username",
  password: "required",
};

Afterwards, we validate the inputs against these rules

const validation = await validateAll(request.all(), rules);
if (validation.fails()) {
  session.withErrors(validation.messages()).flashAll();
  return response.redirect("back");
}

Get hands-on with 1200+ tech skills courses.