Search by

adachsoft / content-blocks

Arkadiusz Adach

Validate Content Blocks documents against JSON Schema and referential integrity rules.

Package info

gitlab.com/a.adach/content-blocks

Issues

pkg:composer/adachsoft/content-blocks

Statistics

Installs: 21

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.2 2026-12-02 19:39 UTC

This package is auto-updated.

Last update: 2026-12-02 18:39:52 UTC


README

A PHP validator for Content Blocks 1.0 documents. Validation combines the bundled JSON Schema (draft 2020-12) with the referential rules from section 9 of the specification. It returns all detected issues together as immutable {code, path, message} values; path is an RFC 6901 JSON Pointer.

Features

  • Validates document structure against the bundled JSON Schema.
  • Checks cross-references, assets, anchors, headings, tables, image alternatives, and URL safety.
  • Collects all validation issues in one result with stable error codes and JSON Pointer paths.
  • Accepts decoded PHP data or the original JSON string.

Requirements

  • PHP 8.3 or later
  • Composer

Installation

composer require adachsoft/content-blocks

Usage

use AdachSoft\ContentBlocks\Validation\DocumentValidator;

$validator = new DocumentValidator();
$result = $validator->validate($document); // $document is an associative array

if (!$result->isValid()) {
    foreach ($result->errors() as $error) {
        printf("%s at %s: %s\n", $error->code, $error->path, $error->message);
    }
}

Use validateJson() when the original JSON string is available. It decodes JSON objects for JSON Schema validation and associative arrays for referential checks, preserving the difference between JSON objects and arrays (including an empty assets object).

$result = $validator->validateJson($json);

Both methods return a ValidationResult. isValid() reports whether validation succeeded, and errors() returns a ValidationErrorCollection with hasCode(string $code) and codes() helpers.

Integration

Create one DocumentValidator and call it at the boundary where documents enter your application, such as an HTTP request handler, import process, or content publishing workflow. Reject or report documents when isValid() returns false; use each error's code and JSON Pointer path to present actionable feedback. The validator does not persist or modify the document.

Error codes

  • schema — JSON Schema validation error.
  • duplicate_block_id — block ids are not unique.
  • duplicate_anchor — anchors are not unique.
  • unknown_asset — a referenced asset id is not declared.
  • asset_kind_mismatch — a referenced asset has the wrong kind.
  • unknown_source — a referenced source id is not declared.
  • missing_alt — an image does not have an accessible text alternative.
  • heading_level_skip — heading levels skip the permitted progression.
  • table_row_length — table rows have different cell counts.
  • unsafe_url — a URL uses a disallowed scheme or relative form.

Specification copy

The bundled specification and examples are copied from adachsoft/content-blocks-spec at commit 240761c, recorded in resources/spec/SPEC_COMMIT.