Enforcing schema attributes

Is there a plan to have ProseMirror enforce SchemaItem’s attributes, ie if I try to set an attribute which wasn’t declared in the NodeType it would not allow me to?

Right now I can set arbitrary properties on a Node’s attrs object and they will be reflected in the output of getContent(‘json’). This is kind of a double edged sword. On one hand, I like being able to set temporary, arbitrary values that are used in the state of my block, however I don’t particularly like that they are saved in the persisted state created by getContent(). It would be pretty trivial to filter them out in our custom parser, but if this functionality is something that should be part of ProseMirror core I’d rather build it there instead for others to take advantage of.

On a somewhat related note, if schema enforcement is going to be part of ProseMirror I’d like to propse the ability to define an enumerated set of values for an Attribute, ie { align: new Attribute({ default: "left", enum:["left", "center", "right"] }) }

It does. I.e. schema.node("image", {foo: "xx", src: "img.png"}) yields a node with attrs set to {src: "img.png", title: "", alt: ""}. How are you setting the attributes? If you’re mutating the attrs object then the answer is again, don’t do that. Assume data structures created by ProseMirror are read-only.

ProseMirror is not going to enforce schema consistency at every level, since that would simply be too expensive. The idea is for the code that creates a node to enforce that is is producing valid nodes, so that downstream code can make assumptions about the data. Commands, which are a common way to create and modify nodes, support parameter validation.

In general I am setting values on node.attrs after ajax calls come back. I will store this data elsewhere and it should solve my problem.

What are your thoughts about being able to define an enumeration of acceptable values for an Attribute?

I think that might as well live in the layer that creates the attributes, and stay out of the schema code. If you have a use case that suggests otherwise, let me know.