patternRequired.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 error = {
  6. message: ({ params: { missingPattern } }) => (0, codegen_1.str) `should have property matching pattern '${missingPattern}'`,
  7. params: ({ params: { missingPattern } }) => (0, codegen_1._) `{missingPattern: ${missingPattern}}`,
  8. };
  9. function 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, (0, codegen_1._) `${(0, _util_1.usePattern)(cxt, pattern)}.test(${key})`);
  26. gen.if(matched, () => gen.break());
  27. });
  28. cxt.setParams({ missingPattern: pattern });
  29. gen.assign(valid, (0, 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. }
  40. exports.default = getDef;
  41. module.exports = getDef;
  42. //# sourceMappingURL=patternRequired.js.map