input-validator.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. /**
  11. * Input data validation utility for this module. Only intended for internal
  12. * use.
  13. */
  14. module.exports = {
  15. /**
  16. * Checks if a value is defined.
  17. *
  18. * @function checkIsDefined
  19. * @private
  20. * @param {Object}
  21. * value The value to check.
  22. * @param {string}
  23. * label The error message label for the value.
  24. * @throws {TypeError}
  25. * If the value is undefined.
  26. */
  27. checkIsDefined: function(value, label) {
  28. if (typeof value === 'undefined') {
  29. throw new TypeError(label + ' is undefined.');
  30. }
  31. },
  32. /**
  33. * Checks if a value is not null.
  34. *
  35. * @function checkIsNotNull
  36. * @private
  37. * @param {Object}
  38. * value The value to check.
  39. * @param {string}
  40. * label The error message label for the value.
  41. * @throws {TypeError}
  42. * If the value is null.
  43. */
  44. checkIsNotNull: function(value, label) {
  45. if (value === null) {
  46. throw new TypeError(label + ' is null.');
  47. }
  48. },
  49. /**
  50. * Checks if a value is defined and not null.
  51. *
  52. * @function checkIsDefinedAndNotNull
  53. * @private
  54. * @param {Object}
  55. * value The value to check.
  56. * @param {string}
  57. * label The error message label for the value.
  58. * @throws {TypeError}
  59. * If the value is not defined or it is null.
  60. */
  61. checkIsDefinedAndNotNull: function(value, label) {
  62. this.checkIsDefined(value, label);
  63. this.checkIsNotNull(value, label);
  64. },
  65. /**
  66. * Checks if a value is of an expected type.
  67. *
  68. * @function checkIsType
  69. * @private
  70. * @param {Object}
  71. * value The value to check.
  72. * @param {string}
  73. * type The expected type of the value.
  74. * @param {string}
  75. * label The error message label for the value.
  76. * @throws {TypeError}
  77. * If the value is not of the expected type.
  78. */
  79. checkIsType: function(value, type, label) {
  80. var typeFound = typeof value;
  81. if (typeFound !== type) {
  82. throw new TypeError(
  83. 'Expected ' + label + ' to have type \'' + type + '\' ; Found: \'' +
  84. typeFound + '\'');
  85. }
  86. },
  87. /**
  88. * Checks if a value is an object.
  89. *
  90. * @function checkIsObject
  91. * @private
  92. * @param {Object}
  93. * value The value to check.
  94. * @param {string}
  95. * label The error message label for the value.
  96. * @throws {TypeError}
  97. * If the value is not an object.
  98. */
  99. checkIsObject: function(value, label) {
  100. if (typeof value !== 'object') {
  101. throw new TypeError(label + ' is not an object.');
  102. }
  103. },
  104. /**
  105. * Checks if a value is an object and has properties.
  106. *
  107. * @function checkIsObjectWithProperties
  108. * @private
  109. * @param {Object}
  110. * value The value to check.
  111. * @param {string}
  112. * label The error message label for the value.
  113. * @throws {TypeError}
  114. * If the value is not an object or it has no properties.
  115. */
  116. checkIsObjectWithProperties: function(value, label) {
  117. this.checkIsDefinedAndNotNull(value, label);
  118. this.checkIsObject(value, label);
  119. if (!Object.getOwnPropertyNames(value).length) {
  120. throw new TypeError(label + ' does not have any properties.');
  121. }
  122. },
  123. /**
  124. * Checks if a value is an Array object.
  125. *
  126. * @function checkIsArray
  127. * @private
  128. * @param {Array}
  129. * array The value to check.
  130. * @param {string}
  131. * label The error message label for the value.
  132. * @throws {TypeError}
  133. * If the value is not an Array object.
  134. */
  135. checkIsArray: function(value, label) {
  136. this.checkIsDefinedAndNotNull(value, 'Array of ' + label);
  137. if (value.constructor !== Array) {
  138. throw new TypeError('Array of ' + label + ' is not of type Array.');
  139. }
  140. },
  141. /**
  142. * Checks if a value is a non-empty Array object.
  143. *
  144. * @function checkIsNonEmptyArray
  145. * @private
  146. * @param {Array}
  147. * array The value to check.
  148. * @param {string}
  149. * label The error message label for the value.
  150. * @throws {TypeError}
  151. * If the value is not a non-empty Array object.
  152. */
  153. checkIsNonEmptyArray: function(value, label) {
  154. this.checkIsArray(value, label);
  155. if (value.length < 1) {
  156. throw new TypeError('Array of ' + label + ' is empty.');
  157. }
  158. },
  159. /**
  160. * Checks if a value is a two-dimensional Array object.
  161. *
  162. * @function checkIsTwoDimensionalArray
  163. * @private
  164. * @param {Array}
  165. * array The value to check.
  166. * @param {string}
  167. * label The error message label for the value.
  168. * @throws {TypeError}
  169. * If the value is nota a two-dimensional Array object.
  170. */
  171. checkIsTwoDimensionalArray: function(value, label) {
  172. this.checkIsNonEmptyArray(value, label);
  173. for (var i = 0; i < value.length; i++) {
  174. this.checkIsArray(value[i], label);
  175. }
  176. },
  177. /**
  178. * Checks if a value is a non-empty string.
  179. *
  180. * @function checkIsNonEmptyString
  181. * @private
  182. * @param {string}
  183. * value The value to check.
  184. * @param {string}
  185. * label The error message label for the value.
  186. * @throws {TypeError}
  187. * If the value is not a non-empty string.
  188. */
  189. checkIsNonEmptyString: function(value, label) {
  190. this.checkIsType(value, 'string', label);
  191. if (value.length === 0) {
  192. throw new TypeError(label + ' is empty.');
  193. }
  194. }
  195. };