intersection.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseIntersectionDef = void 0;
  4. const parseDef_js_1 = require("../parseDef.js");
  5. const isJsonSchema7AllOfType = (type) => {
  6. if ("type" in type && type.type === "string")
  7. return false;
  8. return "allOf" in type;
  9. };
  10. function parseIntersectionDef(def, refs) {
  11. const allOf = [
  12. (0, parseDef_js_1.parseDef)(def.left._def, {
  13. ...refs,
  14. currentPath: [...refs.currentPath, "allOf", "0"],
  15. }),
  16. (0, parseDef_js_1.parseDef)(def.right._def, {
  17. ...refs,
  18. currentPath: [...refs.currentPath, "allOf", "1"],
  19. }),
  20. ].filter((x) => !!x);
  21. let unevaluatedProperties = refs.target === "jsonSchema2019-09"
  22. ? { unevaluatedProperties: false }
  23. : undefined;
  24. const mergedAllOf = [];
  25. // If either of the schemas is an allOf, merge them into a single allOf
  26. allOf.forEach((schema) => {
  27. if (isJsonSchema7AllOfType(schema)) {
  28. mergedAllOf.push(...schema.allOf);
  29. if (schema.unevaluatedProperties === undefined) {
  30. // If one of the schemas has no unevaluatedProperties set,
  31. // the merged schema should also have no unevaluatedProperties set
  32. unevaluatedProperties = undefined;
  33. }
  34. }
  35. else {
  36. let nestedSchema = schema;
  37. if ("additionalProperties" in schema &&
  38. schema.additionalProperties === false) {
  39. const { additionalProperties, ...rest } = schema;
  40. nestedSchema = rest;
  41. }
  42. else {
  43. // As soon as one of the schemas has additionalProperties set not to false, we allow unevaluatedProperties
  44. unevaluatedProperties = undefined;
  45. }
  46. mergedAllOf.push(nestedSchema);
  47. }
  48. });
  49. return mergedAllOf.length
  50. ? {
  51. allOf: mergedAllOf,
  52. ...unevaluatedProperties,
  53. }
  54. : undefined;
  55. }
  56. exports.parseIntersectionDef = parseIntersectionDef;