You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know this is late but if anyone is facing this issue and needs a workaround you could use the custom validator to validate the array.
export const arrayValidator = (elementSchema: Schema) => {
return (val: any[]) => {
return val.every(element => {
const errors: any[] = elementSchema.validate(element, { strip: false });
if (errors && errors.length >= 1) {
throw new Error(errors.map(error => error.message).join(", "));
// I'm throwing the error here because since Im using nodejs with express
// I have an error handler that handles all the errors
// By doing so I could get the custom validation messages in the elementSchema
// to my error handler
}
return true;
});
};
};
const studies = new Schema({
label: { type: String, required: true }
});
const userSchema = new Schema({
studies: {
required: true,
type: Array,
use: { studiesValidator: arrayValidator(studies) }
// you might also want to add an empty array validator if required
}
});
Then later validate using
let user = {
studies: [{}]
}
const errors = userSchema.validate(user);
console.log(errors.map(error => error.message).join(", ")); // => label is required
var test= new Schema ({
test:[
{name:{type:String, required:true}}
]
})
Input:
test.validate({}) // throws no error
test.validate({vinod:{}}) // throws no error
test.validate({vinod:[]}) // throws no error
test.validate([]) // throws no error
I really like you library, but please give support for this It will be very helpful
Regards,
Vinod
The text was updated successfully, but these errors were encountered: