validate.js 1016 B

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.validatePath = exports.validateJsonPointer = void 0;
  4. const validateJsonPointer = (pointer) => {
  5. if (typeof pointer === 'string') {
  6. if (pointer) {
  7. if (pointer[0] !== '/')
  8. throw new Error('POINTER_INVALID');
  9. if (pointer.length > 1024)
  10. throw new Error('POINTER_TOO_LONG');
  11. }
  12. }
  13. else
  14. (0, exports.validatePath)(pointer);
  15. };
  16. exports.validateJsonPointer = validateJsonPointer;
  17. const { isArray } = Array;
  18. const validatePath = (path) => {
  19. if (!isArray(path))
  20. throw new Error('Invalid path.');
  21. if (path.length > 256)
  22. throw new Error('Path too long.');
  23. for (const step of path) {
  24. switch (typeof step) {
  25. case 'string':
  26. case 'number':
  27. continue;
  28. default:
  29. throw new Error('Invalid path step.');
  30. }
  31. }
  32. };
  33. exports.validatePath = validatePath;