pubkey/rxdb

Firestore Replication Validation Errors being thrown

jonnyreeves opened this issue · 7 comments

I am using rxdb with Firestore replication. In dev mode I am using the wrappedValidateZSchemaStorage validator.

When changes are pulled, the following error is being thrown:

 LOG  [RxDb] rx active$ households true
 LOG  [RxDb] rx recieved$ households {"_attachments": {}, "_deleted": false, "_meta": {"lwt": 1706706713404.01}, "_rev": "3-548759169d2215403e6e19ee1a8b83e2efdb22ac26bbbf32d6f8df6b080d0246", "id": "yDAJ2lx8WKAFGo4ZPZYn", "name": "test_dY_Yh"}
 LOG  [RxDb] rx error$ households [RxError (RC_PULL): RxError (RC_PULL):
RxReplication pull handler threw an error - see .errors for more details
Given parameters: {
id:"yDAJ2lx8WKAFGo4ZPZYn|0"
writeError:{
  "status": 422,
  "isError": true,
  "documentId": "yDAJ2lx8WKAFGo4ZPZYn|0",
  "writeRow": {
    "previous": {
      "id": "yDAJ2lx8WKAFGo4ZPZYn|0",
      "isCheckpoint": "0",
      "itemId": "yDAJ2lx8WKAFGo4ZPZYn",
      "docData": {
        "_deleted": false,
        "name": "kek_test_5VGYr",
        "id": "yDAJ2lx8WKAFGo4ZPZYn"
      },
      "_attachments": {},
      "_deleted": false,
      "_rev": "1-rx_storage_replication_73c4a9ae2b2d37d278e71c91b9c7924517c926e5b08de45c2b79c99e52a679b1",
      "_meta": {
        "lwt": 1706706713402.01
      },
      "isResolvedConflict": "2-rx_storage_replication_73c4a9ae2b2d37d278e71c91b9c7924517c926e5b08de45c2b79c99e52a679b1"
    },
    "document": {
      "id": "yDAJ2lx8WKAFGo4ZPZYn|0",
      "isCheckpoint": "0",
      "itemId": "yDAJ2lx8WKAFGo4ZPZYn",
      "docData": {
        "_deleted": false,
        "name": "test_dY_Yh",
        "id": "yDAJ2lx8WKAFGo4ZPZYn"
      },
      "_attachments": {},
      "_deleted": false,
      "_rev": "2-rx_storage_replication_73c4a9ae2b2d37d278e71c91b9c7924517c926e5b08de45c2b79c99e52a679b1",
      "_meta": {
        "lwt": 1706706762654.01
      },
      "isResolvedConflict": null
    }
  },
  "validationErrors": [
    {
      "title": null,
      "description": null,
      "message": "Expected type string but found type undefined",
      "path": "#/isResolvedConflict"
    }
  ]
}}]

This error message is a little cryptic as it's not clear which field recieved type undefined when a string was expected. I'm also confused as I can see both id and name present in the docData field.

A simplified version of my schema is as follows:

export const householdSchema = {
  title: "household schema",
  version: 0,
  primaryKey: "id",
  type: "object",
  required: [
    "id",
    "name",
  ],
  properties: {
    id: {
      type: "string",
      maxLength: 21
    },
    name: {
      type: "string",
      maxLength: 128
    },
  }
}

Thanks!

Removing the wrappedValidateZSchemaStorage from my storage resolves the sync issues I am seeing - guessing there is either a problem with my schema (have I misunderstood how to use it), or an issue within the firebase replication plugin itself - will see what I can debug further myself.

isResolvedConflict is defined as optional in the schema, so it should not be a problem when it is undefined.
https://github.com/pubkey/rxdb/blob/master/src/replication-protocol/meta-instance.ts#L76

Can you try with the ajv validator?

I found several problems when adding a validator to the storage with the firestore replication. Working on a fix.

@jonnyreeves Thank you but I am fine.
So I fixed it by not setting isResolvedConflict at all when it has the value undefined.
Strange that noone found issue that before but also when replication metadata is set, it is an internal thing so RxDB never expected that write error could occur at that point so it did not emit an error$

By the way, live replication in the firestore-replication tests is still not tested because the firestore emulators did not emulate an event-stream at the time I build that. So maybe when you find the time you can check if this is fixed now and we can add tests for that.

@pubkey Confirmed that #5572 fixes the issue for me, logs below showing a query, followed by a direct firestore mutation, followed by a second query showing the updated state being replicated.

 LOG  Household:  {"id": "8xg7R2v7zCCAcPyelPym", "name": "bananatest"}
 LOG  Household:  {"id": "yDAJ2lx8WKAFGo4ZPZYn", "name": "test_O_97P"}
 LOG  updating yDAJ2lx8WKAFGo4ZPZYn name to:  test_vHofq
 LOG  [RxDb] rx active$ households true
 LOG  !!!! fix applied
 LOG  [RxDb] rx recieved$ households {"_attachments": {}, "_deleted": false, "_meta": {"lwt": 1706725849367.01}, "_rev": "3-548759169d2215403e6e19ee1a8b83e2efdb22ac26bbbf32d6f8df6b080d0246", "id": "yDAJ2lx8WKAFGo4ZPZYn", "name": "test_vHofq"}
 LOG  [RxDb] rx active$ households false
 LOG  [RxDb] rx active$ households true
 LOG  [RxDb] rx active$ households false
 LOG  Household:  {"id": "8xg7R2v7zCCAcPyelPym", "name": "bananatest"}
 LOG  Household:  {"id": "yDAJ2lx8WKAFGo4ZPZYn", "name": "test_vHofq"}