Telescope: adding and testing validation middle-ware
Tags: opensource
Published: 2020-11-19 04:52:07

## Intro
Last week, I worked on an [issue](https://github.com/Seneca-CDOT/telescope/issues/1313) to add a validation middle-ware to query param in the posts route using express-validator. When, I saw the issue I thought that I can definitely do the issue  and I did complete the issue fairly quick. This blog, Is not about implementation a solution to the issue but the many things that go on after submitting a pr and getting it accepted. 

## Refactoring
After I submitted a pr with a excellent solution encompassing both the rules the Middleware checks for in the route and an appropriate Middleware that reports the error into one function Middleware. Although, the solution was sufficient there was a lack of evidence such as tests to test the middleware were not implemented. So, I had to implement more tests that would test the /posts middleware I had implemented. while, this was ongoing there were sevral issues on the project that dealt with adding validation middle ware for different routes and all of them lived in the validation.js file. After, I implemented my tests and I commited my changes. The project had already merged one of the other middlewares and the concept I used to implement my middleware was also used on the other merge. The problem now was I got a request to refactor the code to create a common validation.js function for all of the validation middleware

```
const validate = (rules) => {
  return async (req, res, next) => {
    await Promise.all(rules.map((rule) => rule.run(req)));

    const result = validationResult(req);
    if (result.isEmpty()) {
      return next();
    }

    const errors = result.array();
    return res.status(400).send(errors);
  };
};

```
## Git Issues
After the refraction my problems began and the problems were not with the implementation itself but with the version-control system known as git. The problem that occurred was each time I rebased my commits into one commit and try to push the changes I would have to merge and there would be conflicts. This created a loop of me fixing the conflicts and the rebasing to one commit and then another iteration of the loop would begin. So, the problem that was happening here is that my pr was on the master branch of my fork and the way this works is that it will always want to merge before rebasing and with some help. I got the solution here are the steps if you are in a rebase loop I was in.

```
1. Create a branch for the issue and then use the cherrypick command in git to pick the commit that you want to push.

git checkout -b issue-name origin/master && git cherry-pick <commit-id>

2.Then we reset the master branch and update it with upstream

git checkout -B master upstream/master
git pull upstream master

3. rebase
git checkout issue-1313 && git rebase master

4. Also push by force if need be
git push --force

5. Create a new pr with the new branch 
```
Following these simple steps will solve all problems/s

## Lessons Learned
This issue taught me many things such as always create a branch for the issue to avoid conflicts like above. Even if the code is perfect if the appropriate testing is not done its not perfect and needs evidence. While completing this pr and after I had the opportunity to start reviewing others code and this really gives you new perspective, on what changes to make before committing a pr, such as following proper naming conventions.