lib/schema.js
function merge (schema, doc, self) {
  var keys = Object.keys(doc);

  for (var i = 0; i < keys.length; ++i) {
    var key = keys[i];
    var type;

    // if nothing in schema yet, just assign
    if ('undefined' == typeof schema[key]) {
      var type = self.getType(doc, key);
      if (undefined !== type) {
        schema[key] = type;
      }
      continue;
    }

    // exists in schema and incoming is an object?
    if (isObject(doc[key])) {
      if (isObject(schema[key])) {
        merge(schema[key], doc[key], self);

      } else {
        type = self.getType(doc, key);
        if (undefined !== type) {
          if ('null' == schema[key]) {
            schema[key] = type;
          } else if (type != schema[key]) {
            schema[key] = 'Mixed';
          }
        }
      }
    } else {
      // exists in schema and incoming is not an object.
      var type = self.getType(doc, key);
      if (undefined === type) {
        continue;
      }

      if ('null' == schema[key]) {
        schema[key] = type;

      } else if ('null' == type) {
        // ignore

      } else if (Array.isArray(schema[key]) && Array.isArray(type)) {

        if (0 === schema[key].length) {
          schema[key] = type;

        } else if (schema[key][0] instanceof Schema && isObject(type[0])) {
          // merge schemas
          schema[key][0].combine(type[0]);

        } else if (!equalType(schema[key], type)) {
          schema[key] = ['Mixed'];
        }

      } else if (!equalType(schema[key], type)) {
        schema[key] = 'Mixed';
      }
    }
  }
}
function merge (schema, doc, self) {
  var keys = Object.keys(doc);

  for (var i = 0; i < keys.length; ++i) {
    var key = keys[i];
    var type;

    // if nothing in schema yet, just assign
    if ('undefined' == typeof schema[key]) {
      var type = self.getType(doc, key);
      if (undefined !== type) {
        schema[key] = type;
      }
      continue;
    }

    // exists in schema and incoming is an object?
    if (isObject(doc[key])) {
      if (isObject(schema[key])) {
        merge(schema[key], doc[key], self);

      } else {
        type = self.getType(doc, key);
        if (undefined !== type) {
          if ('null' == schema[key]) {
            schema[key] = type;
          } else if (type != schema[key]) {
            schema[key] = 'Mixed';
          }
        }
      }
    } else {
      // exists in schema and incoming is not an object.
      var type = self.getType(doc, key);
      if (undefined === type) {
        continue;
      }

      if ('null' == schema[key]) {
        schema[key] = type;

      } else if ('null' == type) {
        // ignore

      } else if (Array.isArray(schema[key]) && Array.isArray(type)) {

        if (0 === schema[key].length) {
          schema[key] = type;

        } else if (schema[key][0] instanceof Schema && isObject(type[0])) {
          // merge schemas
          schema[key][0].combine(type[0]);

        } else if (!equalType(schema[key], type)) {
          schema[key] = ['Mixed'];
        }

      } else if (!equalType(schema[key], type)) {
        schema[key] = 'Mixed';
      }
    }
  }
}
function merge ( schema , doc , self ) { var keys = Object . keys ( doc ) ; for ( var i = 0 ; i < keys . length ; ++ i ) { var key = keys [ i ] ; var type ; // if nothing in schema yet, just assign if ( 'undefined' == typeof schema [ key ] ) { var type = self . getType ( doc , key ) ; if ( undefined !== type ) { schema [ key ] = type ; } continue ; } // exists in schema and incoming is an object? if ( isObject ( doc [ key ] ) ) { if ( isObject ( schema [ key ] ) ) { merge ( schema [ key ] , doc [ key ] , self ) ; } else { type = self . getType ( doc , key ) ; if ( undefined !== type ) { if ( 'null' == schema [ key ] ) { schema [ key ] = type ; } else if ( type != schema [ key ] ) { schema [ key ] = 'Mixed' ; } } } } else { // exists in schema and incoming is not an object. var type = self . getType ( doc , key ) ; if ( undefined === type ) { continue ; } if ( 'null' == schema [ key ] ) { schema [ key ] = type ; } else if ( 'null' == type ) { // ignore } else if ( Array . isArray ( schema [ key ] ) && Array . isArray ( type ) ) { if ( 0 === schema [ key ] . length ) { schema [ key ] = type ; } else if ( schema [ key ] [ 0 ] instanceof Schema && isObject ( type [ 0 ] ) ) { // merge schemas schema [ key ] [ 0 ] . combine ( type [ 0 ] ) ; } else if ( ! equalType ( schema [ key ] , type ) ) { schema [ key ] = [ 'Mixed' ] ; } } else if ( ! equalType ( schema [ key ] , type ) ) { schema [ key ] = 'Mixed' ; } } } }
Merge an object into schema
Merge an object into schema