2
0

input-validator.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 an instance of an Object.
  67. *
  68. * @function checkIsInstanceOf
  69. * @private
  70. * @param {Object}
  71. * value The value to check.
  72. * @param {Object}
  73. * obj The Object to check against.
  74. * @param {string}
  75. * objName The Object name, for error handling purposes.
  76. * @param {string}
  77. * label The error message label for the value.
  78. * @throws {TypeError}
  79. * If the object is undefined, null, or it is not an instance of
  80. * the Object.
  81. */
  82. checkIsInstanceOf: function(value, obj, objName, label) {
  83. this.checkIsDefinedAndNotNull(value, label);
  84. if (!(value instanceof obj)) {
  85. throw new TypeError(label + ' is not an instance of Object ' + objName);
  86. }
  87. },
  88. /**
  89. * Checks if a value is an object.
  90. *
  91. * @function checkIsObject
  92. * @private
  93. * @param {Object}
  94. * value The value to check.
  95. * @param {string}
  96. * label The error message label for the value.
  97. * @throws {TypeError}
  98. * If the value is not an object.
  99. */
  100. checkIsObject: function(value, label) {
  101. if (typeof value !== 'object') {
  102. throw new TypeError(label + ' is not an object.');
  103. }
  104. },
  105. /**
  106. * Checks if a value is an object and has properties.
  107. *
  108. * @function checkIsObjectWithProperties
  109. * @private
  110. * @param {Object}
  111. * value The value to check.
  112. * @param {string}
  113. * label The error message label for the value.
  114. * @throws {TypeError}
  115. * If the value is not an object or it has no properties.
  116. */
  117. checkIsObjectWithProperties: function(value, label) {
  118. this.checkIsDefinedAndNotNull(value, label);
  119. this.checkIsObject(value, label);
  120. if (!Object.getOwnPropertyNames(value).length) {
  121. throw new TypeError(label + ' does not have any properties.');
  122. }
  123. }
  124. };