Skip to content

Add bs_block_schema() to describe stored field values as JSON Schema - #199

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/block-schema
Nov 7, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/block-schema

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

bs_validate_block() tells a tool afterwards what is wrong with stored values. This adds the counterpart before writing: bs_block_schema( $name ) returns a JSON Schema of the object a block stores in blockstudio.attributes, so code that generates block markup (an import, a REST route, an AI integration exposed through the Abilities API) can produce valid values on the first try instead of guessing keys and options and repairing them after validation.

What it returns

  • Built from the same compiled definitions validate() uses (now shared through definition_sets()), including fields of matching extensions, group prefixes, anonymous groups and tabs, repeater rows and the <id>__size companion of files fields. additionalProperties is false on every level.
  • Each field in the simplest stored form validation accepts: option values with enum (static options and populate options resolved at registration; no enum for fetch, {attributes.*} arguments or open palettes), attachment IDs, link and icon objects, attributes pairs, repeaters with minItems/maxItems, custom field types with their registered attribute type.
  • Keys with conflicting definitions are left out, because validation reports any value stored under them. A block field and an extension field with the same key keep only the option values both accept. In repeater rows the first field with a matching ID applies, as on render.
  • label becomes title, help becomes description, default stays an annotation, and the block title is the schema title.
  • Only keywords that rest_validate_value_from_schema() checks are used for constraints, so the result works as a REST or ability input schema as it is. A block without fields has no properties keyword (an empty PHP array would encode as a JSON list).
  • null for unmanaged blocks, like bs_validate_block(), and a blockstudio/blocks/attributes/schema filter next to the existing attributes/validate filter.

Tests

Five tests in BlockValidatorTest, on the existing fixture with every field type:

  • the shape of each field, including groups, tabs, repeaters, populate, extensions and conflicts;
  • a value covering every field passes both rest_validate_value_from_schema() and bs_validate_block(), and an unknown key, an unknown option and too many rows fail both;
  • empty and unmanaged blocks, extension-only blocks (core/paragraph), and the filter.

Run in WordPress Playground (PHP 8.4, WordPress 7.1) with PHPUnit started inside the request, since wp-env is not available here: the whole BlockValidatorTest passes except test_blocks_without_blockstudio_fields_do_not_check_top_level_keys, which fails the same way on unchanged main in that environment. With additionalProperties switched to true, three of the new tests fail. composer cs and PHPStan are clean for the changed source files, the PHPStan package type test covers the new stub, and npm run build:llm only adds the new sections.

Docs: a "Describing stored values" section in Rendering, the filter in PHP hooks, and the regenerated LLM files. No new translatable strings. @since 7.7.13 is a guess for the next release.

The schema is built from the definitions bs_validate_block() uses and describes each field in the simplest stored form validation accepts, so a value that matches it produces no findings. It uses only keywords rest_validate_value_from_schema() checks and works as a REST or ability input schema.
@Contributolo Contributolo added the enhancement New feature or request label Nov 6, 2026
@dnnsjsk
dnnsjsk merged commit 9bbe3fb into inline0:main Nov 7, 2026
@dnnsjsk

dnnsjsk commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Merged and released in 7.7.13, included in 12440a6.

The public bs_block_schema() helper, schema filter, and documentation are included. The companion PHPStan stub and hook-inventory updates are also synced to the PHPStan package's main branch. Review added regressions for shared field and repeater constraints, empty option sets, attachment IDs, and extension-only blocks. Full CI and distribution checks passed. Thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants