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

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

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": [

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.


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.