You can also write this way...and this is for typescript code const userSchema = new mongoose.Schema({ username: String, email: String, password: String, firstname: String, lastname: String, mobile: { type: Number, validate: { validator: function (v: number) { return /d{10}/.test(v.toString()); }, message: "mobile number must be exactly 10 digits" }, }, });