I'm working on a project and need an array value, when entered, to have an array length of at least 12. I've read through some of the documentation on their site and I haven't found anything yet so I thought it wouldn't hurt to post here while I continue searching.
This is an example of the code.
const schema = new mongoose.Schema({
Array: {
type: Array,
required: true,
}
});
Any insights you have would be greatly appreciated! If there's something I could clarify better please let me know!
CodePudding user response:
You can use validate()
from mongoose while defining your schema.
const schema = new mongoose.Schema({
Array: {
type: Array,
required: true,
validate : lengthMin12
}
});
const lengthMin12 = (val) => {
return val.length >= 12;
}
PS : The above link is from the latest version, but validate is available for older mongoose versions too.