How to validate frontmatter?

How do I validate my website’s frontmatter to ensure

  1. No unknown keys - this can catch typo e.g. “alises” instead of “aliases”
  2. No incorrect types - e.g. string instead of array of string for “aliases”

We do some front matter validation on the docs site:

As you can see from the first reference above, we only run it once (when building the home page).

In your situation I would create a slice of valid front matter fields, then range through all pages (outside loop) and front matter parameters (inside loop) and compare with the slice.

You can test data types using printf with the %T verb , reflect.IsMap, and reflect.IsSlice.

There’s no need to test .Aliases as the value is cast to []string.

Using Remark with a frontmatter extension.

Not sure how versed you are in these things, here is the short version of my setup:

  • The plugin remark-lint-frontmatter-schema
  • my (handmade) schemata for some frontmatter (this could/should probably be online somewhere, but I am not “done” setting it up and online sources are heavily cached within the remark plugin.
  • my remark config - the interesting part starts in line 20ff.
  • Using the remark plugin in vscode errors show up. If they don’t show up I add the schema to the frontmatter like this (from my archetype):
    ---
    '$schema': /static/_schemata/blog.schema.yaml
    title: {{ replace .Name "-" " " | title }}
    linkTitle: {{ replace .Name "-" " " | title }}
    description: ""
    summary: ""
    ---
    

Remark is in generally good for everything where your search query contains “markdown”.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.