[Bug?] type-safety lost after sharing?

If you try to access an already defined top-level entry again with a different constructor

const originalDoc  = new Y.Doc()
originalDoc.get('',Y.Map)
originalDoc.get('',Y.Array)

Yjs will throw. However, if the Y.Doc has been loaded by a provider, this type-safety is lost. According to my tests,

  • a Y.Map may then be accessed as a Y.Array, and
  • a Y.Array, a Y.Text, a Y.XmlElement, a Y.XmlFragment or a Y.XmlText as a Y.Map

This should presumably destroy your shared data?

It’s up to your application to keep all your top level Y.Doc access square.

Well, this sounds simple and obvious if both data model and applications are from the same developer.

But in a situation, where multiple applications may work on the same shared data, mistakes may destroy data.

Following the rule “every service has to protect itself” one could think of implementing s.th. like “schema validation” either in the applications themselves or in a thin layer around actual data - but Yjs providers are not prepared for that.

According to my current knowledge, the only solution would be to convert updates to (intermediate) docs, check them for validity and (if so) pass the updates to the actual data model - or ignore them otherwise.

This not only covers top-level entries but any part of shared data.

1 Like

Unfortunately from experience, it’s not simple and obvious even with one dev origin. :sweat_smile: Validation and backwards compat takes up a lot of my time.