Skip to content

Commit

Permalink
Merge pull request #351 from smog-root/updated
Browse files Browse the repository at this point in the history
Enhance Mongoose Schemas: Improved Validation and Security #351
  • Loading branch information
Anuj3553 authored Nov 4, 2024
2 parents d8adb5e + 38a2f11 commit 2dcfbf8
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 61 deletions.
14 changes: 9 additions & 5 deletions server/Models/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const mongoose = require('mongoose')
const mongoose = require('mongoose');

const AvatarSchema = new mongoose.Schema({
// user is a foreign key
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
required: true, // Ensure this is required if each avatar must be linked to a user
},
image: String
})
imageUrl: {
type: String,
required: true,
match: [/^(https?:\/\/.*\.(?:png|jpg|jpeg|gif|svg))$/, 'Please enter a valid image URL']
}
}, { timestamps: true });

module.exports = mongoose.model("avatar", AvatarSchema)
module.exports = mongoose.model("avatar", AvatarSchema);
14 changes: 11 additions & 3 deletions server/Models/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const contactFormSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
trim: true,
minlength: 2,
maxlength: 50
},
email: {
type: String,
Expand All @@ -17,15 +19,21 @@ const contactFormSchema = new mongoose.Schema({
msg: {
type: String,
required: true,
trim: true
trim: true,
minlength: 10,
maxlength: 500
},
createdAt: {
type: Date,
default: Date.now
},
ipAddress: {
type: String,
required: false
}
});

// Create the model from the schema
const ContactForm = mongoose.model('ContactForm', contactFormSchema);

module.exports = ContactForm;
module.exports = ContactForm;
16 changes: 15 additions & 1 deletion server/Models/Newsletter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
const mongoose = require("mongoose");

const NewsletterSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
email: {
type: String,
required: true,
unique: true,
match: [/\S+@\S+\.\S+/, 'Please enter a valid email address']
},
subscribedAt: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: ['Subscribed', 'Unsubscribed'],
default: 'Subscribed'
}
});

module.exports = mongoose.model("Newsletter", NewsletterSchema);
22 changes: 16 additions & 6 deletions server/Models/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,54 @@ const { Schema } = mongoose;

const ProfileSchema = new Schema(
{
// user is a foreign key
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
required: true,
},
image: {
image: String,
imageUrl: {
type: String,
trim: true,
},
name: {
type: String,
required: true,
trim: true,
},
email: {
type: String,

unique: true,
required: true,
match: [/\S+@\S+\.\S+/, "Please enter a valid email address"],
},
address: {
type: String,
trim: true,
},
college: {
type: String,
trim: true,
},
phone: {
type: Number,
type: String,
trim: true,
match: [/^\+?[0-9]{10,15}$/, "Enter a valid phone number"],
},
github: {
type: String,
match: [/^https:\/\/github.com\/.+/, "Enter a valid GitHub URL"],
},
linkedin: {
type: String,
match: [/^https:\/\/linkedin.com\/in\/.+/, "Enter a valid LinkedIn URL"],
},
password: {
type: String,
required: true,
minlength: 8,
},
},
{ timestamps: true }
);

module.exports = mongoose.model("profile", ProfileSchema);
module.exports = mongoose.model("profile", ProfileSchema);
18 changes: 13 additions & 5 deletions server/Models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ const mongoose = require("mongoose");
const { Schema } = mongoose;

const ProjectSchema = new Schema({
// user is a foreign key
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
image: {
image: String,
imageUrl: {
type: String,
},
title: {
type: String,
Expand All @@ -17,17 +16,26 @@ const ProjectSchema = new Schema({
},
description: {
type: String,
maxlength: 500, // Optional limit
},
gitHubLink: {
type: String,
match: [/^https:\/\/github.com\/.+/, 'Enter a valid GitHub URL'],
},
youTubeLink: {
type: String,
match: [/^https:\/\/(www\.)?youtube.com\/watch\?v=.+/, 'Enter a valid YouTube video URL'],
},
date: {
type: Date,
default: Date.now
default: Date.now,
required: true,
},
status: {
type: String,
enum: ['In Progress', 'Completed', 'On Hold'],
default: 'In Progress',
}
});

module.exports = mongoose.model('projects', ProjectSchema);
module.exports = mongoose.model('projects', ProjectSchema);
68 changes: 39 additions & 29 deletions server/Models/User.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const UserSchema = new Schema({
image: {
type: String,
required: true
const UserSchema = new Schema(
{
imageUrl: {
type: String,
required: true
},
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true,
match: [/\S+@\S+\.\S+/, 'Please enter a valid email address'],
},
password: {
type: String,
required: true,
minlength: 8 // Minimum length for password
},
date: {
type: Date,
default: Date.now
},
verificationToken: {
type: String,
},
verified: {
type: Boolean,
default: false
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user',
}
},
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
verificationToken: {
type: String,
},
verified: {
type: Boolean,
default: false
}
});
{ timestamps: true }
);

const User = mongoose.model('user', UserSchema);
module.exports = User;
26 changes: 18 additions & 8 deletions server/Models/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const blogSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
trim: true,
minlength: 5
},
author: {
type: String,
Expand All @@ -17,17 +18,26 @@ const blogSchema = new mongoose.Schema({
},
content: {
type: String,
required: true
},
id: {
type: String,
unique: true // Optional: Ensure this is unique
required: true,
minlength: 50
},
category: {
type: String,
required: true,
trim: true
}
trim: true,
enum: ["Tech", "Health", "Finance"] // Example categories
},
tags: {
type: [String],
default: []
},
comments: [
{
user: { type: String, required: true },
comment: { type: String, required: true },
date: { type: Date, default: Date.now }
}
]
});

const Blog = mongoose.model('Blog', blogSchema);
Expand Down
12 changes: 8 additions & 4 deletions server/Models/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,38 @@ const feedbackSchema = new mongoose.Schema({
type: String,
required: true,
trim: true,
match: [/\S+@\S+\.\S+/, 'Email is invalid'], // Basic email validation
match: [/\S+@\S+\.\S+/, 'Email is invalid'],
},
subject: {
type: String,
required: true,
trim: true,
},
dateOfVisit: {
type: String,
type: Date,
required: true,
},
deviceUsed: {
type: String,
required: true,
enum: ['Desktop', 'Mobile', 'Tablet'],
},
priorityLevel: {
type: String,
required: true,
enum: ['Low', 'Medium', 'High'],
},
suggestions: {
type: String,
trim: true,
required: true,
maxlength: 500,
},
feedback: {
type: String,
trim: true,
required: true,
maxlength: 1000,
},
rating: {
type: Number,
Expand All @@ -48,11 +52,11 @@ const feedbackSchema = new mongoose.Schema({
},
createdAt: {
type: Date,
default: Date.now, // Automatically set the created date
default: Date.now,
}
});

// Create the model from the schema
const Feedback = mongoose.model('Feedback', feedbackSchema);

module.exports = Feedback;
module.exports = Feedback;

0 comments on commit 2dcfbf8

Please sign in to comment.