Document.prototype.validateSync()

Parameters
  • pathsToValidate «Array|string» only validate the given paths

  • [options] «Object» options for validation

  • [options.validateModifiedOnly=false] «Boolean» If true, Mongoose will only validate modified paths, as opposed to modified paths and required paths.

Returns:
  • «ValidationError,undefined» ValidationError if there are errors during validation, or undefined if there is no error.

Executes registered validation rules (skipping asynchronous validators) for this document.

Note:

This method is useful if you need synchronous validation.

Example:

  1. const err = doc.validateSync();
  2. if (err) {
  3. handleError(err);
  4. } else {
  5. // validation passed
  6. }