UNPKG

1.58 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 error = {
6 message: ({ params: { missingPattern } }) => codegen_1.str `should have property matching pattern '${missingPattern}'`,
7 params: ({ params: { missingPattern } }) => codegen_1._ `{missingPattern: ${missingPattern}}`,
8};
9function getDef() {
10 return {
11 keyword: "patternRequired",
12 type: "object",
13 schemaType: "array",
14 error,
15 code(cxt) {
16 const { gen, schema, data } = cxt;
17 if (schema.length === 0)
18 return;
19 const valid = gen.let("valid", true);
20 for (const pat of schema)
21 validateProperties(pat);
22 function validateProperties(pattern) {
23 const matched = gen.let("matched", false);
24 gen.forIn("key", data, (key) => {
25 gen.assign(matched, codegen_1._ `${_util_1.usePattern(gen, pattern)}.test(${key})`);
26 gen.if(matched, () => gen.break());
27 });
28 cxt.setParams({ missingPattern: pattern });
29 gen.assign(valid, codegen_1.and(valid, matched));
30 cxt.pass(valid);
31 }
32 },
33 metaSchema: {
34 type: "array",
35 items: { type: "string", format: "regex" },
36 uniqueItems: true,
37 },
38 };
39}
40exports.default = getDef;
41module.exports = getDef;
42//# sourceMappingURL=patternRequired.js.map
\No newline at end of file