Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
entities = entities.concat([entity]);
}
// resolve complete parameters from entities
entities = crossMatchEntities(entities);
// merge entities into parameters obj
var parameters = _.reduce(entities, function (merged, entity) {
merged[entity.type] = entity.entity;
return merged;
}, {});
// validate and remove those parameters marked as invalid
var schemaObject = wrap(schema);
inspector.sanitize(schemaObject, parameters);
var result = inspector.validate(schemaObject, parameters);
if (!result.valid) {
var invalidParameterNames = result.error.map(getParameterName);
parameters = _.omit(parameters, invalidParameterNames);
}
return parameters;
}
entity = _.merge({}, entity, { type: actionModel.currentParameter });
entities = entities.concat([entity]);
}
// resolve complete parameters from entities
entities = crossMatchEntities(entities);
// merge entities into parameters obj
var parameters = _.reduce(entities, function (merged, entity) {
merged[entity.type] = entity.entity;
return merged;
}, {});
// validate and remove those parameters marked as invalid
var schemaObject = wrap(schema);
inspector.sanitize(schemaObject, parameters);
var result = inspector.validate(schemaObject, parameters);
if (!result.valid) {
var invalidParameterNames = result.error.map(getParameterName);
parameters = _.omit(parameters, invalidParameterNames);
}
return parameters;
}
};
// do data validation
const validation = {
username: {
type: 'string',
pattern: /^[0-9a-z_]{4,20}$/
},
email: {
type: 'string',
pattern: 'email',
optional: true
}
};
if (inspector.validate(validation, info).length > 0) {
return res
.status(400)
.json({code: 2, message: 'VALIDATION FAILED'});
}
// check username / email duplication
const p1 = Account.findUser(info.username);
const p2 = Account.findUserByEmail(info.email);
// wait for all fulfillments
Promise
.all([p1/*, p2*/
])
.then(values => {
if (values[0]) {
pagesdb.upsertRow = (rowDataObj) => {
inspector.sanitize(schemas.upsertRowSanitization, rowDataObj)
/*****
* Check requestDataValidation.js for why we're making a copy (_.assign) of rowDataObj/req.body data
* for inspector.validate().
*/
var validatedPageDataObj = inspector.validate(schemas.upsertRowValidation, _.assign({}, rowDataObj))
if(!validatedPageDataObj.valid){
var errMessage = `Error, passed in row data did not pass validation.
Error(s): ${ validatedPageDataObj.format() }`
global.devMode && console.error(errMessage)
appLogger.log.error({err: errMessage})
let errorToReturn = new Error(errMessage)
/****
* Note: we need to return a blubird promise here, because we use bluebird's
* bind method in addPage.js and returning a native promise would cause an
* uncaughtException error as native promise bind is a bit different.
* Also, throwing an error here would also cause an uncaughtException error
* because we wouldn't be returning a bluebird promise.
*/
return Promise.reject(errorToReturn)
}
return pagesdb
inspector.sanitize(schemas.reqParamsSanitization, reqParams)
inspector.sanitize(schemas.reqBodySanitization, reqBody)
req.body = reqBody
req.params = reqParams
var validReqParams = inspector.validate(schemas.reqParamsValidation, reqParams)
if(!validReqParams.valid){
let errMessage = `Error(s) with the reqParams data in requestDataValidation : ${ validReqParams.format() }`
let err = new Error(errMessage)
global.devMode && console.error(errMessage)
appLogger.log.error({err, req, res})
return res.status(500).json({errMessage})
}
var validReqBody = inspector.validate(schemas.reqBodyValidation, reqBody)
if(!validReqBody.valid){
let errMessage = `Error(s) with the reqBody data in requestDataValidation : ${ validReqBody.format() }`
let err = new Error(errMessage)
global.devMode && console.error(errMessage)
appLogger.log.error({err, req, res})
return res.status(500).json({errMessage})
}
if(validReqParams.valid && validReqBody.valid){
return next()
}
}
*
* I'm not sure why it is happening, so for now, I'm just going to use lodash to copy the key/values to a new
* object with a proper prototype and then assign the sanitized object back to the req.body & req.params.
*
* note: {} is equivalent to: Object.create(Object.prototype)
*
* I guess do it for req.params as well.
*
* Maybe related: https://github.com/expressjs/express/issues/2613
*/
var reqBody = _.assign({}, req.body)
var reqParams = _.assign({}, req.params)
inspector.sanitize(schemas.reqParamsSanitization, reqParams)
inspector.sanitize(schemas.reqBodySanitization, reqBody)
req.body = reqBody
req.params = reqParams
var validReqParams = inspector.validate(schemas.reqParamsValidation, reqParams)
if(!validReqParams.valid){
let errMessage = `Error(s) with the reqParams data in requestDataValidation : ${ validReqParams.format() }`
let err = new Error(errMessage)
global.devMode && console.error(errMessage)
appLogger.log.error({err, req, res})
return res.status(500).json({errMessage})
}
var validReqBody = inspector.validate(schemas.reqBodyValidation, reqBody)
if(!validReqBody.valid){
let errMessage = `Error(s) with the reqBody data in requestDataValidation : ${ validReqBody.format() }`
pagesdb.updateColumns = (columnsDataObj) => {
inspector.sanitize(schemas.updateColumnSanitization, columnsDataObj)
/*****
* Check requestDataValidation.js for why we're making a copy (_.assign) of columnsDataObj/req.body data
* for inspector.validate().
*/
var validatedColumnsDataObj = inspector.validate(schemas.updateColumnValidation, _.assign({}, columnsDataObj))
if(!validatedColumnsDataObj.valid){
var errMessage = `Error, passed in column data did not pass validation.
Error(s): ${ validatedColumnsDataObj.format() }`
global.devMode && console.error(errMessage)
appLogger.log.error({err: errMessage})
let errorToReturn = new Error(errMessage)
/****
* Note: we need to return a blubird promise here, because we use bluebird's
* bind method in addPage.js and returning a native promise would cause an
* uncaughtException error as native promise bind is a bit different.
* Also, throwing an error here would also cause an uncaughtException error
static validateSchema(profile: any, strict = false) {
schemaDefinition.strict = strict
return inspector.validate(schemaDefinition, profile)
}
function validateLoginBody(body) {
const validation = {
type: 'object',
properties: {
username: {
type: 'string',
minLength: 1
},
password: {
type: 'string',
minLength: 1
}
}
};
return inspector.validate(validation, body);
}