Failed schema validation after insert with sdk's becuase of null value

Luckily for me the key was obsolete, so I removed it to from the Schema.
Then there was no complaints from the API

But No, I did not find a solution.
From my tests, it seems that it is an issue in the Realm Backend Validation Service somewhere.
There is a clash between the Sync Client from mobile and the validation using the Functions.

The issue here is that null in MongoDB is treated as its own distinct type, which is quite different than how most developers would think about a nullable field in statically defined languages. A conversion feature/project is on deck for this quarter so hopefully we will get this sorted for you soon. In the meantime, you can use the workaround Sumedha described: define an empty value, or change the field to be required. Another option is leveraging our new Mixed type here:

Hope this helps

2 Likes

thx @Ian_Ward

as the deadline for the migration to mongodb is near: I guess the Workaround with empty / required is the best solution?

Yes, having the field required with an empty default value would always pass validation and prevent such errors. @rouuuge

Hi,
In relation to this thread, what would be the best recommended way to have a null value in an object relationship?

For instance, I have an object as follows:

public class Template : RealmObject
{
    [PrimaryKey]
    [MapTo("_id")]
    public int TemplateId { get; set; }
    public string TemplateDescription { get; set; }
    public Department Department { get; set; }
    public Example Example{ get; set; }
}

Where Department and Example are Realm objects.

When this is inserted into the realm by my .Net app, the entry in the MongoDB is as follows:

{
    "_id":{"$numberLong":"123"},"
    _partitionKey":"key",
    "Example":null,
    "Department":null,
    "TemplateDescription":"Example"
}

This fails validation because of the null values of Department and Example even though it still syncs with the app.

How do you suggest I implement this as you can’t create a default Realm Object to use in a relationship?

Many Thanks

hi @ScribeDev my solution was:

I removed the relationship in the sync definition (relationship.json when you export your realm-app)

Then you could do something like:

public ObjectId Departement

and set a default Value of: (swift style)

ObjectId.init("000000000000000000000000")

Is there a solution for this yet?

I am using the latest Swift SDK and experience the same problem:

Invalid type. Expected: undefined, given: nul

Hi all,

I faced the same issue using Realm Web and after struggle with it I figured it out a 3rd option that has worked for me.
The solution is simply not set the properties which would be null or undefined in the object you are about to persist. If I set them as null or undefined, realm consider this as invalid and throws me the error " Expected: undefined, given: null payerAddress: Invalid type."

Here you can see the null checks that I have to do before setting the properties and its values.

I just checked the android SKD (java). There is the same problem as with the swift SDK..

.