From bd6e5ae02a006693b75b727cc1dc0790c9941e83 Mon Sep 17 00:00:00 2001 From: ryanwong Date: Sun, 6 Feb 2022 21:41:19 -0500 Subject: [PATCH] add validation service --- day1/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day2/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day3/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day4/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day5/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day6/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day7/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day8/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ day9/services/ValidationService.js | 85 ++++++++++++++++++++++++++++++ 9 files changed, 765 insertions(+) create mode 100644 day1/services/ValidationService.js create mode 100644 day2/services/ValidationService.js create mode 100644 day3/services/ValidationService.js create mode 100644 day4/services/ValidationService.js create mode 100644 day5/services/ValidationService.js create mode 100644 day6/services/ValidationService.js create mode 100644 day7/services/ValidationService.js create mode 100644 day8/services/ValidationService.js create mode 100644 day9/services/ValidationService.js diff --git a/day1/services/ValidationService.js b/day1/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day1/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day2/services/ValidationService.js b/day2/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day2/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day3/services/ValidationService.js b/day3/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day3/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day4/services/ValidationService.js b/day4/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day4/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day5/services/ValidationService.js b/day5/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day5/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day6/services/ValidationService.js b/day6/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day6/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day7/services/ValidationService.js b/day7/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day7/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day8/services/ValidationService.js b/day8/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day8/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +}; diff --git a/day9/services/ValidationService.js b/day9/services/ValidationService.js new file mode 100644 index 0000000..bf60bc8 --- /dev/null +++ b/day9/services/ValidationService.js @@ -0,0 +1,85 @@ +const { Validator, addCustomMessages } = require('node-input-validator'); + +// {fieldName:message} eg:{email:"Invalid Email", password:"Password too short"} +const formatValidationError = (error) => { + const formatted = Object.entries(error) + .map(([key, value]) => ({ + field: key, + message: value.message, + })) + .reduce((accumulator, currentValue) => { + if (!accumulator[currentValue]) { + accumulator[currentValue.field] = currentValue.message; + } + return accumulator; + }, {}); + return formatted; +}; + +module.exports = { + /** + * Input Validator middleware for controller + * @param {object} validationObject object defining body fields and its validation types eg:{email:required|email} + * @param {object} _extendMessages object defining message to throw on validation error eg: {"email.required":"Email is required","email.email":"Invalid email"} + * + */ + validateInput: (validationObject = {}, _extendMessages = {}) => async ( + req, + res, + next, + ) => { + const validation = new Validator(req.body, validationObject); + addCustomMessages(_extendMessages); + + try { + const isValid = await validation.check(); + if (!isValid) { + req.validationError = formatValidationError(validation.errors); + } + return next(); + } catch (error) { + req.validationError = error.message; + return next(); + } + }, + + handleValidationErrorForViews: ( + req, + res, + viewModel, + viewPath = '/', + fieldsStoreKey, + defaultValue = {}, + ) => { + const validationError = req.validationError; + + if (validationError) { + // Remembers fields if validation error occurs + Object.entries(defaultValue).forEach(([key, value]) => { + viewModel[fieldsStoreKey][key] = value; + }); + + if (typeof validationError === 'string') { + viewModel.error = validationError; + } else { + viewModel.validationError = req.validationError; + } + return res.render(viewPath, viewModel); + } + }, + + handleValidationErrorForAPI: (req, res, next) => { + const validationError = req.validationError; + + if (validationError) { + let error; + if (typeof validationError === 'string') { + error = validationError; + } else { + error = req.validationError; + } + return res.json({ success: false, error }); + } + next(); + }, +};