Model.validate()
Parameters
- obj «Object»
- pathsToValidate «Array»
- [context] «Object»
- [callback] «Function»
Returns:
- «Promise,undefined»
Casts and validates the given object against this model’s schema, passing the given context
to custom validators.
Example:
const Model = mongoose.model('Test', Schema({
name: { type: String, required: true },
age: { type: Number, required: true }
});
try {
await Model.validate({ name: null }, ['name'])
} catch (err) {
err instanceof mongoose.Error.ValidationError; // true
Object.keys(err.errors); // ['name']
}
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .