HUGO

Restrict taxonomy values

Hi folks,

From reading the documentation, it seems that once I define a taxonomy term, I cannot restrict the values. Is there an easy way to create an equivalent of a Go’s Enum for each taxonomy term?

For example, I define taxonomy team, and I decide the only valid values are team A and team B. I don’t want developers adding team C, or any other values. In that case, ideally, the hugo build phase would fail.

Is there an easy way to achieve that?

Not sure whether it would be easy and I haven’t tested what I am posting below.

The only way that I can think of would be to conditionally check in the templates, whether a given taxonomy contains the allowed terms and then if there are more terms than the allowed ones, call the function errorF to make the build fail.

Also have a look at:

Indeed, I thought of something like that, but I considered that a hack. Not sure whether that’s a good way to enforce the Enum-like functionality.

I would have done the same, but more like “if term-a do this, else do that” with a clear note in the docs. There is to my knowledge no solution to fix terms, because Hugo will just take anything I define in frontmatter for your taxonomy.

Another solution would be a “manual” enum. Define the values of your “taxonomy” (stop calling it that in that case) in a data file and then let the user add a frontmatter parameter that contains one (or more) of the available values. In your templates do the same described above by me or by @onedrawingperday. You will loose list pages and all taxonomy functionality though with this solution.

Both solutions work only with the developers/writers knowing about that.