Json Schema validation error messages are frustratingly unhelpful

"A bulk write operation resulted in one or more errors.\r\n  Document failed validation"

Is the error I get when the schema validation fails. It doesn’t tell me what failed, so I have to spend several hours hand hacking and guessing before giving up on the idea of having important data follow any kind of guaranteed structure.

I can insert a test set into the db, then in Compass, analyze the collection and share the schema but it turns out that that format is not acceptable to be used in a $jsonSchema. The other popular json schema generator is the newtonsoft library, but it generates standard schema so the “type” attributes and aliases need to be mongofied before trying to use it as $jsonSchema, but it still does not work.

This is a small c# class

public class EntitySequence
{
    public static string CollectionName => $"{nameof(EntitySequence).ToLowerInvariant()}s";

    public object Id { get; set; }
    public string EntityTypeName { get; set; }
    public bool IsTenantSpecific { get; set; }
    public long TenantCode { get; set; }
    public long CurrentNumber { get; set; }
}

and the schema file I hand made looks like

 {
    "$schema": "http://json-schema.org/draft-04/schema",
     "title": "EntitySequence jsonSchema",
     "bsonType": "object",
     "properties": {
        "Id": {
          "bsonType": "objectid" 
         },
       "EntityTypeName": {
           "bsonType": "string"
         },
       "IsTenantSpecific": {
          "bsonType": "bool"
       },
       "TenantCode": {
          "bsonType": "long"
      },
      "CurrentNumber": {
        "bsonType": "long"
      }
  },
 "required": [
    "EntityTypeName",
    "IsTenantSpecific",
    "TenantCode",
    "CurrentCode"
  ]
}

What else does this want?

1 Like