validator.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. import {VERSION} from '../env/data.js';
  3. import AxiosError from '../core/AxiosError.js';
  4. const validators = {};
  5. // eslint-disable-next-line func-names
  6. ['object', 'boolean', 'number', 'function', 'string', 'symbol'].forEach((type, i) => {
  7. validators[type] = function validator(thing) {
  8. return typeof thing === type || 'a' + (i < 1 ? 'n ' : ' ') + type;
  9. };
  10. });
  11. const deprecatedWarnings = {};
  12. /**
  13. * Transitional option validator
  14. *
  15. * @param {function|boolean?} validator - set to false if the transitional option has been removed
  16. * @param {string?} version - deprecated version / removed since version
  17. * @param {string?} message - some message with additional info
  18. *
  19. * @returns {function}
  20. */
  21. validators.transitional = function transitional(validator, version, message) {
  22. function formatMessage(opt, desc) {
  23. return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
  24. }
  25. // eslint-disable-next-line func-names
  26. return (value, opt, opts) => {
  27. if (validator === false) {
  28. throw new AxiosError(
  29. formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')),
  30. AxiosError.ERR_DEPRECATED
  31. );
  32. }
  33. if (version && !deprecatedWarnings[opt]) {
  34. deprecatedWarnings[opt] = true;
  35. // eslint-disable-next-line no-console
  36. console.warn(
  37. formatMessage(
  38. opt,
  39. ' has been deprecated since v' + version + ' and will be removed in the near future'
  40. )
  41. );
  42. }
  43. return validator ? validator(value, opt, opts) : true;
  44. };
  45. };
  46. validators.spelling = function spelling(correctSpelling) {
  47. return (value, opt) => {
  48. // eslint-disable-next-line no-console
  49. console.warn(`${opt} is likely a misspelling of ${correctSpelling}`);
  50. return true;
  51. }
  52. };
  53. /**
  54. * Assert object's properties type
  55. *
  56. * @param {object} options
  57. * @param {object} schema
  58. * @param {boolean?} allowUnknown
  59. *
  60. * @returns {object}
  61. */
  62. function assertOptions(options, schema, allowUnknown) {
  63. if (typeof options !== 'object') {
  64. throw new AxiosError('options must be an object', AxiosError.ERR_BAD_OPTION_VALUE);
  65. }
  66. const keys = Object.keys(options);
  67. let i = keys.length;
  68. while (i-- > 0) {
  69. const opt = keys[i];
  70. const validator = schema[opt];
  71. if (validator) {
  72. const value = options[opt];
  73. const result = value === undefined || validator(value, opt, options);
  74. if (result !== true) {
  75. throw new AxiosError('option ' + opt + ' must be ' + result, AxiosError.ERR_BAD_OPTION_VALUE);
  76. }
  77. continue;
  78. }
  79. if (allowUnknown !== true) {
  80. throw new AxiosError('Unknown option ' + opt, AxiosError.ERR_BAD_OPTION);
  81. }
  82. }
  83. }
  84. export default {
  85. assertOptions,
  86. validators
  87. };