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;
}
};
// 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()
}
}
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);
}
Object.keys(config).forEach((key) => {
let result = inspector.validate(schema, config[key]);
if (!result.valid) invalidConfigs[key] = result;
});
function validate(schema, parameters, onValidationErrors, onValidationPass) {
var schemaObject = wrap(schema);
inspector.sanitize(schemaObject, parameters);
var result = inspector.validate(schemaObject, parameters);
if (result.valid) {
onValidationPass(parameters);
} else {
var errors = result.error.map(function (fieldError) {
var parameterName = getParameterName(fieldError);
var errorMessage = schema[parameterName].message;
return {
parameterName: parameterName,
message: errorMessage
};
});
onValidationErrors(parameters, errors);
}
}