index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const formats_1 = require("./formats");
  4. const limit_1 = require("./limit");
  5. const codegen_1 = require("ajv/dist/compile/codegen");
  6. const fullName = new codegen_1.Name("fullFormats");
  7. const fastName = new codegen_1.Name("fastFormats");
  8. const formatsPlugin = (ajv, opts = { keywords: true }) => {
  9. if (Array.isArray(opts)) {
  10. addFormats(ajv, opts, formats_1.fullFormats, fullName);
  11. return ajv;
  12. }
  13. const [formats, exportName] = opts.mode === "fast" ? [formats_1.fastFormats, fastName] : [formats_1.fullFormats, fullName];
  14. const list = opts.formats || formats_1.formatNames;
  15. addFormats(ajv, list, formats, exportName);
  16. if (opts.keywords)
  17. limit_1.default(ajv);
  18. return ajv;
  19. };
  20. formatsPlugin.get = (name, mode = "full") => {
  21. const formats = mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats;
  22. const f = formats[name];
  23. if (!f)
  24. throw new Error(`Unknown format "${name}"`);
  25. return f;
  26. };
  27. function addFormats(ajv, list, fs, exportName) {
  28. var _a;
  29. var _b;
  30. (_a = (_b = ajv.opts.code).formats) !== null && _a !== void 0 ? _a : (_b.formats = codegen_1._ `require("ajv-formats/dist/formats").${exportName}`);
  31. for (const f of list)
  32. ajv.addFormat(f, fs[f]);
  33. }
  34. module.exports = exports = formatsPlugin;
  35. Object.defineProperty(exports, "__esModule", { value: true });
  36. exports.default = formatsPlugin;
  37. //# sourceMappingURL=index.js.map