Skip to content

Commit ea63289

Browse files
committed
CSHARP-1439: Add document validation support to create collection (v1.x).
1 parent 09551fe commit ea63289

File tree

6 files changed

+181
-2
lines changed

6 files changed

+181
-2
lines changed

MongoDB.Driver/Builders/CollectionOptionsBuilder.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ public static CollectionOptionsBuilder SetStorageEngineOptions(BsonDocument valu
8585
{
8686
return new CollectionOptionsBuilder().SetStorageEngineOptions(value);
8787
}
88+
89+
/// <summary>
90+
/// Sets the validation action.
91+
/// </summary>
92+
/// <param name="validationAction">The validation action.</param>
93+
/// <returns>The builder (so method calls can be chained).</returns>
94+
public static CollectionOptionsBuilder SetValidationAction(DocumentValidationAction validationAction)
95+
{
96+
return new CollectionOptionsBuilder().SetValidationAction(validationAction);
97+
}
98+
99+
/// <summary>
100+
/// Sets the validation level.
101+
/// </summary>
102+
/// <param name="validationLevel">The validation level.</param>
103+
/// <returns>The builder (so method calls can be chained).</returns>
104+
public static CollectionOptionsBuilder SetValidationLevel(DocumentValidationLevel validationLevel)
105+
{
106+
return new CollectionOptionsBuilder().SetValidationLevel(validationLevel);
107+
}
108+
109+
/// <summary>
110+
/// Sets the validator.
111+
/// </summary>
112+
/// <param name="validator">The validator.</param>
113+
/// <returns>The builder (so method calls can be chained).</returns>
114+
public static CollectionOptionsBuilder SetValidator(IMongoQuery validator)
115+
{
116+
return new CollectionOptionsBuilder().SetValidator(validator);
117+
}
88118
}
89119

90120
/// <summary>
@@ -161,6 +191,39 @@ public CollectionOptionsBuilder SetStorageEngineOptions(BsonDocument value)
161191
return this;
162192
}
163193

