-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from smog-root/updated
Enhance Mongoose Schemas: Improved Validation and Security #351
- Loading branch information
Showing
8 changed files
with
129 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters