Step 6: Update UserDao

Let's update UserDao.js to account for the newly added (password) attribute to User schema.

If we want some requirements on password, here is the place to enforce it!

const validPassword = z
  .string()
  .min(6, "Password should be at least 6 characters.");

The create method must take the password, validate and hash it!

async create({ name, email, password }) {
  // ...
  debug("Validating the password..");
  result = validPassword.safeParse(password);
  if (!result.success) {
    throw new ApiError(400, "Password should be at least 6 characters.");
  }

  password = hashPassword(password);
  // ...
}

The update method must take the password, validate it and hash it!

async update({ id, name, email, password }) {
  // ...
  if (password !== undefined) {
    debug("Validating the password..");
    result = validPassword.safeParse(password);
    if (!result.success) {
      throw new ApiError(400, "Invalid Password!");
    }

    password = hashPassword(password);
  }
  // ...
}

Refer to the commit history to see the changes made at this step.