194+
/// <summary>
195+
/// Sets the validation action.
196+
/// </summary>
197+
/// <param name="validationAction">The validation action.</param>
198+
/// <returns>The builder (so method calls can be chained).</returns>
199+
public CollectionOptionsBuilder SetValidationAction(DocumentValidationAction validationAction)
200+
{
201+
_document["validationAction"] = validationAction.ToString().ToLowerInvariant();
202+
return this;
203+
}
204+
205+
/// <summary>
206+
/// Sets the validation level.
207+
/// </summary>
208+
/// <param name="validationLevel">The validation level.</param>
209+
/// <returns>The builder (so method calls can be chained).</returns>
210+
public CollectionOptionsBuilder SetValidationLevel(DocumentValidationLevel validationLevel)
211+
{
212+
_document["validationLevel"] = validationLevel.ToString().ToLowerInvariant();
213+
return this;
214+
}
215+
216+
/// <summary>
217+
/// Sets the validator.
218+
/// </summary>
219+
/// <param name="validator">The validator.</param>
220+
/// <returns>The builder (so method calls can be chained).</returns>
221+
public CollectionOptionsBuilder SetValidator(IMongoQuery validator)
222+
{
223+
_document["validator"] = validator.ToBsonDocument();
224+
return this;
225+
}
226+
164227
/// <summary>
165228
/// Returns the result of the builder as a BsonDocument.
166229
/// </summary>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
namespace MongoDB.Driver
17+
{
18+
/// <summary>
19+
/// Represents the document validation action.
20+
/// </summary>
21+
public enum DocumentValidationAction
22+
{
23+
/// <summary>
24+
/// Validation failures result in an error.
25+
/// </summary>
26+
Error = 0, // the default
27+
/// <summary>
28+
/// Validation failures result in a warning.
29+
/// </summary>
30+
Warn
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
namespace MongoDB.Driver
17+
{
18+
/// <summary>
19+
/// Represents the document validation level.
20+
/// </summary>
21+
public enum DocumentValidationLevel
22+
{
23+
/// <summary>
24+
/// Strict document validation.
25+
/// </summary>
26+
Strict = 0, // the default
27+
/// <summary>
28+
/// Moderate document validation.
29+
/// </summary>
30+
Moderate,
31+
/// <summary>
32+
/// No document validation.
33+
/// </summary>
34+
Off
35+
}
36+
}

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
<Compile Include="Communication\FeatureDetection\InstanceTypeDependency.cs" />
9191
<Compile Include="Communication\FeatureDetection\NotDependency.cs" />
9292
<Compile Include="Communication\Security\Mechanisms\ScramSha1Mechanism.cs" />
93+
<Compile Include="DocumentValidationAction.cs" />
94+
<Compile Include="DocumentValidationLevel.cs" />
9395
<Compile Include="Exceptions\MongoBulkWriteException.cs" />
9496
<Compile Include="Exceptions\MongoExecutionTimeoutException.cs" />
9597
<Compile Include="Exceptions\MongoWriteConcernException.cs" />

MongoDB.DriverUnitTests/Builders/CollectionOptionsBuilderTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using MongoDB.Bson;
17+
using MongoDB.Driver;
1718
using MongoDB.Driver.Builders;
1819
using NUnit.Framework;
1920

@@ -89,5 +90,29 @@ public void TestSetNone()
8990
var expected = "{ }".Replace("'", "\"");
9091
Assert.AreEqual(expected, options.ToJson());
9192
}
93+
94+
[Test]
95+
public void TestSetValidationAction()
96+
{
97+
var options = CollectionOptions.SetValidationAction(DocumentValidationAction.Error);
98+
var expected = "{ \"validationAction\" : \"error\" }";
99+
Assert.AreEqual(expected, options.ToJson());
100+
}
101+
102+
[Test]
103+
public void TestSetValidationLevel()
104+
{
105+
var options = CollectionOptions.SetValidationLevel(DocumentValidationLevel.Strict);
106+
var expected = "{ \"validationLevel\" : \"strict\" }";
107+
Assert.AreEqual(expected, options.ToJson());
108+
}
109+
110+
[Test]
111+
public void TestSetValidator()
112+
{
113+
var options = CollectionOptions.SetValidator(new QueryDocument("_id", new BsonDocument("$exists", true)));
114+
var expected = "{ \"validator\" : { \"_id\" : { \"$exists\" : true } } }";
115+
Assert.AreEqual(expected, options.ToJson());
116+
}
92117
}
93118
}

MongoDB.DriverUnitTests/MongoDatabaseTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -70,6 +70,27 @@ public void TestCreateCollection()
7070
Assert.IsTrue(_database.CollectionExists(collectionName));
7171
}
7272

73+
[Test]
74+
[RequiresServer(MinimumVersion = "3.1.9999")]
75+
public void TestCreateCollectionSetValidator()
76+
{
77+
var collection = _database.GetCollection("testvalidation");
78+
collection.Drop();
79+
Assert.IsFalse(collection.Exists());
80+
var options = CollectionOptions
81+
.SetValidator(new QueryDocument("_id", new BsonDocument("$exists", true)))
82+
.SetValidationAction(DocumentValidationAction.Error)
83+
.SetValidationLevel(DocumentValidationLevel.Strict);
84+
85+
_database.CreateCollection(collection.Name, options);
86+
87+
var commandResult = _database.RunCommand("listCollections");
88+
var collectionInfo = commandResult.Response["cursor"]["firstBatch"].AsBsonArray.Where(c => c["name"] == collection.Name).Single();
89+
Assert.That(collectionInfo["options"]["validator"], Is.EqualTo(new BsonDocument("_id", new BsonDocument("$exists", true))));
90+
Assert.That(collectionInfo["options"]["validationAction"].AsString, Is.EqualTo("error"));
91+
Assert.That(collectionInfo["options"]["validationLevel"].AsString, Is.EqualTo("strict"));
92+
}
93+
7394
[Test]
7495
public void TestDropCollection()
7596
{

0 commit comments

Comments
 (0)