function dataValidate(datalist, callback) {
  console.log('check unique fields');
  var dupFields = null;
  uniqueFields.forEach(function (f) {
    var values = [];
    var i;
    for (i = 0; i < datalist.length; i += 1) {
      if (values.indexOf(datalist[i][f]) === -1) {
        values.push(datalist[i][f]);
      } else {
        if (!dupFields) {
          dupFields = {};
        }
        if (!dupFields[f]) {
          dupFields[f] = [];
        }
        if (dupFields[f].indexOf(datalist[i][f]) === -1) {
          dupFields[f].push(datalist[i][f]);
        }
      }
    }
  });
  if (dupFields) {
    if (typeof callback === 'function') {
      return callback(new Error('Duplicate fields found: ' + JSON.stringify(dupFields)));
    } else {
      return console.error('Duplicate fields found: ' + JSON.stringify(dupFields));
    }
  } else {
    console.log('all values in unique fields are unique.');
  }

  console.log('Schema validation start.');
  var errs = [];
  var i;
  for (i = 0; i < datalist.length; i++) {
    var sobj = new Model(datalist[i]);
    var err = sobj.validateSync();
    if (err) {
      console.error('Validate failed for ' + sobj);
      console.error(err.errors);
      errs.push(err);
    }
  }

  if (errs.length > 0) {
    if (typeof callback === 'function') {
      return callback(new Error(errs.length + ' items are invalid.'));
    } else {
      return console.error(errs.length + ' items are invalid.');
    }
  } else {
    console.log('all data passed the schema validation.');
  }
  if (typeof callback === 'function') {
    return callback(null);
  }
}
/**
 * remove the duplicate entries and validate field format by schema.
 * @param oridatalist    json object list
 * @param callback
 */
remove the duplicate entries and validate field format by schema.
@param oridatalist    json object list
@param callback
remove the duplicate entries and validate field format by schema . @param oridatalist json object list @param callback
function dataValidate(datalist, callback) {
  console.log('check unique fields');
  var dupFields = null;
  uniqueFields.forEach(function (f) {
    var values = [];
    var i;
    for (i = 0; i < datalist.length; i += 1) {
      if (values.indexOf(datalist[i][f]) === -1) {
        values.push(datalist[i][f]);
      } else {
        if (!dupFields) {
          dupFields = {};
        }
        if (!dupFields[f]) {
          dupFields[f] = [];
        }
        if (dupFields[f].indexOf(datalist[i][f]) === -1) {
          dupFields[f].push(datalist[i][f]);
        }
      }
    }
  });
  if (dupFields) {
    if (typeof callback === 'function') {
      return callback(new Error('Duplicate fields found: ' + JSON.stringify(dupFields)));
    } else {
      return console.error('Duplicate fields found: ' + JSON.stringify(dupFields));
    }
  } else {
    console.log('all values in unique fields are unique.');
  }
  console.log('Schema validation start.');
  var errs = [];
  var i;
  for (i = 0; i < datalist.length; i++) {
    var sobj = new Model(datalist[i]);
    var err = sobj.validateSync();
    if (err) {
      console.error('Validate failed for ' + sobj);
      console.error(err.errors);
      errs.push(err);
    }
  }
  if (errs.length > 0) {
    if (typeof callback === 'function') {
      return callback(new Error(errs.length + ' items are invalid.'));
    } else {
      return console.error(errs.length + ' items are invalid.');
    }
  } else {
    console.log('all data passed the schema validation.');
  }
  if (typeof callback === 'function') {
    return callback(null);
  }
}
function dataValidate ( datalist , callback ) { console . log ( 'check unique fields' ) ; var dupFields = null ; uniqueFields . forEach ( function ( f ) { var values = [ ] ; var i ; for ( i = 0 ; i < datalist . length ; i += 1 ) { if ( values . indexOf ( datalist [ i ] [ f ] ) === - 1 ) { values . push ( datalist [ i ] [ f ] ) ; } else { if ( ! dupFields ) { dupFields = { } ; } if ( ! dupFields [ f ] ) { dupFields [ f ] = [ ] ; } if ( dupFields [ f ] . indexOf ( datalist [ i ] [ f ] ) === - 1 ) { dupFields [ f ] . push ( datalist [ i ] [ f ] ) ; } } } } ) ; if ( dupFields ) { if ( typeof callback === 'function' ) { return callback ( new Error ( 'Duplicate fields found: ' + JSON . stringify ( dupFields ) ) ) ; } else { return console . error ( 'Duplicate fields found: ' + JSON . stringify ( dupFields ) ) ; } } else { console . log ( 'all values in unique fields are unique.' ) ; } console . log ( 'Schema validation start.' ) ; var errs = [ ] ; var i ; for ( i = 0 ; i < datalist . length ; i ++ ) { var sobj = new Model ( datalist [ i ] ) ; var err = sobj . validateSync ( ) ; if ( err ) { console . error ( 'Validate failed for ' + sobj ) ; console . error ( err . errors ) ; errs . push ( err ) ; } } if ( errs . length > 0 ) { if ( typeof callback === 'function' ) { return callback ( new Error ( errs . length + ' items are invalid.' ) ) ; } else { return console . error ( errs . length + ' items are invalid.' ) ; } } else { console . log ( 'all data passed the schema validation.' ) ; } if ( typeof callback === 'function' ) { return callback ( null ) ; } }
remove the duplicate entries and validate field format by schema.
remove the duplicate entries and validate field format by schema .