Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validateBeforeSave.js 901B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. module.exports = function(schema) {
  6. const unshift = true;
  7. schema.pre('save', false, function(next, options) {
  8. const _this = this;
  9. // Nested docs have their own presave
  10. if (this.ownerDocument) {
  11. return next();
  12. }
  13. const hasValidateBeforeSaveOption = options &&
  14. (typeof options === 'object') &&
  15. ('validateBeforeSave' in options);
  16. let shouldValidate;
  17. if (hasValidateBeforeSaveOption) {
  18. shouldValidate = !!options.validateBeforeSave;
  19. } else {
  20. shouldValidate = this.schema.options.validateBeforeSave;
  21. }
  22. // Validate
  23. if (shouldValidate) {
  24. this.validate(function(error) {
  25. return _this.schema.s.hooks.execPost('save:error', _this, [ _this], { error: error }, function(error) {
  26. next(error);
  27. });
  28. });
  29. } else {
  30. next();
  31. }
  32. }, null, unshift);
  33. };