diff --git a/server/src/api/controllers/Contacts/Contacts.ts b/server/src/api/controllers/Contacts/Contacts.ts index 0ac82f44a..91a0c5f38 100644 --- a/server/src/api/controllers/Contacts/Contacts.ts +++ b/server/src/api/controllers/Contacts/Contacts.ts @@ -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(), diff --git a/server/src/api/controllers/Contacts/Customers.ts b/server/src/api/controllers/Contacts/Customers.ts index e2acdd6f3..80481a57a 100644 --- a/server/src/api/controllers/Contacts/Customers.ts +++ b/server/src/api/controllers/Contacts/Customers.ts @@ -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(), ]; diff --git a/server/src/api/middleware/ConvertEmptyStringsToNull.ts b/server/src/api/middleware/ConvertEmptyStringsToNull.ts new file mode 100644 index 000000000..5099f28ef --- /dev/null +++ b/server/src/api/middleware/ConvertEmptyStringsToNull.ts @@ -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(); +}; \ No newline at end of file diff --git a/server/src/loaders/express.ts b/server/src/loaders/express.ts index 435d6b0e6..242239de5 100644 --- a/server/src/loaders/express.ts +++ b/server/src/loaders/express.ts @@ -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()); diff --git a/server/src/utils/index.js b/server/src/utils/index.js index d2e08dfca..8c068a17a 100644 --- a/server/src/utils/index.js +++ b/server/src/utils/index.js @@ -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, };