patternProperties.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const code_1 = require("../code");
  4. const codegen_1 = require("../../compile/codegen");
  5. const subschema_1 = require("../../compile/subschema");
  6. const validate_1 = require("../../compile/validate");
  7. const util_1 = require("../../compile/util");
  8. const def = {
  9. keyword: "patternProperties",
  10. type: "object",
  11. schemaType: "object",
  12. code(cxt) {
  13. const { gen, schema, data, parentSchema, it } = cxt;
  14. const { opts } = it;
  15. const patterns = code_1.schemaProperties(it, schema);
  16. // TODO mark properties matching patterns with always valid schemas as evaluated
  17. if (patterns.length === 0)
  18. return;
  19. const checkProperties = opts.strict && !opts.allowMatchingProperties && parentSchema.properties;
  20. const valid = gen.name("valid");
  21. if (it.props !== true && !(it.props instanceof codegen_1.Name)) {
  22. it.props = util_1.evaluatedPropsToName(gen, it.props);
  23. }
  24. const { props } = it;
  25. validatePatternProperties();
  26. function validatePatternProperties() {
  27. for (const pat of patterns) {
  28. if (checkProperties)
  29. checkMatchingProperties(pat);
  30. if (it.allErrors) {
  31. validateProperties(pat);
  32. }
  33. else {
  34. gen.var(valid, true); // TODO var
  35. validateProperties(pat);
  36. gen.if(valid);
  37. }
  38. }
  39. }
  40. function checkMatchingProperties(pat) {
  41. for (const prop in checkProperties) {
  42. if (new RegExp(pat).test(prop)) {
  43. validate_1.checkStrictMode(it, `property ${prop} matches pattern ${pat} (use allowMatchingProperties)`);
  44. }
  45. }
  46. }
  47. function validateProperties(pat) {
  48. gen.forIn("key", data, (key) => {
  49. gen.if(codegen_1._ `${code_1.usePattern(gen, pat)}.test(${key})`, () => {
  50. cxt.subschema({
  51. keyword: "patternProperties",
  52. schemaProp: pat,
  53. dataProp: key,
  54. dataPropType: subschema_1.Type.Str,
  55. }, valid);
  56. if (it.opts.unevaluated && props !== true) {
  57. gen.assign(codegen_1._ `${props}[${key}]`, true);
  58. }
  59. else if (!it.allErrors) {
  60. // can short-circuit if `unevaluatedProperties` is not supported (opts.next === false)
  61. // or if all properties were evaluated (props === true)
  62. gen.if(codegen_1.not(valid), () => gen.break());
  63. }
  64. });
  65. });
  66. }
  67. },
  68. };
  69. exports.default = def;
  70. //# sourceMappingURL=patternProperties.js.map