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:
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:
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:
# GENERATED by tauler from kitty-config.schema.yaml — do not edit by hand.
font_family JetBrains Mono
font_size 14
scrollback_lines 10000The 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:
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:
| Field | Meaning |
|---|---|
component | Required. The exported name. Must match ^[A-Za-z_$][A-Za-z0-9_$]*$. |
root | true on exactly one node. That component returns the rendered file text. |
container | true if the component takes children. Requires children_as. |
children_as | Name under which a container's children appear in the template data (settings above). |
props | Map of prop name to a rule. Every declared prop is checked on each use. |
constants | Map of field name to a fixed string. Added to the component's data without being a prop. |
A prop rule takes any of:
| Field | Meaning |
|---|---|
pattern | A regular expression the value must match. The value must be a string. |
enum | A list of allowed values (at least one). |
error | The sentence to throw when pattern fails, in place of must match <pattern>. |
items | A 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: flatdirective valuelines, no nesting.rofi-config.schema.yaml: rofi'sconfiguration{}block, also flat.rofi-full-theme.schema.yaml: rofi theme selectors and properties, including compound selectors such aselement selectedand the array-valuedchildrenproperty.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:
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:
| Mistake | Error |
|---|---|
<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:
| Mistake | Error |
|---|---|
No root: true node, or two of them | schema does not match the node-schema meta-schema: None of [...] are valid under the given schema |
container: true without children_as | schema does not match the node-schema meta-schema: "children_as" is a required property |
| Invalid YAML | schema is not valid YAML: ... |