Validator.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. 'use strict';
  2. var isArray = require('lodash/isArray');
  3. var isObject = require('lodash/isObject');
  4. var DROPDOWN_TYPE = 'Dropdown';
  5. var VALID_TYPES = [ 'String', 'Text', 'Boolean', 'Hidden', DROPDOWN_TYPE ];
  6. var PROPERTY_TYPE = 'property',
  7. ACTIVITI_PROPERTY_TYPE = 'activiti:property',
  8. ACTIVITI_INPUT_PARAMETER_TYPE = 'activiti:inputParameter',
  9. ACTIVITI_OUTPUT_PARAMETER_TYPE = 'activiti:outputParameter',
  10. ACTIVITI_IN_TYPE = 'activiti:in',
  11. ACTIVITI_OUT_TYPE = 'activiti:out',
  12. ACTIVITI_IN_BUSINESS_KEY_TYPE = 'activiti:in:businessKey',
  13. ACTIVITI_EXECUTION_LISTENER = 'activiti:executionListener',
  14. ACTIVITI_FIELD = 'activiti:field';
  15. var VALID_BINDING_TYPES = [
  16. PROPERTY_TYPE,
  17. ACTIVITI_PROPERTY_TYPE,
  18. ACTIVITI_INPUT_PARAMETER_TYPE,
  19. ACTIVITI_OUTPUT_PARAMETER_TYPE,
  20. ACTIVITI_IN_TYPE,
  21. ACTIVITI_OUT_TYPE,
  22. ACTIVITI_IN_BUSINESS_KEY_TYPE,
  23. ACTIVITI_EXECUTION_LISTENER,
  24. ACTIVITI_FIELD
  25. ];
  26. /**
  27. * A element template validator.
  28. */
  29. function Validator() {
  30. this._templatesById = {};
  31. this._validTemplates = [];
  32. this._errors = [];
  33. /**
  34. * Adds the templates.
  35. *
  36. * @param {Array<TemplateDescriptor>} templates
  37. *
  38. * @return {Validator} self
  39. */
  40. this.addAll = function(templates) {
  41. if (!isArray(templates)) {
  42. this._logError('templates must be []');
  43. } else {
  44. templates.forEach(this.add, this);
  45. }
  46. return this;
  47. };
  48. /**
  49. * Add the given element template, if it is valid.
  50. *
  51. * @param {TemplateDescriptor} template
  52. *
  53. * @return {Validator} self
  54. */
  55. this.add = function(template) {
  56. var err = this._validateTemplate(template);
  57. if (!err) {
  58. this._templatesById[template.id] = template;
  59. this._validTemplates.push(template);
  60. }
  61. return this;
  62. };
  63. /**
  64. * Validate given template and return error (if any).
  65. *
  66. * @param {TemplateDescriptor} template
  67. *
  68. * @return {Error} validation error, if any
  69. */
  70. this._validateTemplate = function(template) {
  71. var err,
  72. id = template.id,
  73. appliesTo = template.appliesTo,
  74. properties = template.properties,
  75. scopes = template.scopes;
  76. if (!id) {
  77. return this._logError('missing template id');
  78. }
  79. if (id in this._templatesById) {
  80. return this._logError('template id <' + id + '> already used');
  81. }
  82. if (!isArray(appliesTo)) {
  83. err = this._logError('missing appliesTo=[]', template);
  84. }
  85. if (!isArray(properties)) {
  86. err = this._logError('missing properties=[]', template);
  87. } else {
  88. if (!this._validateProperties(properties)) {
  89. err = new Error('invalid properties');
  90. }
  91. }
  92. if (scopes) {
  93. err = this._validateScopes(template, scopes);
  94. }
  95. return err;
  96. };
  97. this._validateScopes = function(template, scopes) {
  98. var err,
  99. scope,
  100. scopeName;
  101. if (!isObject(scopes) || isArray(scopes)) {
  102. return this._logError('invalid scopes, should be scopes={}', template);
  103. }
  104. for (scopeName in scopes) {
  105. scope = scopes[scopeName];
  106. if (!isObject(scope) || isArray(scope)) {
  107. err = this._logError('invalid scope, should be scope={}', template);
  108. }
  109. if (!isArray(scope.properties)) {
  110. err = this._logError(
  111. 'missing properties=[] in scope <' + scopeName + '>', template
  112. );
  113. } else {
  114. if (!this._validateProperties(scope.properties)) {
  115. err = new Error('invalid properties in scope <' + scopeName + '>');
  116. }
  117. }
  118. }
  119. return err;
  120. };
  121. /**
  122. * Validate properties and return false if any is invalid.
  123. *
  124. * @param {Array<PropertyDescriptor>} properties
  125. *
  126. * @return {Boolean} true if all properties are valid
  127. */
  128. this._validateProperties = function(properties) {
  129. var validProperties = properties.filter(this._validateProperty, this);
  130. return properties.length === validProperties.length;
  131. };
  132. /**
  133. * Validate property and return false, if there was
  134. * a validation error.
  135. *
  136. * @param {PropertyDescriptor} property
  137. *
  138. * @return {Boolean} true if property is valid
  139. */
  140. this._validateProperty = function(property) {
  141. var type = property.type,
  142. binding = property.binding;
  143. var err;
  144. var bindingType = binding.type;
  145. if (VALID_TYPES.indexOf(type) === -1) {
  146. err = this._logError(
  147. 'invalid property type <' + type + '>; ' +
  148. 'must be any of { ' + VALID_TYPES.join(', ') + ' }'
  149. );
  150. }
  151. if (type === DROPDOWN_TYPE && bindingType !== ACTIVITI_EXECUTION_LISTENER) {
  152. if (!isArray(property.choices)) {
  153. err = this._logError(
  154. 'must provide choices=[] with ' + DROPDOWN_TYPE + ' type'
  155. );
  156. } else
  157. if (!property.choices.every(isDropdownChoiceValid)) {
  158. err = this._logError(
  159. '{ name, value } must be specified for ' +
  160. DROPDOWN_TYPE + ' choices'
  161. );
  162. }
  163. }
  164. if (!binding) {
  165. return this._logError('property missing binding');
  166. }
  167. if (VALID_BINDING_TYPES.indexOf(bindingType) === -1) {
  168. err = this._logError(
  169. 'invalid property.binding type <' + bindingType + '>; ' +
  170. 'must be any of { ' + VALID_BINDING_TYPES.join(', ') + ' }'
  171. );
  172. }
  173. if (bindingType === PROPERTY_TYPE ||
  174. bindingType === ACTIVITI_PROPERTY_TYPE ||
  175. bindingType === ACTIVITI_INPUT_PARAMETER_TYPE ||
  176. bindingType === ACTIVITI_FIELD) {
  177. if (!binding.name) {
  178. err = this._logError(
  179. 'property.binding <' + bindingType + '> requires name'
  180. );
  181. }
  182. }
  183. if (bindingType === ACTIVITI_OUTPUT_PARAMETER_TYPE) {
  184. if (!binding.source) {
  185. err = this._logError(
  186. 'property.binding <' + bindingType + '> requires source'
  187. );
  188. }
  189. }
  190. if (bindingType === ACTIVITI_IN_TYPE) {
  191. if (!binding.variables && !binding.target) {
  192. err = this._logError(
  193. 'property.binding <' + bindingType + '> requires ' +
  194. 'variables or target'
  195. );
  196. }
  197. }
  198. if (bindingType === ACTIVITI_OUT_TYPE) {
  199. if (!binding.variables && !binding.source && !binding.sourceExpression) {
  200. err = this._logError(
  201. 'property.binding <' + bindingType + '> requires ' +
  202. 'variables, sourceExpression or source'
  203. );
  204. }
  205. }
  206. if (bindingType === ACTIVITI_EXECUTION_LISTENER) {
  207. if (type !== 'Hidden') {
  208. err = this._logError(
  209. 'invalid property type <' + type + '> for ' + ACTIVITI_EXECUTION_LISTENER + '; ' +
  210. 'must be <Hidden>'
  211. );
  212. }
  213. }
  214. return !err;
  215. };
  216. this._logError = function(err, template) {
  217. if (typeof err === 'string') {
  218. if (template) {
  219. err = 'template(id: ' + template.id + ') ' + err;
  220. }
  221. err = new Error(err);
  222. }
  223. this._errors.push(err);
  224. return err;
  225. };
  226. this.getErrors = function() {
  227. return this._errors;
  228. };
  229. this.getValidTemplates = function() {
  230. return this._validTemplates;
  231. };
  232. }
  233. module.exports = Validator;
  234. // helpers ///////////////////////////////////
  235. function isDropdownChoiceValid(c) {
  236. return 'name' in c && 'value' in c;
  237. }