intersection.mjs 1.9 KB

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