From 55fb9bf00e5dbabc4cc7f3837b6f1a17ff43b371 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 21 Jun 2021 07:42:20 -1000 Subject: [PATCH] add ValidationUtils --- src/libs/ValidationUtils.js | 40 +++++++++++++++++++ src/pages/ReimbursementAccount/CompanyStep.js | 39 ++---------------- 2 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 src/libs/ValidationUtils.js diff --git a/src/libs/ValidationUtils.js b/src/libs/ValidationUtils.js new file mode 100644 index 000000000000..2a4f0061da23 --- /dev/null +++ b/src/libs/ValidationUtils.js @@ -0,0 +1,40 @@ +import moment from 'moment'; +import CONST from '../CONST'; + +/** + * Validating that this is a valid address (PO boxes are not allowed) + * + * @param {String} value + * @returns {Boolean} + */ +function isValidAddress(value) { + if (!CONST.REGEX.ANY_VALUE.test(value)) { + return false; + } + + return !CONST.REGEX.PO_BOX.test(value); +} + +/** + * Validate date fields + * + * @param {String} date + * @returns {Boolean} true if valid + */ +function isValidDate(date) { + return moment(date).isValid(); +} + +/** + * @param {String} code + * @returns {Boolean} + */ +function isValidIndustryCode(code) { + return CONST.REGEX.INDUSTRY_CODE.test(code); +} + +export { + isValidAddress, + isValidDate, + isValidIndustryCode, +}; diff --git a/src/pages/ReimbursementAccount/CompanyStep.js b/src/pages/ReimbursementAccount/CompanyStep.js index 5423086d7e69..4158cefc6a2e 100644 --- a/src/pages/ReimbursementAccount/CompanyStep.js +++ b/src/pages/ReimbursementAccount/CompanyStep.js @@ -19,6 +19,7 @@ import Picker from '../../components/Picker'; import StatePicker from '../../components/StatePicker'; import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize'; import Growl from '../../libs/Growl'; +import {isValidAddress, isValidDate, isValidIndustryCode} from '../../libs/ValidationUtils'; class CompanyStep extends React.Component { constructor(props) { @@ -44,38 +45,6 @@ class CompanyStep extends React.Component { }; } - /** - * Validating that this is a valid address (PO boxes are not allowed) - * - * @param {String} value - * @returns {Boolean} - */ - isValidAddress(value) { - if (!CONST.REGEX.ANY_VALUE.test(value)) { - return false; - } - - return !CONST.REGEX.PO_BOX.test(value); - } - - /** - * Validate date fields - * - * @param {String} date - * @returns {Boolean} true if valid - */ - isValidDate(date) { - return moment(date).isValid(); - } - - /** - * @param {String} code - * @returns {Boolean} - */ - validateIndustryCode(code) { - return !CONST.REGEX.INDUSTRY_CODE.test(code); - } - validate() { // @TODO check more than just the password if (!this.state.password.trim()) { @@ -83,7 +52,7 @@ class CompanyStep extends React.Component { return false; } - if (!this.isValidAddress(this.state.addressStreet)) { + if (!isValidAddress(this.state.addressStreet)) { Growl.error(this.props.translate('bankAccount.error.addressStreet')); return false; } @@ -103,12 +72,12 @@ class CompanyStep extends React.Component { return false; } - if (!this.isValidDate(this.state.incorporationDate)) { + if (!isValidDate(this.state.incorporationDate)) { Growl.error(this.props.translate('bankAccount.error.incorporationDate')); return false; } - if (this.validateIndustryCode(this.state.industryCode)) { + if (!isValidIndustryCode(this.state.industryCode)) { Growl.error(this.props.translate('bankAccount.error.industryCode')); return false; }