@@ -54,14 +54,25 @@ func ReadConcern(readConcern *readconcern.ReadConcern) MongoOption {
54
54
// If the option is not specified the default is: [writeconcern.Majority].
55
55
func WriteConcern (writeConcern * writeconcern.WriteConcern ) MongoOption {
56
56
return func (opts * options.ClientOptions ) error {
57
- if writeConcern = = nil {
58
- return errors . New ( "WriteConcern not specified" )
57
+ if err := validateWriteConcern ( writeConcern ); err ! = nil {
58
+ return err
59
59
}
60
+
60
61
opts .SetWriteConcern (writeConcern )
61
62
return nil
62
63
}
63
64
}
64
65
66
+ func validateWriteConcern (writeConcern * writeconcern.WriteConcern ) error {
67
+ if writeConcern == nil {
68
+ return errors .New ("WriteConcern not specified" )
69
+ }
70
+ if w , ok := writeConcern .W .(int ); ok && w == 0 {
71
+ return errors .New ("WriteConcern without acknowledgment is not allowed (w: 0)" )
72
+ }
73
+ return nil
74
+ }
75
+
65
76
// NoRS option removes replica set name setting
66
77
func NoRS () MongoOption {
67
78
return func (opts * options.ClientOptions ) error {
@@ -102,6 +113,9 @@ func MongoConnectWithOpts(ctx context.Context,
102
113
103
114
// apply and override using end-user options from conn string
104
115
mopts .ApplyURI (uri )
116
+ if err := validateConnStringOpts (mopts ); err != nil {
117
+ return nil , nil , errors .Wrap (err , "invalid connection string option" )
118
+ }
105
119
106
120
// override with explicit options from the code
107
121
for _ , opt := range mongoOptions {
@@ -125,6 +139,18 @@ func MongoConnectWithOpts(ctx context.Context,
125
139
return conn , mopts , nil
126
140
}
127
141
142
+ func validateConnStringOpts (opts * options.ClientOptions ) error {
143
+ var err error
144
+ if err = opts .Validate (); err != nil {
145
+ return err
146
+ }
147
+ if err = validateWriteConcern (opts .WriteConcern ); err != nil {
148
+ return err
149
+ }
150
+
151
+ return nil
152
+ }
153
+
128
154
func MongoConnect (
129
155
ctx context.Context ,
130
156
uri string ,
0 commit comments