Content Schema System


Norgolith content schemas system allows you to define and enforce structure and validation rules for your Norg documents’ metadata. This ensures consistency across your content and prevents errors during the build process.

Why Use Content Schemas?

Content schemas are particularly useful for:


Schema Basics

Content schemas are defined in your norgolith.toml file under the [content_schema] section. A schema consists of:

Example: Basic Schema

[content_schema]
required = ["title", "author"]

[content_schema.fields.title]
type = "string"
max_length = 120

[content_schema.fields.author]
type = "string"

Field Types

Norgolith supports several field types, each with its own validation rules:

String

Options

Example

[content_schema.fields.title]
type = "string"
max_length = 100
pattern = "^[A-Z].*"  # Must start with uppercase letter

Boolean

Example

[content_schema.fields.draft]
type = "boolean"

Array

Options

Example

[content_schema.fields.categories]
type = "array"
items = { type = "string" }
min_items = 1
max_items = 5
must_contain = ["norgolith"]

Object

Options

Example

[content_schema.fields.author]
type = "object"
schema = { name = { type = "string" }, email = { type = "string" } }

Validation Rules

Validation rules allow you to enforce conditional logic based on metadata values.

Example: Conditional Rule

[content_schema.posts.rules]
if = { draft = false }  # Condition
then = { required = ["publish_date"] }  # Action

This rule ensures that if draft is false, the publish_date field must be present in any posts/ file.

Schema Inheritance

Schemas can inherit and extend rules based on content paths. For example, rules for /posts can extend global rules, and /posts/2023 can further extend /posts.

Example: Path-Based Inheritance

# Global rules
[content_schema]
required = ["title"]

# Rules for /posts
[content_schema.posts]
required = ["category"]

# Rules for /posts/2023
[content_schema.posts.2023]
required = ["year"]

In this example:


Practical Examples

Example 1: Blog Post Schema

[content_schema]
required = ["title", "author"]

[content_schema.fields.title]
type = "string"
max_length = 120

[content_schema.fields.author]
type = "string"

[content_schema.posts.rules]
if = { draft = false }
then = { required = ["publish_date"] }

Example 2: Project Documentation Schema

[content_schema]
required = ["title", "version"]

[content_schema.fields.title]
type = "string"

[content_schema.fields.project_version]
type = "string"
pattern = '^\d+\.\d+\.\d+$'  # Semantic versioning

[content_schema.docs.rules]
if = { status = "published" }
then = { required = ["reviewer"] }

Troubleshooting Validation Errors

Norgolith will show you validation issues by default in the following format, either as errors when trying to build the site or as warnings during the development server:

11:58 AM 2025-03-01 ERROR Validation failed for '/home/amartin/Develop/Rust/norgolith/my-site/content/index.norg'
→ Schema applied: 'index'
→ Constraint violation for field 'title': Exceeds max length 12

Common Errors and Fixes

Missing Required Field

Type Mismatch

Constraint Violation

Rule Condition Failed