Handle schema validation properly and return error
parent
fd0bbb83c9
commit
12c2a4d4e2
|
@ -25,8 +25,8 @@ router.post('/customer/create', async (req, res) => {
|
||||||
created: new Date()
|
created: new Date()
|
||||||
};
|
};
|
||||||
|
|
||||||
const schemaResult = validateJson('newCustomer', customerObj);
|
const schemaResult = validateJson('customer', customerObj);
|
||||||
if(!schemaResult){
|
if(!schemaResult.result){
|
||||||
res.status(400).json(schemaResult.errors);
|
res.status(400).json(schemaResult.errors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,15 +102,15 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate the body again schema
|
// Validate the body again schema
|
||||||
const schemaResult = validateJson('newProduct', doc);
|
const schemaValidate = validateJson('newProduct', doc);
|
||||||
if(!schemaResult){
|
if(!schemaValidate.result){
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
res.status(400).json(schemaResult.errors);
|
res.status(400).json(schemaValidate.errors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('schemaResult errors', schemaResult.errors);
|
console.log('schemaValidate errors', schemaValidate.errors);
|
||||||
req.session.message = 'Form invalid. Please check values and try again.';
|
req.session.message = 'Form invalid. Please check values and try again.';
|
||||||
req.session.messageType = 'danger';
|
req.session.messageType = 'danger';
|
||||||
|
|
||||||
|
@ -341,11 +341,11 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate the body again schema
|
// Validate the body again schema
|
||||||
const schemaResult = validateJson('editProduct', productDoc);
|
const schemaValidate = validateJson('editProduct', productDoc);
|
||||||
if(!schemaResult){
|
if(!schemaValidate.result){
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
res.status(400).json(schemaResult.errors);
|
res.status(400).json(schemaValidate.errors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue