_common-action-interface-check.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = _default;
  8. var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));
  9. function _default(action, {
  10. checkPath = true,
  11. checkAbortOnFail = true
  12. } = {}) {
  13. // it's not even an object, you fail!
  14. if (typeof action !== 'object') {
  15. return `Invalid action object: ${(0, _stringify.default)(action)}`;
  16. }
  17. const {
  18. path,
  19. abortOnFail
  20. } = action;
  21. if (checkPath && (typeof path !== 'string' || path.length === 0)) {
  22. return `Invalid path "${path}"`;
  23. } // abortOnFail is optional, but if it's provided it needs to be a Boolean
  24. if (checkAbortOnFail && abortOnFail !== undefined && typeof abortOnFail !== 'boolean') {
  25. return `Invalid value for abortOnFail (${abortOnFail} is not a Boolean)`;
  26. }
  27. if ('transform' in action && typeof action.transform !== 'function') {
  28. return `Invalid value for transform (${typeof action.transform} is not a function)`;
  29. }
  30. if (action.type === 'modify' && !('pattern' in action) && !('transform' in action)) {
  31. return 'Invalid modify action (modify must have a pattern or transform function)';
  32. }
  33. if ('skip' in action && typeof action.skip !== 'function') {
  34. return `Invalid value for skip (${typeof action.skip} is not a function)`;
  35. }
  36. return true;
  37. }