This repository was archived by the owner on Mar 9, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +56
-14
lines changed
Expand file tree Collapse file tree 4 files changed +56
-14
lines changed Original file line number Diff line number Diff line change 11const mongoose = require ( 'mongoose' )
22const { Schema } = mongoose
33
4+ const {
5+ linkType
6+ } = require ( './validators' )
7+
48const 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 ] ,
Original file line number Diff line number Diff line change 11const mongoose = require ( 'mongoose' )
22const { Schema } = mongoose
33
4+ const { linkType } = require ( './validators' )
5+
46const 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 : {
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 = {
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 }
You can’t perform that action at this time.
0 commit comments