regexp.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const codegen_1 = require("ajv/dist/compile/codegen");
  4. const _util_1 = require("./_util");
  5. const regexpMetaSchema = {
  6. type: "object",
  7. properties: {
  8. pattern: { type: "string" },
  9. flags: { type: "string", nullable: true },
  10. },
  11. required: ["pattern"],
  12. additionalProperties: false,
  13. };
  14. const metaRegexp = /^\/(.*)\/([gimuy]*)$/;
  15. function getDef() {
  16. return {
  17. keyword: "regexp",
  18. type: "string",
  19. schemaType: ["string", "object"],
  20. code(cxt) {
  21. const { data, schema } = cxt;
  22. const regx = getRegExp(schema);
  23. cxt.pass((0, codegen_1._) `${regx}.test(${data})`);
  24. function getRegExp(sch) {
  25. if (typeof sch == "object")
  26. return (0, _util_1.usePattern)(cxt, sch.pattern, sch.flags);
  27. const rx = metaRegexp.exec(sch);
  28. if (rx)
  29. return (0, _util_1.usePattern)(cxt, rx[1], rx[2]);
  30. throw new Error("cannot parse string into RegExp");
  31. }
  32. },
  33. metaSchema: {
  34. anyOf: [{ type: "string" }, regexpMetaSchema],
  35. },
  36. };
  37. }
  38. exports.default = getDef;
  39. module.exports = getDef;
  40. //# sourceMappingURL=regexp.js.map