Feature request: Frontmatter validation (type checking)

Hello, dear developers!

Can you please implement Front Matter validation (type checking) for Hugo?

I think it’s important for large sites with a lot of content.

When creating lots of posts within different folders, it’s easy to forget something in the Front Matter. On large sites, the Front Matter can be extensive, storing a lot of values, right?

Don’t you think it’s necessary to have some validation for the Front Matter? Maybe it could be set up within the config file (e.g., validation for specific folders/sections in the content directory, also type checking for front matter values, like it should be string, number, boolean, etc), and Hugo could validate this data during build time.

How would you check for something that was forgotten? Eg draft – if you don’t specify it, it’s false. How would whatever verification step know if you forgot it or just didn’t want to specify the default value?

What do you plan to do, for example, with a parameter country that can have a single string as a value, or an array of strings, or not be present at all?

To validate frontmatter, you’d need a whole grammar specifying permitted types as well as if the parameter is required, forbidden, or optional.

The idiomatic way to handle this is with an init partial that is run once during each build. For example…

layouts/baseof.html

{{ if .IsHome }}
  {{ partial "init.html" . }}
{{ end }}

layouts/_partials/init.html

{{ range $p := site.Pages }}
  {{/* Validate front matter: description */}}
  {{ if not $p.Description }}
    {{ warnf "Missing description: %s" $p.String }}
  {{ end }}

  {{/* Validate front matter: params.is_approved */}}
  {{ with $p.Params.is_approved }}
    {{ if ne (printf "%T" .) "bool" }}
      {{ warnf "Incorrect data type: the 'is_approved' front matter field must be a boolean value: %s" $p.Page.String }}
    {{ end }}
  {{ end }}
{{ end }}

You can use the same approach to validate data files, etc.

Hugo’s documentation site uses this approach to validate one of the front matter fields:
https://github.com/gohugoio/hugoDocs/blob/master/layouts/_partials/helpers/validation/validate-keywords.html

What do you plan to do, for example, with a country parameter that can have a single string as a value, an array of strings, or not be present at all?

  1. You don’t just provide whatever data value you want, that’s the whole idea of type checking. You first set up the accepted value type for the validator, e.g., it should be a string, number, or array (the allowed data type for that value in Front Matter).
  2. If country should be set up as a string, and you provide a number instead, the validator should give you an error (because the data types don’t match). If it does not exist, it depends on your validator whether this Front Matter value is required or optional (like in TypeScript).

To validate frontmatter, you’d need a whole grammar specifying permitted types as well as if the parameter is required, forbidden, or optional.

Yep, that’s what I’m asking about.

Thank you, I will try to setup custom partial for validation.

My point was rather

  • country could be a single string value
  • or an array of strings
  • or not be defined at all.

Apparently, what you want can be done with Hugo. How that works with many parameters to check remains to be seen.