JsonSchema for mongodb purposes looks like this:
package main
import (
"context"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
jsonSchema := bson.M{
"bsonType": "object",
"required": []string{"name", "age"},
"properties": bson.M{
"name": bson.M{
"bsonType": "string",
"minLength": 1, // Minimum length of 1 character
"maxLength": 50, // Maximum length of 50 characters
"description": "must be a string with 1 to 50 characters and is required",
},
"age": bson.M{
"bsonType": "int",
"minimum": 0,
"description": "must be an integer greater than or equal to 0 and is required",
},
},
}
validationOptions := options.CreateCollection().SetValidator(bson.M{"$jsonSchema": jsonSchema})
err = db.CreateCollection(context.TODO(), "your_collection_name", validationOptions)
collection := db.Collection("your_collection_name")
newPerson := Person{Name: "John Doe", Age: 30} // NUM #1
insertResult, err := collection.InsertOne(context.Background(), newPerson)
// or we could do this instead:
newPerson := bson.D{{"name", "John Doe"}, {"age", 30}} // NUM #2
insertResult, err := collection.InsertOne(context.Background(), newPerson)
}
my question is - is there a library/technique that can validate the person struct (#1) or the bson.M person map (#2) against the jsonSchema map definition before inserting to that db? That way we can fail early at the application level and know that the struct/bson.M declaration match the schema. (And also test it at app layer).
This seems to work, but if someone knows of a better way, please let me know: