In my express + mongo application, I am using mongoose for database schema.
For the schema, I need a custom validator say "for checking whether length is x"
Below is my schema containing that custom validator
var schema = new Schema({ ..., contactNumber: { type: Number, validate: [ { validator: hasLength, message: "Contact number must be 10 digits", length: 10 } ] }, ... } And below is the custom validator function:
function hasLength(str, length) { let pattern = "^d{" + length + "}$"; let regex = new RegExp(pattern, 'g'); return regex.test(str); } In this validator function, I want to access the length parameter from the above schema.
I know this function code will not work properly. So I need help from someone that how can I access that length variable in my validator function.
Your help will be highly appreciated :)