I want to verify some data that will be embedded in a new document but not before as many other things need to occur. So I was thinking to add a function to the static methods that would hopefully validate objects in an array against the model schema.
Here is the code:
module.exports = Mongoose => {
const Schema = Mongoose.Schema
const peopleSchema = new Schema({
name: {
type: Schema.Types.String,
required: true,
minlength: 3,
maxlength: 25
},
age: Schema.Types.Number
})
/**
* Validate the settings of an array of people
*
* @param {array} people Array of people (objects)
* @return {boolean}
*/
peopleSchema.statics.validatePeople = function( people ) {
return _.every(people, p => {
/**
* How can I validate the object `p` against the peopleSchema
*/
})
}
return Mongoose.model( 'People', peopleSchema )
}
So the peopleSchema.statics.validatePeople is where I'm attempting to do the validation. I have gone through mongooses validation documents, but it doesn't seem to state how to authenticate against a model without saving the data.