-
Notifications
You must be signed in to change notification settings - Fork 11
Mongo validation
In this release, we have upgraded the CSV validation to the latest version of OpenCSV, and exploited the validation feature available since Mongo 3.6 for JSON validation.
Mongo/POST with CSV has its own validation supported by OpenCSV. The entries must match the CSV headers. However, CSV does not support complex objects. Therefore the potential, as well as the challenges, come from the JSON inputs. We use Mongo's own validation for this. Mongo offers this feature starting from its 3.6 release. We can use it, since we have updated the mongo driver and Mongo database in this release.
More details on this can be found from Mongo documentation.
A validation action can be defined as "error" or "warn". Error fails a query when it does not adhere to the schema. On the other hand, warn just returns a warning message if the query does not fit the format and still successfully executes the query. In the context of JSON validation for Bindaas Mongo provider, we are more interested in the Error, which is described first below.
db.createCollection( "contacts3", { validator: {
$jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb.com$ ", description: "must be a string and match the regular expression pattern" }, status: { enum: [ "Unknown", "Incomplete" ], description: "can only be one of the enum values" } } } }, validationAction: "error" } )
Some sample inputs, and the respective messages (HTTP code 200) shown in the Bindaas admin console.
{name: "Amanda", status: "Updated"} <--- Output: "Document failed validation" //Fail.
{name: "Aman", status: "Updated", phone: "22222"} <--- Output: "Document failed validation" //Fail.
{name: "Paba", status: "Incomplete", phone: "333"} <---- output: "{ "count" : 1}" // Succeed.
With csv: phone
"Pabas", "Incomplete", "333" "Pabass", "Incomplete", "333" <--- Output: "Document failed validation" //Fail.
With csv: phone
"11111" "22222" <---- output: "{ "count" : 2}" // Succeed
db.createCollection( "contacts2", { validator: {
$jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb.com$ ", description: "must be a string and match the regular expression pattern" }, status: { enum: [ "Unknown", "Incomplete" ], description: "can only be one of the enum values" } } } }, validationAction: "warn" } )
csv: name, status, phone
"Pabas", "Incomplete", "333"
"Pabass", "Incomplete", "333" <---- output: "{ "count" : 2}" // Succeed
Please note that for the JSON validation to be successful, the schema needs to be defined at the time when the collection was created. Bindaas currently does not provide a mechanism to impose validation to an existing collection that was created without such constructs in place. We have tested a json-schema-validator to support such validations into existing collections. However, this is not implemented in Bindaas, as we did not find the usecase convincing or important for Bindaas Mongo query, to depend on this yet another library.
Note: Avoid using CSV when you are using POST on an existing collection configured with validation. It will lead to accepting only the inputs that conform to both the schema that was stored at the creation time of the collection, and the CSV headers at the time of the POST action. This is most likely not the expected outcome.