Explainer

JSON Schema Examples for Content Models

JSON Schema examples for content models: an author, an article, an FAQ list, and mixed blocks. All 2020-12, all tested with Ajv, with the real errors.

DT
Draftbase Team · Updated · 9 min read

JSON Schema is a JSON file that describes the shape of other JSON. It says which fields exist, what type each one is, which are required, and what values are allowed. For content, you write down what an article or an author must look like. Then you check every entry against it.

This guide walks through JSON Schema examples for real content models. They run from a simple author to a block list with oneOf. Each schema uses the current 2020-12 draft. We ran every example through Ajv 8.20.0 with ajv-formats 3.0.1. The errors shown are the ones it printed. For the modeling side of the same problem, see the content modeling hub.

What is JSON Schema?

JSON Schema is a set of words for checking JSON data. A schema is itself JSON. It uses keywords like type, properties, and required to set rules.

The current version is 2020-12. The one before it was 2019-09. Many tools still default to the older draft-07, which matters when you pick a validator. We'll come back to that.

A schema does two jobs. It checks data, so bad input fails before it reaches your app. And it explains data, so people and tools know what a field means. The title and description keywords exist for that second job.

A basic JSON Schema example: an author

Start small. Here's a schema for an author with a required name and slug and an optional bio:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Author",
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "slug": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "bio": { "type": "string", "maxLength": 500 }
  },
  "required": ["name", "slug"],
  "additionalProperties": false
}

A few things to notice. $schema says which draft the schema uses. properties lists the fields. required lists the ones that must be there. And additionalProperties: false blocks any field you didn't list. That catches typos like nmae before they're saved.

JSON Schema example: an article with rules

An article needs more rules. Here's a schema for the kind of guide you're reading now. It uses enum for fixed choices and length caps for SEO fields. It uses format for dates and links:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Cluster article",
  "type": "object",
  "properties": {
    "title": { "type": "string", "minLength": 1, "maxLength": 120 },
    "slug": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
    "pillar": { "enum": ["content-modeling", "frameworks", "headless-cms", "mcp"] },
    "type": { "enum": ["vs", "guide", "how-to"] },
    "metaDescription": { "type": "string", "minLength": 120, "maxLength": 160 },
    "content": { "type": "string", "minLength": 1 },
    "publishedAt": { "type": "string", "format": "date-time" },
    "canonicalUrl": { "type": "string", "format": "uri" }
  },
  "required": ["title", "slug", "pillar", "type", "metaDescription", "content"],
  "additionalProperties": false
}

We fed it an entry with five problems. It had no content and one extra field. The slug had spaces. The type was review. And publishedAt was next tuesday. Ajv listed all five:

/ must have required property 'content'
/ must NOT have additional properties
/slug must match pattern "^[a-z0-9]+(?:-[a-z0-9]+)*$"
/type must be equal to one of the allowed values
/publishedAt must match format "date-time"

Each error names the path and the rule that failed. That's enough to show a writer which field to fix.

JSON Schema example: an array of objects

Lists of small objects show up all over content: FAQs, how-to steps, links, specs. Here's an FAQ list with three to five items. Each item needs a question and an answer:

{
  "type": "array",
  "minItems": 3,
  "maxItems": 5,
  "items": {
    "type": "object",
    "properties": {
      "question": { "type": "string", "minLength": 1 },
      "answer": { "type": "string", "minLength": 1 }
    },
    "required": ["question", "answer"],
    "additionalProperties": false
  }
}

items applies one schema to every element. minItems and maxItems cap the count. You can nest this schema inside the article schema above. Or keep it on its own and point to it with $ref.

JSON Schema example: reusable blocks with oneOf

Some content is a list of mixed blocks: an image, then a quote, then another image. Each block kind has its own fields. $defs holds the shapes, and oneOf says each item must match exactly one:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "image": {
      "type": "object",
      "properties": {
        "kind": { "const": "image" },
        "src": { "type": "string", "format": "uri" },
        "alt": { "type": "string", "minLength": 1 }
      },
      "required": ["kind", "src", "alt"],
      "additionalProperties": false
    },
    "quote": {
      "type": "object",
      "properties": {
        "kind": { "const": "quote" },
        "text": { "type": "string" },
        "cite": { "type": "string" }
      },
      "required": ["kind", "text"],
      "additionalProperties": false
    }
  },
  "type": "array",
  "items": { "oneOf": [{ "$ref": "#/$defs/image" }, { "$ref": "#/$defs/quote" }] }
}

The kind field with const tells the shapes apart. An image block must have a src and an alt.

How do you validate JSON Schema in JavaScript?

We used Ajv, a JSON Schema validator for JavaScript. There's one catch. The default Ajv class targets draft-07. For 2020-12, you import a separate class:

import Ajv2020 from 'ajv/dist/2020.js';
import addFormats from 'ajv-formats';

const ajv = addFormats(new Ajv2020({ allErrors: true }));
const validate = ajv.compile(articleSchema);

if (!validate(entry)) {
  console.log(validate.errors);
}

We tried compiling a 2020-12 schema with the default class. It failed with no schema with key or ref "https://json-schema.org/draft/2020-12/schema". If you see that error, you've got the wrong class.

allErrors: true reports every problem, not just the first. That's what you want in a form, so people fix it all at once. Leave it off on a hot API path if speed matters more.

