To validate JSON-LD in CI, run a script after the build that reads every HTML file, parses each JSON-LD block, checks it against a JSON Schema and exits with code 1 on any error. This post gives a Node script that does this with Ajv, tested locally, and says exactly what it does not check.
Usman Ishaq is a semantic SEO strategist and consultant, and the founder of Digital Vikingz. Markup is written from the visible page outward, and a template change can silently break it. A check in the pipeline catches that before it ships.
What does the check cover?
The script covers three things and ignores the rest.
- Covered: each built page has at least one JSON-LD block, each block is valid JSON, and each business or
Servicenode matches a JSON Schema that this project defines. - Not covered: schema.org vocabulary in general, eligibility for Google rich results, and whether the markup matches the visible text.
The schemas
Install the packages with npm install ajv ajv-formats, set "type": "module" in package.json and add "validate:jsonld": "node scripts/validate-jsonld.mjs out" under scripts. The two schemas set the required properties. schemas/business.schema.json:
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["@id", "name", "url", "telephone", "address"], "properties": { "@id": { "type": "string", "format": "uri" }, "name": { "type": "string", "minLength": 1 }, "url": { "type": "string", "format": "uri" }, "telephone": { "type": "string", "pattern": "^\\+?[0-9 ()-]{7,20}$" }, "address": { "type": "object", "required": ["@type", "streetAddress", "addressLocality", "addressCountry"], "properties": { "@type": { "const": "PostalAddress" } } } } } schemas/service.schema.json:
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["name", "provider"], "properties": { "name": { "type": "string", "minLength": 1 }, "provider": { "type": "object", "required": ["@id"], "properties": { "@id": { "type": "string", "format": "uri" } } } } } Ajv needs ajv-formats for "format": "uri". Without it, Ajv 8.20.0 throws unknown format "uri" ignored in schema when it compiles the schema.
The script
scripts/validate-jsonld.mjs walks the output folder, extracts JSON-LD blocks, flattens @graph and picks a schema by @type:
import { readFileSync, readdirSync, statSync } from "node:fs"; import { join } from "node:path"; import Ajv from "ajv"; import addFormats from "ajv-formats"; const ajv = new Ajv({ allErrors: true }); addFormats(ajv); const load = (f) => JSON.parse(readFileSync(new URL(`../schemas/${f}`, import.meta.url))); const validators = { business: ajv.compile(load("business.schema.json")), service: ajv.compile(load("service.schema.json")), }; const BUSINESS = new Set(["LocalBusiness", "Plumber", "Electrician", "HVACBusiness"]); const htmlFiles = (dir) => readdirSync(dir).flatMap((n) => { const p = join(dir, n); return statSync(p).isDirectory() ? htmlFiles(p) : p.endsWith(".html") ? [p] : []; }); const blockRe = /<script[^>]*type=["']application\/ld\+json["'][^>]*>([\s\S]*?)<\/script>/gi; let failures = 0; const fail = (file, msg) => { failures++; console.log(`FAIL ${file}: ${msg}`); }; for (const file of htmlFiles(process.argv[2] ?? "out")) { const blocks = [...readFileSync(file, "utf8").matchAll(blockRe)].map((m) => m[1]); if (blocks.length === 0) { fail(file, "no JSON-LD block found"); continue; } const before = failures; for (const raw of blocks) { let data; try { data = JSON.parse(raw); } catch (e) { fail(file, `invalid JSON (${e.message})`); continue; } for (const node of [data].flat().flatMap((d) => d["@graph"] ?? [d])) { const types = [node["@type"]].flat(); const kind = types.some((t) => BUSINESS.has(t)) ? "business" : types.includes("Service") ? "service" : null; if (!kind) continue; if (!validators[kind](node)) for (const e of validators[kind].errors) fail(file, `${kind} ${e.instancePath || "/"} ${e.message}`); } } if (failures === before) console.log(`PASS ${file}`); } process.exit(failures ? 1 : 0); A regular expression extracts the blocks, which is enough for generated HTML. A page with no block fails, because a template change that drops the markup is a regression this check should catch.
What does a failing run look like?
This is real output from a test folder with a good page, a page with a wrong @id, no telephone and an incomplete address, a page with a trailing comma in its JSON and a page with no block:
FAIL out/blog/bad.html: business / must have required property 'telephone' FAIL out/blog/bad.html: business /@id must match format "uri" FAIL out/blog/bad.html: business /address must have required property 'addressLocality' FAIL out/blog/bad.html: business /address must have required property 'addressCountry' FAIL out/broken.html: invalid JSON (Expected double-quoted property name in JSON at position 33 (line 1 column 34)) PASS out/good.html FAIL out/none.html: no JSON-LD block found The exit code was 1. With only the good page, the output was one PASS line and the exit code was 0. The tests ran on Node 26.13.0 with ajv 8.20.0 and ajv-formats 3.0.1. The workflow file below was not run on GitHub.
How do you run it in CI?
Run the script after the build, on every pull request. This GitHub Actions file assumes a static export into out:
name: jsonld on: pull_request jobs: validate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: 22 } - run: npm ci - run: npm run build - run: npm run validate:jsonld What should still be checked by hand?
The schemas test shape. Google's Rich Results Test and the Schema Markup Validator test against their own rules, and the script does not replace either. Compare the markup with the visible page by eye as well.
For the wider idea, read entities on a business website. The fixed-scope project that writes and checks structured data for a business is Technical SEO and Schema.
About the author
Usman Ishaq is a semantic SEO strategist and consultant, and the founder of Digital Vikingz. He plans websites around entities, attributes and values, then builds topical maps, briefs, pages, internal links and schema so businesses rank in Google and get named by ChatGPT, Gemini and Perplexity.
