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

hi,
After I insert a new Realm-Object via SDK (Swift or Java) that contains a null value I run into a schema validation error:

2 Invalid Type InvalidTypeError taskId

taskId is a optional attribute defined like:

"taskId": {
      "bsonType": "objectId"
 }

how can i save a null value?

thank you

1 Like

In the Documentation I found this:

Optional properties may contain a null value or be entirely omitted from an object. By default, all properties are required unless explicitly marked as optional.

so i modify my schema to:

"taskId": {
   "bsonType": "objectId",
   "optional": true
 }

But I still get the InvlidTypeError because of the null value…

here my swift object model:

class EventT: Object {
@objc dynamic var _id: ObjectId = ObjectId.generate()
@objc dynamic var taskId: ObjectId?
}

Hi @rouuuge, have you defined a primary key for the Object (I assume you’d want something like:

override static func primaryKey() -> String? {
    return "_id"
} 

In your schema, check that taskId isn’t included in the required section. e.g., this would prevent you having it set to nil:

"required": [
        "_id",
        "taskId"
      ],

hi @Andrew_Morgan, yes I defined my primary key and the taskId is not ind the required section. Sorry for the incomplete example!

Also the synchronization works. The only thing is the validation from the object after I inserted it via sdk:

because of the null type here:

So far I ignored that because the sync between mobile works. But now I started to implement a WebApp with realm-web. This works until I try to update a object that was created by a IOS-Device via the WebApp.

If I want to update just one field from the object with the update $set:

"update": {
  "$set": {
    "state": {
      "$numberInt": "1"
     }
 ...

I get the same error like on the mongodb-validation step:

could not validate document: note: Invalid type. Expected: undefined, given: null taskId: Invalid type. Expected: undefined, given: null

thanks for looking into this

@rouuuge Thanks for the extra info.

I’ve recreated the issue and have raised the question with the engineering team. I’ll let you know when I get a response.

1 Like

Thank you @Andrew_Morgan!

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

Thanks for confirming. I suspect that the issue will be with the translator component in the backend that maps to Atlas documents

1 Like

hi @Andrew_Morgan do you have any updates about this?

Hi @rouuuge it appears that we’re running up against the differences between what’s supported by Objects and JSON Schema. Something to try (to allow you to modify documents in Atlas) is to click on the “Advanced Mode” button on the schema page in the Realm UI and set apply_when to false.

hi,

to allow you to modify documents in Atlas

my problem is not that i cant modify documents in Atlas. Or maybe I missunderstand you. This post I posted on february describe the problem maybe a bit more clearer:

hi @rouuuge,
The schema that you see in the Realm UI is used for a number of things – GraphQL API, Atlas document validations, and Realm Sync. I’m assuming that the part you care about is Realm Sync. My suggested workaround should stop the schema being used for Atlas document validation, and so (after the change) the schema shouldn’t prevent you from making changes to the Atlas data.

Hi @Andrew_Morgan
Any updates on this? I believe I am facing the same underlying issue.

1 Like

Could you share your iOS Object class definition plus the Realm schema? I’m travelling this week but I’ll try to reproduce the issue.

1 Like

Safe travels.
There you go, I replied to the original post

Hi @Georges_Jamous I’m afraid that I haven’t had chance to recreate this. Have you tried setting apply_when to false in the schema Advanced Mode?

hi @Andrew_Morgan
on Mar 18 you wrote:

I’ve recreated the issue and have raised the question with the engineering team. I’ll let you know when I get a response.

What was the response? It looks that you could recreated my example? If setting apply_when really the only solution could you describe bit more where to insert that rule? (btw im Using realm Sync)

thank you

Hi @Andrew_Morgan

I am not using GraphQL, just using iOS Sync. Where would I be able to change this?

I ran into the same issue today, do you have any update from the team?

I seems that the root cause is that no matter what the field type is, using a function to update it, it must have a value.
Non existent key, key set to null or key set to undefined does not work.
For my optional string schema, I had to pass an empty string ""

1 Like

I also attempted to update an existing object in the Database that was synced from iOS Client containing an non-existent value for an optional key.
In the Collection that value was Null.

await collection.updateOne(
            { _id: ... },
            { $set: { parentId: newID }},
            { session });

// the object I am updating here contains a key
// { name: null }

it failed stating the the value that was already non existent (NULL in record) needed to be Undefined. But it was Null. (again, the NULL value was set by the sync client form the SDK or Realm server)

Expected: undefined, given: null

it looks like the validation mechanism that runs when updating a value is different that the one during a sync operation in the backend

1 Like

hey @Georges_Jamous did you just found a solution? I also have a iOS-Sync App and run into the issue when i am using some serverside javascript functions.

@Sumedha_Mehta1 I had contact with you in April via mail. You suggest me this workaround:

Sounds like you’re asking for nullability support in our types. This is something we’re currently working on, but in the meantime I would suggest using the empty string, or some other dummy string because the InvalidTypeError is important for Sync.

Do you have maybe an update for this?