What does format actually check?

Less than you'd think. In 2020-12, format is an annotation by default. A validator may record that a string claims to be a date. It doesn't have to check it. Making format assert is opt-in.

The spec lists formats like date-time, date, time, duration, email, hostname, ipv4, ipv6, uri, uri-reference, uuid, and uri-template.

Ajv handles this in its own way. It ships with no format checks built in. You add them with the ajv-formats package. And in its default strict mode, Ajv won't quietly skip a format it doesn't know. Without ajv-formats, our article schema failed to compile:

unknown format "date-time" ignored in schema at path "#/properties/publishedAt"

That's a good failure. It's loud. The risk is a tool that skips format without a word. That could be another validator, or Ajv with strict mode off. Then next tuesday passes as a date. If a format matters, test that your setup rejects a bad value.

Do you need JSON Schema if you use TypeScript?

Yes, if data comes from outside your code. TypeScript types vanish when the code runs. They can't check a file, a form post, or an API reply. JSON Schema runs at that edge.

The two work well as a pair. Check the data with a schema when it comes in. After that, your types can trust it. Some tools write types from a schema, or a schema from types, so you only keep one by hand.

Pick one place as the source of truth. Two hand-kept copies drift, and then the check and the type disagree.

What breaks in production?

These are the JSON Schema mistakes that pass review and fail later.

Patterns aren't anchored. A pattern matches anywhere in the string unless you add ^ and $. A slug pattern of [a-z-]+ accepts Hello world! because ello matches. Always anchor.

additionalProperties: false breaks on new fields. It's great for catching typos. But add a field to the data before the schema and every entry fails. Ship the schema change first.

oneOf errors are noisy. When a block matches neither shape, Ajv reports errors for every branch. Our image block with a missing alt produced five errors. Only one was the real problem. Ajv's discriminator option fixes this. Add discriminator: { propertyName: "kind" } to the items schema, turn on the option, and the same input gives one error: must have required property 'alt'.

Drafts get mixed. A 2020-12 schema run through a draft-07 validator may fail, as ours did. Or it may read some keywords in the old way. Array items changed meaning in 2020-12, for one. Put $schema in every file and match the validator to it.

Comments sneak in. JSON Schema files are JSON, so they can't hold comments. Use description instead. If you've hit that wall before, can JSON have comments? covers the options.

How does a CMS template map to JSON Schema?

A CMS template is a schema with a UI on top. If you've read what a content type is, the idea is the same: a named shape with typed fields.

In Draftbase, each template field can carry rules that line up with JSON Schema keywords:

Draftbase field settingJSON Schema keyword
type: text or richText"type": "string"
type: number"type": "number"
type: boolean"type": "boolean"
required: truelisted in required
minLength, maxLengthminLength, maxLength
min, maxminimum, maximum
patternpattern
optionsenum
list: true"type": "array" with items
helpTextdescription

Most of these run on the server. When an entry is saved, the API checks number ranges, patterns, slugs, options, and list fields. Required fields are checked when the entry is published, so drafts can stay half done. Length limits are the exception. The editor caps maxLength in the input box, but the API doesn't check either length rule. If an agent or script writes through the API, check lengths yourself.

There's one gap worth knowing. A json field is checked to be real JSON, not a string. Draftbase doesn't check the shape inside it. An FAQ list in a json field could be missing an answer. The save would still pass.

That's where a JSON Schema earns its place next to a CMS. Write a schema for each json field's shape, like the FAQ array above. Run it in your build or your render code before you trust the data. The draftbase types output marks json fields as unknown for the same reason. Our post on TypeScript Record, satisfies, enums, and tuples covers that.

For the bigger picture of schemas, types, and APIs working together, see schema-driven content modeling.

Conclusion

These JSON Schema examples cover most content models: a plain object, an article with rules, an array of objects, and mixed blocks with oneOf. Use the 2020-12 draft, put $schema in every file, and validate with Ajv2020 plus ajv-formats. Anchor every pattern. Test that format really rejects bad values.

If your content lives in a CMS, let the templates hold the field rules. Keep JSON Schema for the shapes inside json fields. Draftbase templates give you required fields, ranges, patterns, and options with no schema to maintain. The Hobby plan is free. See pricing to model your own content.

Ship content that's built to be found

Draftbase generates schema, structured data, and a fast MDX editor for every post.

Frequently asked questions

What is the latest version of JSON Schema?

2020-12. The draft before it was 2019-09. Many tools still default to draft-07, so set $schema in each file and match your validator to it.

Does JSON Schema format validate dates and emails?

Not by default. In 2020-12, format is a note unless you turn checks on. Ajv needs the ajv-formats package to check date-time, email, or uri.

How do I validate a 2020-12 schema with Ajv?

Import Ajv2020 from ajv/dist/2020.js, not the default Ajv class. The default class targets draft-07 and fails on a 2020-12 $schema.

Are JSON Schema patterns anchored?

No. A pattern matches anywhere in the string. Add ^ at the start and $ at the end, or [a-z-]+ will accept Hello world!

Does a headless CMS replace JSON Schema?

Partly. Draftbase templates check required fields, ranges, patterns, and options. The shape inside a json field isn't checked, so use a schema there.

Working with this hands-on? Draftbase also has a free json to typescript.

Related reading

Go deeper on Content Modeling