adachsoft / content-blocks
Validate Content Blocks documents against JSON Schema and referential integrity rules.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
- opis/json-schema: ^2.6
Requires (Dev)
- adachsoft/php-code-style: ^0.9.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.