๐Ÿ“ฆ YMFE / yapi

๐Ÿ“„ mergeJsonSchema.js ยท 34 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34function isPlainObject(obj) {
  return obj ? typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype : false;
}

function handleProperties(sourceProperties, mergeProperties){
  if(!isPlainObject(mergeProperties)){
    return mergeProperties
  }
  if(! isPlainObject(sourceProperties)){
    return mergeProperties
  }
  Object.keys(mergeProperties).forEach(key=>{
    mergeProperties[key]= handleSchema(sourceProperties[key], mergeProperties[key])
  })
  return mergeProperties;
}


function handleSchema(source, merge){
  if(!isPlainObject(source)) return merge;
  if(!isPlainObject(merge)) return merge;
  let result = {}
  Object.assign(result, source, merge)
  if(merge.type === 'object'){
    result.properties = handleProperties(source.properties, merge.properties);
  }else if(merge.type === 'array'){
    result.items = handleSchema(source.items, merge.items);
  }
  return result;
}

module.exports = function(sourceJsonSchema, mergeJsonSchema){
  return handleSchema(sourceJsonSchema, mergeJsonSchema)
}