Star on GitHub
Generating config components

Generating config components

Turn a YAML schema into JSX components that write another program's config file, with validated props.

A ConfigFile (see Rendering a config file) writes whatever text its render function returns. For a format with dozens of settings, building that string by hand means every typo reaches the target program. A schema file moves the format's rules into one YAML file: Tauler turns it into JSX components, and a bad value throws in your layout instead of landing in kitty.conf.

The schema describes the components and the text they produce. It does not know what kitty, rofi or any other format is. That knowledge is only in the pattern strings and the template you write.

Import a schema file

Import a file whose name ends in .schema.yaml. Tauler generates the components when the import loads:

jsx
import { KittySettings, FontFamily, FontSize, ScrollbackLines } from './kitty-config.schema.yaml'

There is no generate step and no .jsx file to keep in sync. The named exports are the component names the schema declares; there is no default export. The path resolves like any other import (see The layout file). Editing the schema reloads the layout, the same as editing an imported .jsx file.

A schema file that does not parse fails the import, so the layout does not load. Tauler logs the reason as schema failed to parse.

Use the components

ConfigFile takes the root component's output as its render result:

jsx
const KittySettingsFile = ConfigFile({
  path: '/home/you/.config/kitty/tauler-kitty-settings.conf',
  render: () => (
    <KittySettings>
      <FontFamily value="JetBrains Mono" />
      <FontSize value="14" />
      <ScrollbackLines value="10000" />
    </KittySettings>
  ),
})

export default function render() {
  return <root><KittySettingsFile /></root>
}

The file ConfigFile writes, using the schema from the next section:

text
# GENERATED by tauler from kitty-config.schema.yaml — do not edit by hand.

font_family JetBrains Mono
font_size 14
scrollback_lines 10000

The comment line is text from the schema's template, not something Tauler adds. For when ConfigFile writes, and for reloading the program that reads the file, see Telling something the file changed.

Write the schema file

A schema file is two YAML documents separated by ---. The first declares the components, the second holds the template. A file with one document fails with expected two YAML documents (schema, then template) but the template document is missing.

This is a complete schema for the example above. It is a cut-down version of tauler-configgen/examples/kitty-config.schema.yaml, which declares many more settings:

yaml
nodes:
  - component: KittySettings
    root: true
    container: true
    children_as: settings

  - component: FontFamily
    props:
      value: { pattern: '^.+$' }
    constants: { directive: font_family }

  - component: FontSize
    props:
      value: { pattern: '^[0-9]+(\.[0-9]+)?$' }
    constants: { directive: font_size }

  - component: ScrollbackLines
    props:
      value: { pattern: '^-?[0-9]+$' }
    constants: { directive: scrollback_lines }

  - component: ProgressBar
    props:
      value: { enum: ["left", "right", "top", "bottom", "hidden"] }
    constants: { directive: progress_bar }
---
template: |
  # GENERATED by tauler from kitty-config.schema.yaml — do not edit by hand.
  {% for setting in settings %}
  {{ setting.directive }} {{ setting.value }}
  {%- endfor %}

Save it as kitty-config.schema.yaml next to the layout file. Every new setting is one more node of the same shape as FontFamily or ProgressBar.

Declare components

The first document has one key, nodes, a non-empty list. Each node takes:

FieldMeaning
componentRequired. The exported name. Must match ^[A-Za-z_$][A-Za-z0-9_$]*$.
roottrue on exactly one node. That component returns the rendered file text.
containertrue if the component takes children. Requires children_as.
children_asName under which a container's children appear in the template data (settings above).
propsMap of prop name to a rule. Every declared prop is checked on each use.
constantsMap of field name to a fixed string. Added to the component's data without being a prop.

A prop rule takes any of:

FieldMeaning
patternA regular expression the value must match. The value must be a string.
enumA list of allowed values (at least one).
errorThe sentence to throw when pattern fails, in place of must match <pattern>.
itemsA nested rule. The prop must then be an array and every element is checked against the rule.

Two nodes with the same component fail with component name "A" is used for more than one node in this schema — every node needs a distinct name.

Write the template

The second document is template:, a minijinja template (Jinja syntax). It receives one object, built the same way for every root. The object holds the root's constants and the root's own props. If the root is a container, it also holds the list of child data under the root's children_as name. Each child in that list holds its constants and its props. In the kitty schema, the root has no constants or props, so the object holds only settings; setting.directive is a constant and setting.value is the prop.

A child that is itself a container holds its own children under its own children_as name. tauler-configgen/examples/rofi-full-theme.schema.yaml nests selectors this way.

The repository ships four example schemas in tauler-configgen/examples/:

  • kitty-config.schema.yaml: flat directive value lines, no nesting.
  • rofi-config.schema.yaml: rofi's configuration{} block, also flat.
  • rofi-full-theme.schema.yaml: rofi theme selectors and properties, including compound selectors such as element selected and the array-valued children property.
  • rofi.schema.yaml: a small rofi example with flat properties and Padding/Border shorthand.

A template syntax error, or an error while rendering one (an unknown filter, say), throws with minijinja's message prefixed renderTemplate:. Layout code can also call renderTemplate(source, data) directly; it touches nothing outside the template and data.

Check what a bad value does

Every generated component first rejects unknown props, then checks each declared prop. Props are read with bracket access (props["value"]), so a prop name needs no identifier rules. Only component names are validated as identifiers; Tauler refuses to generate from a name such as Bad-Name, with this error:

text
schema does not match the node-schema meta-schema: "Bad-Name" does not match "^[A-Za-z_$][A-Za-z0-9_$]*$"

A component that breaks a rule throws an Error while render runs. For a schema with v: { pattern: '^x$', error: 'must be x' } on component A:

MistakeError
<A w="1" />A: unexpected prop "w"
<A v="y" />A: "v" must be x, got "y"
<A /> (prop missing)A: "v" must be x, got undefined

Without error, the second message is A: "v" must match ^x$, got "y". With enum, it is A: "value" must be one of ["left","right",...], got "up" (the list printed in full). The got value is printed as JSON, so a number 14 passed to a pattern prop reads got 14: pattern props must be strings, so write value="14".

A container rejects stray text or whitespace between its children with flattenChildren: expected a marker object, got "...". Conditionals that evaluate to false, null or undefined, and arrays from .map(), are accepted.

Schema-level mistakes fail when the schema is parsed:

MistakeError
No root: true node, or two of themschema does not match the node-schema meta-schema: None of [...] are valid under the given schema
container: true without children_asschema does not match the node-schema meta-schema: "children_as" is a required property
Invalid YAMLschema is not valid YAML: ...