Schema validation in Realm Sync

I have configured a basic schema validation in Realm Sync: minLength and maxLength of a string property.

To my surprise, a node.js client ignores the validation and inserts a new document just fine. Running the validation check gives StringLengthLTEError and StringLengthGTEError as expected. This can’t be the expected behaviour? I would assume an error similar to when a required property is missing, catchable by error handler.

Moving on, I tried to put a validation rule in Atlas with action Error. Now, in the Realm log, I get a TranslatorCorrectiveErasureError with message “Failed to apply realm changes to MongoDB. This has been resolved by removing the object from the synced realm.”. My new document is created, synced and deleted! My client gets notified about the index being deleted but I’m expecting an error.

I’m grateful for any insight of how to do proper schema validations in Realm Sync.

Thanks!

Hi @Mikael_Gurenius - Welcome to the community.

Would you be able to share your schema?

Hello @Mikael_Gurenius

I want to give an update also in this thread about the discussion we have internally in our support ticket.

Unfortunately, at this moment Realm Sync only supports type validation, however, we are looking into supporting additional JSON schema validation such as minLength and maxLength in the near future. Therefore, synchronizing documents where the types do not match the schema, currently the only type validation supported, will result in errors such as those you have already experienced.

Please let me know if you have any additional questions or concerns regarding the details above.

Kind Regards,
Josman

Sure, in it’s most simple form, here is the schema for Realm Sync:

{
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "_partition": {
      "bsonType": "string"
    },
    "shortText": {
        "bsonType": "string"
    },
      "reportedBy": {
      "bsonType": "string"
    }
  },
  "required": [
    "_id",
    "shortText",
    "reportedBy"
  ],
  "title": "CreateNotification"
}

Here is the validation rule in Atlas:

{
  $jsonSchema: {
    properties: {
      reportedBy: {
        bsonType: 'string',
        maxLength: 12,
        minLength: 3,
        description: 'must be a string of 3 to 12 chars'
      }
    }
  }
}

The result is TranslatorCorrectiveErasureError in the log and my new object is deleted.