If you need to make sure that a macro is used properly, consider using validators. These methods can be used anywhere within the macro code to check whether certain conditions are met or not. Some default validators are provided to check the number of parameters of a macro, and they are actually used in some system macros.
If you want to create your own validators, you can call the generic validate method which takes the
message to display in case of error, a Hash of options and a block containing the validation to perform.
You can, of course, create your own validators to check whether a macro is used directly within another. While this may seem a good idea to enforce constraints into the way documents are created, it has one major drawback: if you define a macro with such validation, you’re effectively limiting its usage, so for example you won’t be able to use within snippets or other custom macros.
Suppose, for example, that the box macro is
only allowed directly under a section macro. This means that, for example:
- the macro cannot be used within
chapterorappendixmacros. - the macro cannot be used in snippets
Even if you consider all the possibilities within the scope of the default macros provided with Glyph, this could
still make the box macro unusable within custom macros.
For the specific cases where a macro does not make sense unless is within another (e.g. the eq macro), a within validator
is used. Note though, that this validator only checks that the macro is used within another, but it is not
necessarily its child.