Can a schema express mandatory nodes?

Is it possible in ProseMirror to define a schema that expresses a mandatory structure of non-optional nodes (and if a document does not adhere to that, mark the document as ‘invalid’)?

There’s no concept of an invalid document in ProseMirror’s core. You can specify that some nodes must be present, but those nodes must be creatable out of thin air (no non-default attributes, no children that can’t be created automatically), and the library will just make sure they are always there. Which works for some use cases, but isn’t appropriate for others. Nothing stops you from writing a separate check that determines whether a given document is valid or not and showing some kind of indicator for invalid documents, of course (maybe taking inspiration from the lint example).

1 Like

Somewhat related - and/but to make sure I ‘get’ this…

You say there is no concept of an invalid document. Now in a schema you can specify the structure of a document. That leaves me with an important question:

How does Prosemirror help in making sure it adheres to the schema?

__edit

Rereading parts of the doc I’ve stumbled upon this:

“The schema is allowed to specify more precise constraints on what may appear where—i.e. even though a node allows block content, that doesn’t mean that it allows all block nodes as content.”

This seems to suggest that indeed Prosemirror enforces (these) constraints somehow. Can this be confirmed?

Yes, you’re expected to provide a valid start document, and any change (transaction) is guaranteed to not violate the schema (steps that would violate it can’t be applied).

1 Like

Thanks for taking my (basic) questions. I apologize but I have yet one more on this topic.

You said:

you’re expected to provide a valid start document

Does this mean that constraints won’t be enforced if you start out with an empty document (obviously if the schema allows for such a thing - e.g. Paragraph*)

Creating your start document with createAndFill will make sure it’s a minimal valid document. If you just create a node without the required content then yes, that’ll mess things up. You can run check on a node you’ve created or loaded to verify that it conforms to the schema.

1 Like