From 35817bc4cb704b4b4c4e1971c6bef3554659bacd Mon Sep 17 00:00:00 2001 From: siddharth shukla Date: Wed, 18 Jun 2025 22:04:59 +0530 Subject: [PATCH] updated validation in student.js inside models folder and also some changes in routes.js --- express-mongoose/src/models/students.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/express-mongoose/src/models/students.js b/express-mongoose/src/models/students.js index 0219e82..397b57c 100644 --- a/express-mongoose/src/models/students.js +++ b/express-mongoose/src/models/students.js @@ -1,5 +1,5 @@ -const mongoose = require('mongoose') -const validator = require('validator') +const mongoose = require('mongoose'); +const validator = require('validator'); const studSchema = mongoose.Schema({ name: { @@ -9,21 +9,24 @@ const studSchema = mongoose.Schema({ email: { type: String, required: true, - unique: [true, "Email already exists"], + unique: true, validate(value) { if(!validator.isEmail(value)) { - throw new Error("Invalid email") + throw new Error("Invalid email"); } } }, phone: { - type: Number, + type: String, required: true, unique: true, - minLength:10, - maxLength:10 + validate(value) { + if(value.length !== 10 || !/^\d+$/.test(value)) { + throw new Error("Phone number must be exactly 10 digits"); + } + } } -}) +}); const Student = mongoose.model('Student', studSchema);