Skip to content

Mongo validation

Pradeeban Kathiravelu edited this page Jul 19, 2018 · 4 revisions

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.

ValidationAction = "error"

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

ValidationAction = "warn"

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

Clone this wiki locally