2
0

input-validator.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. var forge = require('node-forge');
  11. var BigInteger = forge.jsbn.BigInteger;
  12. /**
  13. * Input data validation utility for this module. Only intended for internal
  14. * use.
  15. */
  16. module.exports = {
  17. /**
  18. * Checks if a value is defined.
  19. *
  20. * @function checkIsDefined
  21. * @private
  22. * @param {Object}
  23. * value The value to check.
  24. * @param {string}
  25. * label The error message label for the value.
  26. * @throws {TypeError}
  27. * If the value is undefined.
  28. */
  29. checkIsDefined: function(value, label) {
  30. if (typeof value === 'undefined') {
  31. throw new TypeError(label + ' is undefined.');
  32. }
  33. },
  34. /**
  35. * Checks if a value is not null.
  36. *
  37. * @function checkIsNotNull
  38. * @private
  39. * @param {Object}
  40. * value The value to check.
  41. * @param {string}
  42. * label The error message label for the value.
  43. * @throws {TypeError}
  44. * If the value is null.
  45. */
  46. checkIsNotNull: function(value, label) {
  47. if (value === null) {
  48. throw new TypeError(label + ' is null.');
  49. }
  50. },
  51. /**
  52. * Checks if a value is defined and not null.
  53. *
  54. * @function checkIsDefinedAndNotNull
  55. * @private
  56. * @param {Object}
  57. * value The value to check.
  58. * @param {string}
  59. * label The error message label for the value.
  60. * @throws {TypeError}
  61. * If the value is not defined or it is null.
  62. */
  63. checkIsDefinedAndNotNull: function(value, label) {
  64. this.checkIsDefined(value, label);
  65. this.checkIsNotNull(value, label);
  66. },
  67. /**
  68. * Checks if a value is of an expected type.
  69. *
  70. * @function checkIsType
  71. * @private
  72. * @param {Object}
  73. * value The value to check.
  74. * @param {string}
  75. * type The expected type of the value.
  76. * @param {string}
  77. * label The error message label for the value.
  78. * @throws {TypeError}
  79. * If the value is not of the expected type.
  80. */
  81. checkIsType: function(value, type, label) {
  82. var typeFound = typeof value;
  83. if (typeFound !== type) {
  84. throw new TypeError(
  85. 'Expected ' + label + ' to have type \'' + type + '\' ; Found: \'' +
  86. typeFound + '\'');
  87. }
  88. },
  89. /**
  90. * Checks if a value is an instance of an Object.
  91. *
  92. * @function checkIsInstanceOf
  93. * @private
  94. * @param {Object}
  95. * value The value to check.
  96. * @param {Object}
  97. * obj The Object to check against.
  98. * @param {string}
  99. * objName The Object name, for error handling purposes.
  100. * @param {string}
  101. * label The error message label for the value.
  102. * @throws {TypeError}
  103. * If the object is undefined, null, or it is not an instance of
  104. * the Object.
  105. */
  106. checkIsInstanceOf: function(value, obj, objName, label) {
  107. this.checkIsDefinedAndNotNull(value, label);
  108. if (!(value instanceof obj)) {
  109. throw new TypeError(label + ' is not an instance of Object ' + objName);
  110. }
  111. },
  112. /**
  113. * Checks if a value is an object.
  114. *
  115. * @function checkIsObject
  116. * @private
  117. * @param {Object}
  118. * value The value to check.
  119. * @param {string}
  120. * label The error message label for the value.
  121. * @throws {TypeError}
  122. * If the value is not an object.
  123. */
  124. checkIsObject: function(value, label) {
  125. if (typeof value !== 'object') {
  126. throw new TypeError(label + ' is not an object.');
  127. }
  128. },
  129. /**
  130. * Checks if a value is an object and has properties.
  131. *
  132. * @function checkIsObjectWithProperties
  133. * @private
  134. * @param {Object}
  135. * value The value to check.
  136. * @param {string}
  137. * label The error message label for the value.
  138. * @throws {TypeError}
  139. * If the value is not an object or it has no properties.
  140. */
  141. checkIsObjectWithProperties: function(value, label) {
  142. this.checkIsDefinedAndNotNull(value, label);
  143. this.checkIsObject(value, label);
  144. if (!Object.getOwnPropertyNames(value).length) {
  145. throw new TypeError(label + ' does not have any properties.');
  146. }
  147. },
  148. /**
  149. * Checks if a value is a non-empty string.
  150. *
  151. * @function checkIsNonEmptyString
  152. * @private
  153. * @param {string}
  154. * value The value to check.
  155. * @param {string}
  156. * label The error message label for the value.
  157. * @throws {TypeError}
  158. * If the value is not a non-empty string.
  159. */
  160. checkIsNonEmptyString: function(value, label) {
  161. this.checkIsType(value, 'string', label);
  162. if (value.length === 0) {
  163. throw new TypeError(label + ' is empty.');
  164. }
  165. },
  166. /**
  167. * Checks if a value is a BigInteger object.
  168. *
  169. * @function checkIsBigInteger
  170. * @private
  171. * @param {Object}
  172. * value The value to check.
  173. * @param {string}
  174. * label The error message label for the value.
  175. * @throws {TypeError}
  176. * If the value is not a BigInteger object.
  177. */
  178. checkIsBigInteger: function(value, label) {
  179. this.checkIsDefinedAndNotNull(value, label);
  180. if (typeof value.abs === 'undefined') {
  181. throw new TypeError(label + ' is not a BigInteger object.');
  182. }
  183. },
  184. /**
  185. * Checks if a value is a positive BigInteger object.
  186. *
  187. * @function checkIsPositiveBigInteger
  188. * @private
  189. * @param {Object}
  190. * value The value to check.
  191. * @param {string}
  192. * label The error message label for the value.
  193. * @throws {TypeError}
  194. * If the value is not a positive BigInteger object.
  195. */
  196. checkIsPositiveBigInteger: function(value, label) {
  197. this.checkIsBigInteger(value, label);
  198. if (value.compareTo(BigInteger.ONE) < 0) {
  199. throw new TypeError(label + ' must not be less than 1; Found: ' + value);
  200. }
  201. }
  202. };