Skip to content

Commit 9e9eb2d

Browse files
committed
VK feedback
1 parent f7c3c23 commit 9e9eb2d

File tree

7 files changed

+233
-157
lines changed

7 files changed

+233
-157
lines changed

source/includes/integrations/mongoose-blogSchema-validate.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const blogSchema = new Schema({
1010
slug: {
1111
type: String,
1212
required: true,
13-
lowercase: true,
13+
minLength: 4,
1414
},
1515
published: {
1616
type: Boolean,
@@ -22,17 +22,13 @@ const blogSchema = new Schema({
2222
},
2323
content: String,
2424
tags: [String],
25-
createdAt: {
26-
type: Date,
27-
default: () => Date.now(),
28-
immutable: true,
29-
},
30-
updated: Date,
3125
comments: [{
3226
user: String,
3327
content: String,
3428
votes: Number
3529
}]
30+
}, {
31+
timestamps: true
3632
});
3733
// end-blogSchema
3834

source/includes/integrations/mongoose-blogSchema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const blogSchema = new Schema({
88
author: String,
99
content: String,
1010
tags: [String],
11-
createdAt: Date,
12-
updated: Date,
1311
comments: [{
1412
user: String,
1513
content: String,
1614
votes: Number
1715
}]
16+
}, {
17+
timestamps: true
1818
});
1919

2020
const Blog = model('Blog', blogSchema);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import mongoose from 'mongoose';
2+
import User from './model/User.js';
3+
// start-user-import
4+
import User from './model/User.js';
5+
// end-user-import
6+
7+
mongoose.connect("<connection string>");
8+
9+
// start-create-user-bad-email
10+
const user = await User.create({
11+
name: 'Jess Garica',
12+
email: 'jgarciaemail.com',
13+
});
14+
// end-create-user-bad-email
15+
16+
// start-create-user-ok-email
17+
const user = await User.create({
18+
name: 'Jess Garica',
19+
email: 'jgarcia@email.com',
20+
});
21+
// end-create-user-ok-email
22+

source/includes/integrations/mongoose-schema-example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const blog = new Schema({
88
author: String,
99
content: String,
1010
tags: [String],
11-
createdAt: Date,
12-
updated: Date,
1311
comments: [{
1412
user: String,
1513
content: String,
1614
votes: Number
1715
}]
16+
}, {
17+
timestamps: true
1818
});
1919
// end-schema-example
2020
const Blog = model('Blog', blog);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import mongoose from 'mongoose';
2+
// start-validate-import
3+
import validator from 'validator';
4+
// end-validate-import
5+
const {Schema, model} = mongoose;
6+
7+
const userSchema = new Schema({
8+
name: {
9+
type: String,
10+
required: true,
11+
},
12+
email: {
13+
type: String,
14+
minLength: 10,
15+
required: true,
16+
lowercase: true
17+
},
18+
});
19+
20+
// start-middleware-definition
21+
userSchema.pre('save', function (next) {
22+
const user = this;
23+
24+
// Normalize email
25+
if (user.email) {
26+
user.email = user.email.trim().toLowerCase();
27+
}
28+
29+
// Validate email format
30+
if (!validator.isEmail(user.email)) {
31+
return next(new Error('Invalid email format'));
32+
}
33+
34+
next();
35+
});
36+
// end-middleware-definition
37+
38+
const User = model('User', userSchema);
39+
export default User;

0 commit comments

Comments
 (0)