index.js
function modifyProperties(schema, paths) {
  if (!paths.length) { return; }

  const options = paths[0].options;

  /**
  * Modify document enum (string) properties to an object, with (original) value and
  * values (enumValues)
  *
  */
  function populatePropertyFor (documents, next) {
    if (this._mongooseOptions.lean) {
      asArray(documents).forEach(function(doc) {
        paths.forEach(function(path) {
          try {
            const splitted = path.path.split('.');
            const key      = splitted.shift();
            const insert   = { values: path.enumValues };
            const value    = doc[key];

            insert.value = determineValue(splitted, value);
            doc[key]     = nest(splitted, insert);
          } catch (error) { return next(error); }
        });
      });
    }

    next();
  }

  /**
  * If a document is modified, this method will locate the value on updates and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */
  function reformatUpdateProperty(next) {
    const document = this._update['$set'];

    if (document) {
      paths.forEach(function(path) {
        try {
          const splitted = path.path.split('.');
          const key      = splitted.shift();

          if (document[key] === undefined) { return; }

          const value   = determineValue(splitted, document[key]);

          document[key] = nest(splitted, value);
        } catch (error) { return next(error); }
      });
    }

    next();
  }

  /**
  * If a document is modified, this method will locate the value before save/validation and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */
  function reformatProperty(next) {
    const self = this;
    paths.forEach(function(path) {
      try {
        const splitted = path.path.split('.');
        const key      = splitted.shift();
        const value    = determineValue(splitted, self[key]);

        self[key]      = nest(splitted, value);
      } catch (error) { return next(error); }
    });

    next();
  }

  /**
  * Setup handlers for modifying properties -- ['find', 'findOne']
  */
  options.modify.on.forEach(function(on) {
    schema.post(on, populatePropertyFor);
  });

  schema.pre((options.validateBeforeSave ? 'validate' : 'save'), reformatProperty);

  /*
  * Setup handlers for updating documents (there may be more to consider)
  */
  ['update', 'findOneAndUpdate'].forEach(function(on) {
    schema.pre(on, reformatUpdateProperty);
  });
}
function modifyProperties(schema, paths) {
  if (!paths.length) { return; }

  const options = paths[0].options;

  /**
  * Modify document enum (string) properties to an object, with (original) value and
  * values (enumValues)
  *
  */
  function populatePropertyFor (documents, next) {
    if (this._mongooseOptions.lean) {
      asArray(documents).forEach(function(doc) {
        paths.forEach(function(path) {
          try {
            const splitted = path.path.split('.');
            const key      = splitted.shift();
            const insert   = { values: path.enumValues };
            const value    = doc[key];

            insert.value = determineValue(splitted, value);
            doc[key]     = nest(splitted, insert);
          } catch (error) { return next(error); }
        });
      });
    }

    next();
  }

  /**
  * If a document is modified, this method will locate the value on updates and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */
  function reformatUpdateProperty(next) {
    const document = this._update['$set'];

    if (document) {
      paths.forEach(function(path) {
        try {
          const splitted = path.path.split('.');
          const key      = splitted.shift();

          if (document[key] === undefined) { return; }

          const value   = determineValue(splitted, document[key]);

          document[key] = nest(splitted, value);
        } catch (error) { return next(error); }
      });
    }

    next();
  }

  /**
  * If a document is modified, this method will locate the value before save/validation and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */
  function reformatProperty(next) {
    const self = this;
    paths.forEach(function(path) {
      try {
        const splitted = path.path.split('.');
        const key      = splitted.shift();
        const value    = determineValue(splitted, self[key]);

        self[key]      = nest(splitted, value);
      } catch (error) { return next(error); }
    });

    next();
  }

  /**
  * Setup handlers for modifying properties -- ['find', 'findOne']
  */
  options.modify.on.forEach(function(on) {
    schema.post(on, populatePropertyFor);
  });

  schema.pre((options.validateBeforeSave ? 'validate' : 'save'), reformatProperty);

  /*
  * Setup handlers for updating documents (there may be more to consider)
  */
  ['update', 'findOneAndUpdate'].forEach(function(on) {
    schema.pre(on, reformatUpdateProperty);
  });
}
function modifyProperties ( schema , paths ) { if ( ! paths . length ) { return ; } const options = paths [ 0 ] . options ; /**
  * Modify document enum (string) properties to an object, with (original) value and
  * values (enumValues)
  *
  */ function populatePropertyFor ( documents , next ) { if ( this . _mongooseOptions . lean ) { asArray ( documents ) . forEach ( function ( doc ) { paths . forEach ( function ( path ) { try { const splitted = path . path . split ( '.' ) ; const key = splitted . shift ( ) ; const insert = { values : path . enumValues } ; const value = doc [ key ] ; insert . value = determineValue ( splitted , value ) ; doc [ key ] = nest ( splitted , insert ) ; } catch ( error ) { return next ( error ) ; } } ) ; } ) ; } next ( ) ; } /**
  * If a document is modified, this method will locate the value on updates and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */ function reformatUpdateProperty ( next ) { const document = this . _update [ '$set' ] ; if ( document ) { paths . forEach ( function ( path ) { try { const splitted = path . path . split ( '.' ) ; const key = splitted . shift ( ) ; if ( document [ key ] === undefined ) { return ; } const value = determineValue ( splitted , document [ key ] ) ; document [ key ] = nest ( splitted , value ) ; } catch ( error ) { return next ( error ) ; } } ) ; } next ( ) ; } /**
  * If a document is modified, this method will locate the value before save/validation and assign it to the
  * appropriate property, allowing for proper validations later.
  * @param <Function>: next - function that notifies mongoose this middleware is complete
  */ function reformatProperty ( next ) { const self = this ; paths . forEach ( function ( path ) { try { const splitted = path . path . split ( '.' ) ; const key = splitted . shift ( ) ; const value = determineValue ( splitted , self [ key ] ) ; self [ key ] = nest ( splitted , value ) ; } catch ( error ) { return next ( error ) ; } } ) ; next ( ) ; } /**
  * Setup handlers for modifying properties -- ['find', 'findOne']
  */ options . modify . on . forEach ( function ( on ) { schema . post ( on , populatePropertyFor ) ; } ) ; schema . pre ( ( options . validateBeforeSave ? 'validate' : 'save' ) , reformatProperty ) ; /*
  * Setup handlers for updating documents (there may be more to consider)
  */ [ 'update' , 'findOneAndUpdate' ] . forEach ( function ( on ) { schema . pre ( on , reformatUpdateProperty ) ; } ) ; }
Setup handlers for modifying document properties, must be a `lean` object
Setup handlers for modifying document properties must be a lean object