Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Redirect back with errors (#133)
Browse files Browse the repository at this point in the history
* redirect back with errors instead of rendering in place

* remove unused import

* Bump version and update changelog

* write some more

* redo

* add the Updated header back
  • Loading branch information
dsamojlenko committed Nov 25, 2019
1 parent a94f17c commit 54e6823
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 38 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [6.1.0] - 2019-11-25

### Updated
- Redirect back with errors. This *may* be a breaking change if you have written any customizations to deal with issues surrounding `renderPageWithErrors` in `validate.helpers.js`. That method has been removed, and now on validation errors, we flash the errors to the session and redirect back to the `get` route.

### Removed
- `renderPageWithErrors` has been removed from `validate.helpers.js`

## [6.0.3] - 2019-11-22

- Nodemon [2.0.1](https://github.com/remy/nodemon/releases/tag/v2.0.1)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-starter-app",
"version": "6.0.3",
"version": "6.1.0",
"description": "",
"author": "CDS",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion routes/personal/personal.controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('Can send post request personal route ', async () => {
const csrfToken = extractCsrfToken(getresp);

const postresp = await testSession.post(route.path.en).send({ _csrf: csrfToken });
expect(postresp.statusCode).toBe(200);
expect(postresp.statusCode).toBe(302); // should redirect back with errors on an incomplete form
})

jest.mock('../../utils/flash.message.helpers', () => ({
Expand Down
42 changes: 6 additions & 36 deletions utils/validate.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,16 @@ const checkErrors = template => {

saveSessionData(req)

// flash error messages and redirect back on error
if (!errors.isEmpty()) {
return renderPageWithErrors(req, res, {
template,
errors: errorArray2ErrorObject(errors),
})
req.session.flashmessage = errorArray2ErrorObject(errors)
return res.redirect('back')
}

return next()
}
}

/**
* @param options template
{
template: "personal",
errors: {
fullname: {
value: "",
msg: "errors.fullname.length",
param: "fullname",
location: "body"
}
}
};
*/

const renderPageWithErrors = (
req,
res,
options = { template: '', errors: [] },
) => {
return res.render(options.template, {
data: getSessionData(req),
name: options.template,
body: req.body,
errors: options.errors,
})
}

/**
* @param {Object} req express request obj
*/
Expand All @@ -127,12 +98,12 @@ const validateRouteData = async (req, schema) => {
}

// run checkSchema()
await middleWare[0][0](validateReq, res, () => {})
await middleWare[0][0](validateReq, res, () => { })

// run checkErrors()
middleWare[1](validateReq, res, () => {})
middleWare[1](validateReq, res, () => { })

const errors = checkErrorsJSON(validateReq, res, () => {})
const errors = checkErrorsJSON(validateReq, res, () => { })

if (!isEmptyObject(errors)) {
return { status: false, errors: errors }
Expand Down Expand Up @@ -187,5 +158,4 @@ module.exports = {
checkErrorsJSON,
hasData,
isEmptyObject,
renderPageWithErrors,
}

0 comments on commit 54e6823

Please sign in to comment.