Requiring all elements

Often we want to ensure that eno documents authored by users always contain all of the fields that can be specified according to our schema, regardless of whether they are actually used or left empty, because the simple fact that they see the field in the document they are authoring informs them that it exists and can be used, whereas if it were entirely missing in the document, only external documentation can point the user to the possiblity to define that specific field.

To easily enable such behavior there is a shortcut that saves us from writing out required in front of every element we query from the document:

document.allElementsRequired();
document.field('name').optionalStringValue();

Here the field name always has to be present, no matter if it's empty or not.

Note that this can be called not only on the document but also on any section or fieldset:

const details = document.section('details');

details.allElementsRequired();

Any element queries on details from that point on always require the elements to be there, including the ones in subsections and fieldsets, recursively.

Lastly, this setting can also be reverted again by explicitly passing false:

document.allElementsRequired();
document.allElementsRequired(false);
document.field('name').optionalStringValue();

Here the field name is optional, just as its value.


Next page: Touching elements