intersection.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseIntersectionDef = void 0;
  4. const parseDef_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_1.parseDef)(def.left._def, {
  13. ...refs,
  14. currentPath: [...refs.currentPath, 'allOf', '0'],
  15. }),
  16. (0, parseDef_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' ? { unevaluatedProperties: false } : undefined;
  22. const mergedAllOf = [];
  23. // If either of the schemas is an allOf, merge them into a single allOf
  24. allOf.forEach((schema) => {
  25. if (isJsonSchema7AllOfType(schema)) {
  26. mergedAllOf.push(...schema.allOf);
  27. if (schema.unevaluatedProperties === undefined) {
  28. // If one of the schemas has no unevaluatedProperties set,
  29. // the merged schema should also have no unevaluatedProperties set
  30. unevaluatedProperties = undefined;
  31. }
  32. }
  33. else {
  34. let nestedSchema = schema;
  35. if ('additionalProperties' in schema && schema.additionalProperties === false) {
  36. const { additionalProperties, ...rest } = schema;
  37. nestedSchema = rest;
  38. }
  39. else {
  40. // As soon as one of the schemas has additionalProperties set not to false, we allow unevaluatedProperties
  41. unevaluatedProperties = undefined;
  42. }
  43. mergedAllOf.push(nestedSchema);
  44. }
  45. });
  46. return mergedAllOf.length ?
  47. {
  48. allOf: mergedAllOf,
  49. ...unevaluatedProperties,
  50. }
  51. : undefined;
  52. }
  53. exports.parseIntersectionDef = parseIntersectionDef;
  54. //# sourceMappingURL=intersection.js.map