简体   繁体   中英

Mongo 3.6 debug validation using jsonSchema

I'm following a course from mongodb university to learn new features in the release 3.6, and I'm unable to resolve why my validator is invalid.

This is how I created the collection:

db.getSiblingDB("TSA").createCollection("claims", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            properties: {
                _id: { },
                airportCode: { type: "string", minLength: 3 },
                airportName: { type: "string" },
                airlineName: { type: "string", minLength: 5 },
                claims: {
                    bsonType: "object",
                    properties: {
                        itemCategory: { bsonType: "array", maxItems: 3 },
                        amount: { type: "string", pattern: "^\$.*" }
                    }
                }
            },
            required: ["airportCode", "airlineName", "claims"],
            additionalProperties: false
        }
    }
})

Then, I try to insert this object:

db.getSiblingDB("TSA").claims.insertOne({
    "airportCode": "ABE",
    "airportName": "Lehigh Valley International Airport, Allentown",
    "airlineName": "MongoAir",
    "claims": {
        "claimType": "Property Damage",
        "claimSite": "Checked Baggage",
        "itemCategory": [ "Sporting Equipment & Supplies" ],
        "amount": "$180.00"
    }
})

Getting the following error:

WriteError({
    "index" : 0,
    "code" : 121,
    "errmsg" : "Document failed validation",
    "op" : {
        "_id" : ObjectId("5a705318d3d6c18337f07282"),
        "airportCode" : "ABE",
        "airportName" : "Lehigh Valley International Airport, Allentown",
        "airlineName" : "MongoAir",
        "claims" : {
            "claimType" : "Property Damage",
            "claimSite" : "Checked Baggage",
            "itemCategory" : [
                    "Sporting Equipment & Supplies"
            ],
            "amount" : "$180.00"
        }
    }
})

My question is, is there some way to debug the validator like "property X must be Y type" instead of getting a generic "Document failed validation"?

As of MongoDB 3.6, there is no feedback mechanism that would inform what part of a document failed validation during a server-side check. A corresponding feature request is open: SERVER-20547: Expose the reason an operation fails document validation . For now, it is left to the application code to perform its own validation if detailed feedback is required.

If you use regex pattern string, backslash itself must be escaped too:

db.getSiblingDB("TSA").createCollection("claims", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            properties: {
                _id: { },
                airportCode: { type: "string", minLength: 3 },
                airportName: { type: "string" },
                airlineName: { type: "string", minLength: 5 },
                claims: {
                    bsonType: "object",
                    properties: {
                        itemCategory: { bsonType: "array", maxItems: 3 },
                        amount: { type: "string", pattern: "^\\$.*" }
                    }
                }
            },
            required: ["airportCode", "airlineName", "claims"],
            additionalProperties: false
        }
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM