This repository was archived by the owner on Mar 9, 2021. It is now read-only.
File tree 4 files changed +56
-14
lines changed
4 files changed +56
-14
lines changed Original file line number Diff line number Diff line change 1
1
const mongoose = require ( 'mongoose' )
2
2
const { Schema } = mongoose
3
3
4
+ const {
5
+ linkType
6
+ } = require ( './validators' )
7
+
4
8
const UserSchema = new Schema ( {
5
9
username : {
6
10
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
+ }
8
19
} ,
9
20
email : {
10
21
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!' ]
12
29
} ,
13
30
password : {
14
31
type : String ,
15
- required : true ,
32
+ required : [ true , 'password is required!' ] ,
16
33
minlength : 10
17
34
} ,
18
35
mobile : {
@@ -27,14 +44,14 @@ const UserSchema = new Schema({
27
44
working : Boolean ,
28
45
forhire : Boolean ,
29
46
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 ,
38
55
discord : String
39
56
} ,
40
57
techFamiliarWith : [ String ] ,
Original file line number Diff line number Diff line change 1
1
const mongoose = require ( 'mongoose' )
2
2
const { Schema } = mongoose
3
3
4
+ const { linkType } = require ( './validators' )
5
+
4
6
const UserProjectSchema = new Schema ( {
5
7
name : {
6
8
type : String ,
@@ -15,15 +17,15 @@ const UserProjectSchema = new Schema({
15
17
maxlength : 1000
16
18
} ,
17
19
thumbnailUrl : {
18
- type : String ,
20
+ ... linkType ,
19
21
default : 'Some url to default thumbnail'
20
22
} ,
21
23
creators : {
22
24
type : [ Schema . Types . ObjectId ] ,
23
25
required : true
24
26
} ,
25
27
link : {
26
- type : String ,
28
+ ... linkType ,
27
29
required : true
28
30
} ,
29
31
hits : {
Original file line number Diff line number Diff line change
1
+ function linkValidator ( value ) {
2
+ return / h t t p s ? : \/ \/ ( 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
+ }
Original file line number Diff line number Diff line change @@ -38,7 +38,14 @@ const resolvers = {
38
38
throw new Error ( `A user with the username ${ username } already exists` )
39
39
}
40
40
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 ( )
42
49
} catch ( e ) {
43
50
throw e
44
51
}
You can’t perform that action at this time.
0 commit comments