Elegantly handle failed deserialization in nodeFromJSON

Hi, During development, I am changing the schema often. It makes me wonder how I could elegantly catch scenarios where the data are stale compared to the current schema.

Example code:

        let doc = schema.nodeFromJSON(content)
        return EditorState.create({ ...commonOpts, doc })
      } catch (err) {
        console.warn("Warning: we found saved text editor content but it is not in a valid format. Error:", err)
        return null
      }

The function call may fail due to certain types of nodes, namely very custom nodes I am adding.

What’s your best practice to handle this scenario? Alternatives I’ve contemplated:

  • making fields optional/provide default values, so basically keeping the schema backward compatible until I migrate all schemas
  • display the full JSON
  • other?

Ideally, a failed node could be optionally ignored, outputting a list of warnings instead of a fatal error.

The general solution to this is to eagerly run your whole database through a migration function whenever you change the schema. Handling schema mismatches is something I consider outside of the scope of the library.

2 Likes