Write a Custom Schema

OKF-Schema validates frontmatter against JSONSchema files in _schema/. This guide shows how to write schemas for your own concept types.

Schema file naming

Place schema files in bundle/_schema/ with the naming convention:

_schema/
  _base.schema.yaml      # Optional shared constraints
  table.schema.yaml      # Applied when type: table
  metric.schema.yaml     # Applied when type: metric

The type field selects the schema: type: table → table.schema.yaml.

Supported file extensions

You can use YAML (.yaml, .yml), JSON (.json), or JSON5 (.json5) for schema files. _base.schema.yaml is not applied implicitly; reference it from each type schema that should inherit its constraints.

Minimal schema example

$schema: "https://json-schema.org/draft/2020-12/schema"
type: object
properties:
  type:
    const: "table"
  title:
    type: string
  description:
    type: string
  owner:
    type: string
  columns:
    type: array
    items:
      type: object
      properties:
        name: { type: string }
        type: { type: string }
      required: [name, type]
required:
  - type
  - title
additionalProperties: false

$ref between schemas

The resolver supports whole-file relative references. Compose shared constraints with allOf:

# _schema/_base.schema.yaml
type: object
properties:
  type: { type: string }
  title: { type: string }
required: [type, title]
# _schema/table.schema.yaml
allOf:
  - $ref: "_base.schema.yaml"
  - type: object
    properties:
      type: { const: table }
      owner: { type: string }

Paths in $ref are relative to the _schema/ directory.

Project metadata for plain OKF consumers

A consumer that knows only the OKF specification reads relations from Markdown links in the body (section 6.1) and the lifecycle from status (section 5.4). When your bundle keeps relations as frontmatter IDs or uses its own lifecycle words, declare in the schema how they are projected; okf-schema lint and okfkb update then maintain the projection:

properties:
  state:
    type: string
    enum: [wip, done, gone]
    x-okf-status-projection: {wip: draft, done: stable, gone: deprecated}
  parents:
    type: array
    items:
      type: string
      x-okf-link: {resolution: filename-stem, syntax: plain, link-projection: Parents}
okf-schema lint --path bundle
okf-schema lint --path bundle --check   # exit 1 when a projection is stale

A document with state: wip and parents: [root, missing] becomes:

---
type: note
state: wip
status: draft
parents: [root, missing]
---

# Child

<!-- okf:links:start -->
### Links

<!-- Generated from the frontmatter relations; edit them instead. -->

- Parents:
  - [root](root.md): Root note
  - `missing`
<!-- okf:links:end -->
  • One group per annotated field, in schema order (an allOf base schema first), one relative link per value followed by the target title. A value that does not resolve stays as code, so no broken link is written.

  • Renaming a label renames its group; removing it removes the group, and a section without groups is removed, on the next lint.

  • Only filename-stem, bundle-relative and bundle-relative-stem targets can be projected; okf-schema validate reports E11 for a label on another resolution and for a status mapping that misses an enum value or maps to anything other than draft, stable or deprecated.

  • lint --links skips the generated section, so links/backlinks keep listing authored links only. --no-link-projection turns the projection off for one run.

Check that your schema is loaded

Validation tells you when a schema file yields no schema, so a new or broken file never makes a type silently unchecked:

$ : > _schema/task.schema.yaml
$ echo '{"type": "object",' > _schema/note.schema.json
$ okf-schema validate --path bundle
bundle/_schema/note.schema.json
  WARNING [W17] schema file 'note.schema.json' cannot be parsed: Expecting property name enclosed in double quotes: line 2 column 1 (char 19)

bundle/_schema/task.schema.yaml
  INFO    [I1] schema file 'task.schema.yaml' is empty

An empty (or comment-only) file is informational (I1) and never fails --strict. A syntax error, an unreadable file or a non-mapping value is warning W17. In both cases the type stays without schema. See the validation codes.

Schema metadata for index generation

okf-schema index reads two optional top-level fields to produce richer index.md files. These fields are JSONSchema extensions — they do not affect validation, only the generated documentation.

Field

Type

Purpose

title

string

Short heading used as the H1 in subdirectory index.md files

description

string

Intro text for a type-specific subdirectory

x-okf-summary

string

One-line description used in the root directory listing

When either field is absent, the other is used as its fallback. When a directory contains a mix of concept types, a generic fallback description is used for the subdirectory intro.

Example

# _schema/metric.schema.yaml
$schema: "https://json-schema.org/draft/2020-12/schema"
type: object
title: "Metric"
x-okf-summary: "Business and engineering metrics with targets and owners."
description: "Schema for KPIs, SLIs, and other measurable indicators."
properties:
  type:
    const: "metric"
  # ...

Running okf-schema index on a bundle with this schema produces:

  • Root index.md entry: [metrics](./metrics/) — Business and engineering metrics with targets and owners.

  • metrics/index.md heading: # Metric

  • metrics/index.md intro paragraph: Schema for KPIs, SLIs, and other measurable indicators.

Tips

  • Start with additionalProperties: true while iterating, then lock down to false when stable.

  • Use enum for fields with a fixed set of values (e.g., status: [draft, review, published]).

  • Add description to every property: it becomes documentation for bundle authors.

See also