UNPKG

1.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const codegen_1 = require("ajv/dist/compile/codegen");
4const _util_1 = require("./_util");
5const regexpMetaSchema = {
6 type: "object",
7 properties: {
8 pattern: { type: "string" },
9 flags: { type: "string", nullable: true },
10 },
11 required: ["pattern"],
12 additionalProperties: false,
13};
14const metaRegexp = /^\/(.*)\/([gimuy]*)$/;
15function getDef() {
16 return {
17 keyword: "regexp",
18 type: "string",
19 schemaType: ["string", "object"],
20 code(cxt) {
21 const { gen, data, schema } = cxt;
22 const regx = getRegExp(schema);
23 cxt.pass(codegen_1._ `${regx}.test(${data})`);
24 function getRegExp(sch) {
25 if (typeof sch == "object")
26 return _util_1.usePattern(gen, sch.pattern, sch.flags);
27 const rx = metaRegexp.exec(sch);
28 if (rx)
29 return _util_1.usePattern(gen, 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}
38exports.default = getDef;
39module.exports = getDef;
40//# sourceMappingURL=regexp.js.map
\No newline at end of file