Skip to content

Commit

Permalink
Function for server side validation of a regex (#1153)
Browse files Browse the repository at this point in the history
* Function for server side validation of a regex

* Refactor validate using regex function

* Add trim leading and trailing spaces config option
  • Loading branch information
alejandro-bulgaris-qcif authored Dec 9, 2022
1 parent 6462141 commit 0cc3ee1
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions typescript/api/services/TriggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import { Observable } from 'rxjs/Rx';
import {Services as services} from '@researchdatabox/redbox-core-types';
import {
Services as services,
RBValidationError
} from '@researchdatabox/redbox-core-types';
import { Sails, Model } from "sails";

declare var sails: Sails;
Expand All @@ -27,6 +30,7 @@ declare var _this;
declare var _;
declare var User;
declare var RecordsService;
declare var TranslationService;

export module Services {
/**
Expand All @@ -39,7 +43,8 @@ export module Services {

protected _exportedMethods: any = [
'transitionWorkflow',
'runHooksSync'
'runHooksSync',
'validateFieldUsingRegex'
];

/**
Expand Down Expand Up @@ -121,7 +126,73 @@ export module Services {
}
}

private validateRegex(data, regexPattern, fieldLanguageCode, errorMessageCode) {
let re = new RegExp(regexPattern,'i');
let reTest = re.test(data.toString());
sails.log.error('validateFieldUsingRegex reTest '+reTest);
if(!reTest) {
let customError: RBValidationError;
if(!_.isUndefined(fieldLanguageCode)) {
let fieldName = TranslationService.t(fieldLanguageCode);
let baseErrorMessage = TranslationService.t(errorMessageCode);
customError = new RBValidationError(fieldName + ' '+ baseErrorMessage);
} else {
let baseErrorMessage = TranslationService.t(errorMessageCode);
customError = new RBValidationError(baseErrorMessage);
}
throw customError;
}
}

public validateFieldUsingRegex(oid, record, options) {
//mandatory
let fieldDBName = _.get(options,'fieldDBName');
let errorMessageCode = _.get(options,'errorLanguageCode');
let regexPattern = _.get(options,'regexPattern');
//optional
let fieldLanguageCode = _.get(options,'fieldLanguageCode');
let arrayObjFieldDBName = _.get(options,'arrayObjFieldDBName');
//Set false by default if not present this option will remove leading and trailing spaces from a none array value
//then it will modify the value in the record if the the regex validation is passed therefore handle with care
let trimLeadingAndTrailingSpacesBeforeValidation = _.get(options,'trimLeadingAndTrailingSpacesBeforeValidation') || false;

let data = _.get(record, fieldDBName);

if(_.isArray(data) && !_.isUndefined(arrayObjFieldDBName)) {

sails.log.verbose(`validateFieldUsingRegex is array ${fieldDBName} `+JSON.stringify(data));
sails.log.verbose('validateFieldUsingRegex is array regexPattern '+regexPattern);
for(let row of data) {

let objField = _.get(row, arrayObjFieldDBName);

if(!_.isUndefined(objField) && objField != null && objField != '' && !_.isUndefined(regexPattern) && !_.isUndefined(errorMessageCode) ) {

this.validateRegex(objField, regexPattern, fieldLanguageCode, errorMessageCode);
}
}

} else {

sails.log.verbose(`validateFieldUsingRegex ${fieldDBName} `+data);
sails.log.verbose('validateFieldUsingRegex regexPattern '+regexPattern);
if(!_.isUndefined(data) && data != null && data != '' && !_.isUndefined(regexPattern) && !_.isUndefined(errorMessageCode) ) {

if(trimLeadingAndTrailingSpacesBeforeValidation) {
let trimData = _.trim(data);
data = trimData;
}

this.validateRegex(data, regexPattern, fieldLanguageCode, errorMessageCode);

if(trimLeadingAndTrailingSpacesBeforeValidation) {
_.set(record,fieldDBName,data);
}
}
}

return Observable.of(record);
}

}
}
Expand Down

0 comments on commit 0cc3ee1

Please sign in to comment.