Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit 1b72d66

Browse files
committed
Implement validators for users and projects
1 parent ee15717 commit 1b72d66

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

models/User.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
const mongoose = require('mongoose')
22
const { Schema } = mongoose
33

4+
const {
5+
linkType
6+
} = require('./validators')
7+
48
const UserSchema = new Schema({
59
username: {
610
type: String,
7-
required: true
11+
required: [true, 'username is required!'],
12+
unique: true,
13+
validate: {
14+
validator: async (v) => {
15+
return /^[\w\d]+$/ig.test(v)
16+
},
17+
message: props => `${props.value} is not a valid username!`
18+
}
819
},
920
email: {
1021
type: String,
11-
required: true
22+
validate: {
23+
validator: async (v) => {
24+
return /^[\d\w]+@[\d\w]+\.[\w]+$/ig.test(v)
25+
},
26+
message: props => `${props.value} is not a valid email address!`
27+
},
28+
required: [true, 'email is required!']
1229
},
1330
password: {
1431
type: String,
15-
required: true,
32+
required: [true, 'password is required!'],
1633
minlength: 10
1734
},
1835
mobile: {
@@ -27,14 +44,14 @@ const UserSchema = new Schema({
2744
working: Boolean,
2845
forhire: Boolean,
2946
links: {
30-
facebook: String,
31-
twitter: String,
32-
linkedin: String,
33-
angellist: String,
34-
stackoverflow: String,
35-
codepen: String,
36-
github: String,
37-
behance: String,
47+
facebook: linkType,
48+
twitter: linkType,
49+
linkedin: linkType,
50+
angellist: linkType,
51+
stackoverflow: linkType,
52+
codepen: linkType,
53+
github: linkType,
54+
behance: linkType,
3855
discord: String
3956
},
4057
techFamiliarWith: [String],

models/UserProject.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const mongoose = require('mongoose')
22
const { Schema } = mongoose
33

4+
const { linkType } = require('./validators')
5+
46
const UserProjectSchema = new Schema({
57
name: {
68
type: String,
@@ -15,15 +17,15 @@ const UserProjectSchema = new Schema({
1517
maxlength: 1000
1618
},
1719
thumbnailUrl: {
18-
type: String,
20+
...linkType,
1921
default: 'Some url to default thumbnail'
2022
},
2123
creators: {
2224
type: [Schema.Types.ObjectId],
2325
required: true
2426
},
2527
link: {
26-
type: String,
28+
...linkType,
2729
required: true
2830
},
2931
hits: {

models/validators.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function linkValidator (value) {
2+
return /https?:\/\/(w{3}\.)?[\w\d]+\..+/.test(value)
3+
}
4+
5+
const linkType = {
6+
type: String,
7+
validate: {
8+
validator: linkValidator,
9+
message: 'please provide a valid link'
10+
}
11+
}
12+
13+
module.exports = {
14+
linkValidator,
15+
linkType
16+
}

resolvers/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ const resolvers = {
3838
throw new Error(`A user with the username ${username} already exists`)
3939
}
4040

41-
return new User(user).save()
41+
const newUser = new User(user)
42+
const isInvalid = await newUser.validate()
43+
44+
if (isInvalid) {
45+
throw isInvalid
46+
}
47+
48+
return newUser.save()
4249
} catch (e) {
4350
throw e
4451
}

0 commit comments

Comments
 (0)