Keeping content consistent across files in a collection

Last modified: June 1st, 2023

When you edit a file in a collection with a schema defined, the front matter or data is updated to match the configured schema. This allows you to keep your files consistent across a collection as they are edited by adding, removing, or reordering inputs in the schema file.

Follow these steps to create and configure a schema for your collection.

Here’s an example schema file and the required configuration for a posts collection:

schemas/post.md
copied
---
title:
author:
image:
---
cloudcannon.config.yaml
copied
collections_config:
  posts:
    schemas:
      default:
        path: schemas/post.md
cloudcannon.config.json
copied
{
  "collections_config": {
    "posts": {
      "schemas": {
        "default": {
          "path": "schemas/post.md"
        }
      }
    }
  }
}
cloudcannon.config.js
copied
module.exports = {
  collections_config: {
    posts: {
      schemas: {
        default: {
          path: "schemas/post.md"
        }
      }
    }
  }
};

Existing files are not processed before being loaded into the editor if a schema is not configured on the collection.

Since multiple schemas can be defined, the _schema field inside the file being edited is used to find the correct one. The _schema field is automatically added when creating files for a collection with schemas within CloudCannon.

The default schema is used if _schema does not exist. You can change the field from _schema with the schema_key option when defining your collections.

Example#

In this example, we have the schema defined above for the posts collection, and we have the following post that was created when the schema had different fields:

content/posts/my-existing-post.md
copied
---
_schema: default
title: My post
category: news
---
This is the content of my post!

After editing this post there are these changes:

  1. category was removed
  2. author and image were added
  3. All inputs were reordered to match the schema
content/posts/my-existing-post.md
copied
---
_schema: default
title: My updated post
author: /authors/jane
image: /images/posts/header.png
---
This is the updated content of my post!

You can control whether inputs are reordered, removed and/or hidden with the reorder_inputs, remove_empty_inputs and hide_extra_inputs schema options.

Related Articles

Open in a new tab