I\'m trying to validate some data that will be inserted into a new document, but not before a lot of other things need to happen. So I was going to add a function to the static
As explained here in the mongoose documents https://mongoosejs.com/docs/validation.html , you can use doc.validate(callback)
or doc.validateSync()
to check for validation.
The difference is that you do not have to use await
for validateSync()
as its name suggests. it returns an error if validation fails, otherwise, it returns undefined
.
for example:
const model = new Model({somedata:somedata})
const validatedModel = model.validateSync()
if(!!validatedModel) throw validatedModel