validatorPrimitives.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ValidationError = void 0;
  6. exports.createMetadataValidator = createMetadataValidator;
  7. exports.findValidator = findValidator;
  8. exports.maybeFindValidator = maybeFindValidator;
  9. exports.tUndefined = exports.tType = exports.tString = exports.tOptional = exports.tObject = exports.tNumber = exports.tEnum = exports.tChannel = exports.tBoolean = exports.tBinary = exports.tArray = exports.tAny = exports.scheme = void 0;
  10. var _utils = require("../utils");
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class ValidationError extends Error {}
  27. exports.ValidationError = ValidationError;
  28. const scheme = exports.scheme = {};
  29. function findValidator(type, method, kind) {
  30. const validator = maybeFindValidator(type, method, kind);
  31. if (!validator) throw new ValidationError(`Unknown scheme for ${kind}: ${type}.${method}`);
  32. return validator;
  33. }
  34. function maybeFindValidator(type, method, kind) {
  35. const schemeName = type + (kind === 'Initializer' ? '' : method[0].toUpperCase() + method.substring(1)) + kind;
  36. return scheme[schemeName];
  37. }
  38. function createMetadataValidator() {
  39. return tOptional(scheme['Metadata']);
  40. }
  41. const tNumber = (arg, path, context) => {
  42. if (arg instanceof Number) return arg.valueOf();
  43. if (typeof arg === 'number') return arg;
  44. throw new ValidationError(`${path}: expected number, got ${typeof arg}`);
  45. };
  46. exports.tNumber = tNumber;
  47. const tBoolean = (arg, path, context) => {
  48. if (arg instanceof Boolean) return arg.valueOf();
  49. if (typeof arg === 'boolean') return arg;
  50. throw new ValidationError(`${path}: expected boolean, got ${typeof arg}`);
  51. };
  52. exports.tBoolean = tBoolean;
  53. const tString = (arg, path, context) => {
  54. if (arg instanceof String) return arg.valueOf();
  55. if (typeof arg === 'string') return arg;
  56. throw new ValidationError(`${path}: expected string, got ${typeof arg}`);
  57. };
  58. exports.tString = tString;
  59. const tBinary = (arg, path, context) => {
  60. if (context.binary === 'fromBase64') {
  61. if (arg instanceof String) return Buffer.from(arg.valueOf(), 'base64');
  62. if (typeof arg === 'string') return Buffer.from(arg, 'base64');
  63. throw new ValidationError(`${path}: expected base64-encoded buffer, got ${typeof arg}`);
  64. }
  65. if (context.binary === 'toBase64') {
  66. if (!(arg instanceof Buffer)) throw new ValidationError(`${path}: expected Buffer, got ${typeof arg}`);
  67. return arg.toString('base64');
  68. }
  69. if (context.binary === 'buffer') {
  70. if (!(arg instanceof Buffer)) throw new ValidationError(`${path}: expected Buffer, got ${typeof arg}`);
  71. return arg;
  72. }
  73. throw new ValidationError(`Unsupported binary behavior "${context.binary}"`);
  74. };
  75. exports.tBinary = tBinary;
  76. const tUndefined = (arg, path, context) => {
  77. if (Object.is(arg, undefined)) return arg;
  78. throw new ValidationError(`${path}: expected undefined, got ${typeof arg}`);
  79. };
  80. exports.tUndefined = tUndefined;
  81. const tAny = (arg, path, context) => {
  82. return arg;
  83. };
  84. exports.tAny = tAny;
  85. const tOptional = v => {
  86. return (arg, path, context) => {
  87. if (Object.is(arg, undefined)) return arg;
  88. return v(arg, path, context);
  89. };
  90. };
  91. exports.tOptional = tOptional;
  92. const tArray = v => {
  93. return (arg, path, context) => {
  94. if (!Array.isArray(arg)) throw new ValidationError(`${path}: expected array, got ${typeof arg}`);
  95. return arg.map((x, index) => v(x, path + '[' + index + ']', context));
  96. };
  97. };
  98. exports.tArray = tArray;
  99. const tObject = s => {
  100. return (arg, path, context) => {
  101. if (Object.is(arg, null)) throw new ValidationError(`${path}: expected object, got null`);
  102. if (typeof arg !== 'object') throw new ValidationError(`${path}: expected object, got ${typeof arg}`);
  103. const result = {};
  104. for (const [key, v] of Object.entries(s)) {
  105. const value = v(arg[key], path ? path + '.' + key : key, context);
  106. if (!Object.is(value, undefined)) result[key] = value;
  107. }
  108. if ((0, _utils.isUnderTest)()) {
  109. for (const [key, value] of Object.entries(arg)) {
  110. if (key.startsWith('__testHook')) result[key] = value;
  111. }
  112. }
  113. return result;
  114. };
  115. };
  116. exports.tObject = tObject;
  117. const tEnum = e => {
  118. return (arg, path, context) => {
  119. if (!e.includes(arg)) throw new ValidationError(`${path}: expected one of (${e.join('|')})`);
  120. return arg;
  121. };
  122. };
  123. exports.tEnum = tEnum;
  124. const tChannel = names => {
  125. return (arg, path, context) => {
  126. return context.tChannelImpl(names, arg, path, context);
  127. };
  128. };
  129. exports.tChannel = tChannel;
  130. const tType = name => {
  131. return (arg, path, context) => {
  132. const v = scheme[name];
  133. if (!v) throw new ValidationError(path + ': unknown type "' + name + '"');
  134. return v(arg, path, context);
  135. };
  136. };
  137. exports.tType = tType;