Compare json props (only keys)

by ParanoidAndroid77

JavaScript

var valid = {
  data: {
    id: '',
    type: 'form',
    form: {
      ref: '',
      name: '',
      slug: '',
      type: 'hierarchy',
      inputs: []
    }
  }
};

var A = {
  data: {
    id: '',
    type: 'form',
    form: {
      ref: '',
      name: '',
      slug: '',
      type: 'hierarchy',
      inputs: []
    }
  }
};

var B = {
  data: {
    id: '',
    type: 'form',
    form: {
      type: 'hierarchy',
      ref: '',
      name: '',
      slug: '',
      inputs: []
    }
  }
};
var C = {
  data: {
    id: '',
    type: 'form',
    form: {
      type: 'hierarchy',
      refa: '', //error
      name: '',
      slug: '',
      inputs: []
    }
  }
};

var D = {
  data: {
    id: '',
    type: 'form',
    form: {
      ref: '',
      name: '',
      slug: '',
      type: 'hierarchy',
      inputs: []
    }
  },
  extra: ''
};

var E = {
  data: {
    id: '',
    type: 'form',
    form: {
      ref: '',
      slug: '',
      type: 'hierarchy',
      inputs: []
    }
  }
};

var F = {
  data: {
    id: '',
    type: 'form',
    form: {
      ref: '',
      name: '',
      slug: '',
      type: 'hierarchy',
      inputs: {}
    }
  }
};


var G = {};
var J = []; //array, not object





function hasSameProps(validSource, wannaBe, strict) {
		
    //false when wannabe property is an array but validSource is not
    if (typeof validSource === 'object' && Array.isArray(wannaBe)) {
      return false;
    }

    //check for wannaBe overlapping props
    if (!Object.keys(wannaBe).every(function(key) {
        return validSource.hasOwnProperty(key);
      })) {
      // console.log('wannaBe has extra keys')
      return false;
    }

    //check every key for being same
    return Object.keys(validSource).every(function(key) {
      //if object
      if (typeof(validSource[key]) === 'object' && typeof(wannaBe[key]) === 'object') {

        //check array is array
        if (Array.isArray(validSource[key])) {
          //console.log('must be array key: ' +...