patternProperties.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. strictSchema: it.strictSchema,
  56. }, valid);
  57. if (it.opts.unevaluated && props !== true) {
  58. gen.assign(codegen_1._ `${props}[${key}]`, true);
  59. }
  60. else if (!it.allErrors) {
  61. // can short-circuit if `unevaluatedProperties` is not supported (opts.next === false)
  62. // or if all properties were evaluated (props === true)
  63. gen.if(codegen_1.not(valid), () => gen.break());
  64. }
  65. });
  66. });
  67. }
  68. },
  69. };
  70. exports.default = def;
  71. //# sourceMappingURL=patternProperties.js.map