Refactored global api checks for params

This commit is contained in:
Alexis
2020-12-28 12:25:29 +01:00
parent f7a09c02ae
commit 23ab785163

View File

@@ -1,6 +1,3 @@
// Error handling
const { HttpError } = require('./validations/Errors')
const regexInt = RegExp(/^[1-9]\d*$/) const regexInt = RegExp(/^[1-9]\d*$/)
const regexXSS = RegExp(/<[^>]*script/) const regexXSS = RegExp(/<[^>]*script/)
@@ -13,32 +10,22 @@ const paramIntCheck = (req, res, next, input) => {
throw new Error throw new Error
} }
} catch (err) { } catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero') res.status(err.code).send(JSON.stringify({
res.status(err.code).send(JSON.stringify( "message": "Le paramètre doit être un entier non-nul.",
{ "code": 403,
"error": err.message, })
"code": err.code
})
) )
} }
} }
// Check if script injection attempt // Check if script injection attempt
const isXSSAttempt = (string) => { const isXSSAttempt = (string) => {
if (regexXSS.test(string)) { return regexXSS.test(string);
return true
} else {
return false
}
} }
// Check if object is null // Check if object is null
const isEmptyObject = (obj) => { const isEmptyObject = (obj) => {
if (Object.keys(obj).length === 0 && obj.constructor === Object) { return (Object.keys(obj).length === 0 && obj.constructor === Object);
return true
} else {
return false
}
} }
module.exports = { module.exports = {