mock平台

mergeJsonSchema.js 1.0KB

12345678910111213141516171819202122232425262728293031323334
  1. function isPlainObject(obj) {
  2. return obj ? typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype : false;
  3. }
  4. function handleProperties(sourceProperties, mergeProperties){
  5. if(!isPlainObject(mergeProperties)){
  6. return mergeProperties
  7. }
  8. if(! isPlainObject(sourceProperties)){
  9. return mergeProperties
  10. }
  11. Object.keys(mergeProperties).forEach(key=>{
  12. mergeProperties[key]= handleSchema(sourceProperties[key], mergeProperties[key])
  13. })
  14. return mergeProperties;
  15. }
  16. function handleSchema(source, merge){
  17. if(!isPlainObject(source)) return merge;
  18. if(!isPlainObject(merge)) return merge;
  19. let result = {}
  20. Object.assign(result, source, merge)
  21. if(merge.type === 'object'){
  22. result.properties = handleProperties(source.properties, merge.properties);
  23. }else if(merge.type === 'array'){
  24. result.items = handleSchema(source.items, merge.items);
  25. }
  26. return result;
  27. }
  28. module.exports = function(sourceJsonSchema, mergeJsonSchema){
  29. return handleSchema(sourceJsonSchema, mergeJsonSchema)
  30. }