/** * @description Handles validation of template strings so users cannot submit/save bad templates * NOTE: Also replaces values with predefined default * @type {Class} */ var TemplateValidator = Class.create(); TemplateValidator.prototype = Object.extendsObject(global.AbstractAjaxProcessor, { /** * @description The table for holding template validations * @type {String} */ VALIDATIONS: 'x_scni_template_va_template_validations', /** * @description The ServiceNow ArrayUtil * @type {ArrayUtil} */ AU: new global.ArrayUtil(), /** * @description An array of error messages * @type {Array} */ ERRORS: [], CURRENT : {}, /** * @description Validates a template based on conditions set * @param {String} template The template string * @param {String} table The table for the template * @return {String} A string of errors */ validate: function(template, table, current) { var validations = this.getValidations(table), templateArray = template.toString().split('^'), temp; this.CURRENT = current; while (validations.next()) { this.validateTemplates(validations, templateArray); } current = this.CURRENT; return this.ERRORS.toString(); }, /** * @description The AJAX Handler for the main method * @return {String} A string of errors */ validateAJAX: function() { return this.validate(this.getParameter('sysparm_template_string'), this.getParameter('sysparm_current_table')); }, /** * @description Gets validation records from the validation table * @param {String} table The name of a table * @return {GlideRecord} Records that match the table */ getValidations: function(table) { var validations = new GlideRecord(this.VALIDATIONS); validations.addQuery('table.name', table); validations.query(); return validations; }, /** * @description Processes the validations for each value in the template array * @param {GlideRecord} validations The validation record * @param {Array} templateArray The array of template fields and values */ validateTemplates: function(validations, templateArray) { for (var temp in templateArray) { if (templateArray.hasOwnProperty(temp)) { this.checkValues(validations, templateArray[temp]); } } }, /** * @description Determines if the value in the template needs to be pushed to ERRORS * @param {GlideRecord} validations The validation record * @param {String} temp The template field and value */ checkValues: function(validations, temp) { var item = temp.toString().split('='); if (validations.field_name.element == item[0]) { if (!this.isValueValid(validations.unacceptable_values, item[1])) { this.CURRENT.template = this.CURRENT.template.toString().replace(item[0] + "=" + item[1], item[0] + "=" + validations.default); this.ERRORS.push(this.generateMessage(validations, item)); } } }, /** * @description Generates an error message * @param {GlideRecord} validations The validation record * @param {Array} item The template item (field/value) * @return {String} [description] */ generateMessage: function(validations, item) { return item[0] + " has an invalid value of " + item[1] + ". It cannot be " + validations.unacceptable_values + "."; }, /** * @description Uses the ServiceNow ArrayUtil to check if a value is ok * @param {String} test The value to test against * @param {String} value The value from the template * @return {Boolean} True if the value is ok */ isValueValid: function(test, value) { var testArray = test.toString().split(','); if (this.AU.contains(testArray, value)) { return false; } return true; }, /** * @description Returns a reference qualifier that gets fields based on the table * Note: GlideTableHierarchy would be nice here, but it doesn't work right now. Instead, we'll hope task is all we're doing. * @param {GlideRecord} current The current validation record * @return {String} A reference qualifier for the field name */ fieldRefQual: function(current) { return 'nameIN' + current.table.name + ",task"; }, 'type' : 'TemplateValidator' });