mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: request middleware to convert empty strings to null.
This commit is contained in:
@@ -14,7 +14,7 @@ export default class ContactsController extends BaseController {
|
||||
check('company_name').optional().trim().escape(),
|
||||
check('display_name').exists().trim().escape(),
|
||||
|
||||
check('email').optional().isEmail().trim().escape(),
|
||||
check('email').optional({ nullable: true }).normalizeEmail().isEmail(),
|
||||
check('website').optional().trim().escape(),
|
||||
check('work_phone').optional().trim().escape(),
|
||||
check('personal_phone').optional().trim().escape(),
|
||||
|
||||
@@ -86,8 +86,8 @@ export default class CustomersController extends ContactsController {
|
||||
*/
|
||||
get createCustomerDTOSchema() {
|
||||
return [
|
||||
check('opening_balance').optional().isNumeric().toInt(),
|
||||
check('opening_balance_at').optional().isISO8601(),
|
||||
check('opening_balance').optional({ nullable: true }).isNumeric().toInt(),
|
||||
check('opening_balance_at').optional({ nullable: true }).isISO8601(),
|
||||
|
||||
check('currency_code').optional().trim().escape(),
|
||||
];
|
||||
|
||||
8
server/src/api/middleware/ConvertEmptyStringsToNull.ts
Normal file
8
server/src/api/middleware/ConvertEmptyStringsToNull.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { convertEmptyStringsToNull } from 'utils';
|
||||
|
||||
export default (req: Request, res: Response, next: NextFunction) => {
|
||||
const transfomedBody = convertEmptyStringsToNull(req.body);
|
||||
req.body = transfomedBody;
|
||||
next();
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import i18n from 'i18n';
|
||||
import routes from 'api';
|
||||
import LoggerMiddleware from 'api/middleware/LoggerMiddleware';
|
||||
import AgendashController from 'api/controllers/Agendash';
|
||||
import ConvertEmptyStringsToNull from 'api/middleware/ConvertEmptyStringsToNull';
|
||||
import config from 'config';
|
||||
|
||||
export default ({ app }) => {
|
||||
@@ -36,6 +37,9 @@ export default ({ app }) => {
|
||||
// Logger middleware.
|
||||
app.use(LoggerMiddleware);
|
||||
|
||||
// Converts empty strings to null of request body.
|
||||
app.use(ConvertEmptyStringsToNull);
|
||||
|
||||
// Prefix all application routes.
|
||||
app.use(config.api.prefix, routes());
|
||||
|
||||
|
||||
@@ -195,6 +195,16 @@ const entriesAmountDiff = (newEntries, oldEntries, amountAttribute, idAttribute)
|
||||
.value();
|
||||
};
|
||||
|
||||
const convertEmptyStringsToNull = (obj) => {
|
||||
return _.mapValues(obj, (value, key) => {
|
||||
return typeof value === 'string' ?
|
||||
value.trim() === '' ?
|
||||
null :
|
||||
value :
|
||||
value;
|
||||
});
|
||||
};
|
||||
|
||||
export {
|
||||
hashPassword,
|
||||
origin,
|
||||
@@ -214,4 +224,5 @@ export {
|
||||
getDefinedOptions,
|
||||
|
||||
entriesAmountDiff,
|
||||
convertEmptyStringsToNull,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user