Skip to content

Commit

Permalink
add ValidationUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaaron committed Jun 21, 2021
1 parent d72e8f1 commit 55fb9bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
40 changes: 40 additions & 0 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
@@ -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,
};
39 changes: 4 additions & 35 deletions src/pages/ReimbursementAccount/CompanyStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -44,46 +45,14 @@ 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()) {
Growl.error(this.props.translate('common.passwordCannotBeBlank'));
return false;
}

if (!this.isValidAddress(this.state.addressStreet)) {
if (!isValidAddress(this.state.addressStreet)) {
Growl.error(this.props.translate('bankAccount.error.addressStreet'));
return false;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 55fb9bf

Please sign in to comment.