index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Validator = void 0;
  4. const address_js_1 = require("../utils/address.js");
  5. const validations_js_1 = require("../utils/validations.js");
  6. class Validator {
  7. invalid(param) {
  8. return param.msg || `Invalid ${param.name}${param.type === 'address' ? ' address' : ''} provided`;
  9. }
  10. notPositive(param) {
  11. return `${param.name} must be a positive integer`;
  12. }
  13. notEqual(param) {
  14. return param.msg || `${param.names?.[0]} can not be equal to ${param.names?.[1]}`;
  15. }
  16. notValid(params) {
  17. const normalized = {};
  18. let no = false;
  19. for (const param of params) {
  20. const { name, names, value, type, gt, lt, gte, lte, optional } = param;
  21. if (optional && (!(0, validations_js_1.isNotNullOrUndefined)(value) || (type !== 'boolean' && value === false)))
  22. continue;
  23. normalized[name] = param.value;
  24. switch (type) {
  25. case 'address':
  26. if (!(0, address_js_1.isAddress)(value)) {
  27. no = true;
  28. }
  29. else {
  30. normalized[name] = (0, address_js_1.toHex)(value);
  31. }
  32. break;
  33. case 'integer':
  34. if (!(0, validations_js_1.isInteger)(value) ||
  35. (typeof gt === 'number' && value <= gt) ||
  36. (typeof lt === 'number' && value >= lt) ||
  37. (typeof gte === 'number' && value < gte) ||
  38. (typeof lte === 'number' && value > lte)) {
  39. no = true;
  40. }
  41. break;
  42. case 'positive-integer':
  43. if (!(0, validations_js_1.isInteger)(value) || value <= 0) {
  44. throw new Error(this.notPositive(param));
  45. }
  46. break;
  47. case 'tokenId':
  48. if (!(0, validations_js_1.isString)(value) || !value.length) {
  49. no = true;
  50. }
  51. break;
  52. case 'notEmptyObject':
  53. if (!(0, validations_js_1.isObject)(value) || !Object.keys(value).length) {
  54. no = true;
  55. }
  56. break;
  57. case 'notEqual':
  58. if (names && normalized[names[0]] === normalized[names[1]]) {
  59. throw new Error(this.notEqual(param));
  60. }
  61. break;
  62. case 'resource':
  63. if (!['BANDWIDTH', 'ENERGY'].includes(value)) {
  64. no = true;
  65. }
  66. break;
  67. case 'url':
  68. if (!(0, validations_js_1.isValidURL)(value)) {
  69. no = true;
  70. }
  71. break;
  72. case 'hex':
  73. if (!(0, validations_js_1.isHex)(value)) {
  74. no = true;
  75. }
  76. break;
  77. case 'array':
  78. if (!Array.isArray(value)) {
  79. no = true;
  80. }
  81. break;
  82. case 'not-empty-string':
  83. if (!(0, validations_js_1.isString)(value) || !value.length) {
  84. no = true;
  85. }
  86. break;
  87. case 'boolean':
  88. if (!(0, validations_js_1.isBoolean)(value)) {
  89. no = true;
  90. }
  91. break;
  92. case 'string':
  93. if (!(0, validations_js_1.isString)(value) ||
  94. (typeof gt === 'number' && value.length <= gt) ||
  95. (typeof lt === 'number' && value.length >= lt) ||
  96. (typeof gte === 'number' && value.length < gte) ||
  97. (typeof lte === 'number' && value.length > lte)) {
  98. no = true;
  99. }
  100. break;
  101. }
  102. if (no) {
  103. throw new Error(this.invalid(param));
  104. }
  105. }
  106. return false;
  107. }
  108. }
  109. exports.Validator = Validator;
  110. //# sourceMappingURL=index.js.map