I was studying Mongoose schemas and there is a thing I can not get it or maybe is not possible.
var schemaClient = new mongoose.Schema({
Name:{
type:String,
required:[true,'The name is obligatory'],
maxLength:[10,'Max 10 characters'],
match:[new RegExp('^[a-zA-Z]+$'),'only letters without white space.']
}
})
The attribute name have three validators [required,maxLength,match]
if for example I create a Client like that:
var client={Name:'asdasd asdas asdasd asdd'}
here there are two errors: [maxLength,match]
However mongoose always stop validate when throw the first error, in this case is the maxLength validator error.
var modelClient = mongoose.model('Client',schemaClient);
try{
modelClient.validate(Client)
}catch(error){
console.log(error.errors);
}
But I am interested to throw all possible errors for the attribute Name.
I mean....
the attribute name have the error [maxLength,match]....and I want to catch the error message of maxLength validator and the error message of match validator.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/TvU56LY
Comments
Post a Comment