Skip to content

Report unknown top-level attributes in bs_validate_block() - #194

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/validate-unknown-block-attributes
Nov 2, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/validate-unknown-block-attributes

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

bs_validate_block() and wp bs blocks validate check attrs.blockstudio and the declared top-level fields. Any other top-level key is ignored. A block stored as

<!-- wp:my-theme/steps {"attributes":{"title":"Hello"}} /-->

(the blockstudio wrapper forgotten), or with a mistyped field ID ({"titl":"Hello"}), validates as [] and renders without the value. Nothing says why: the key is not one of the values the template receives (the tests assert this for the new cases), so the block comes out empty.

I searched the issues and pull requests for this (validate, unknown key, typo, silently, ignored) and found nothing that covers stored values. #190 and #192 report unknown properties in block.json and field.json, which is the definition side. #90 and #91 made declared fields readable at the top level, which is why a top-level key can be valid at all.

What changes

For blocks that have Blockstudio fields, Block_Validator::validate() now reports top-level keys the render path does not read:

Code When expected
unknown_attribute A top-level key is not a registered attribute of the block, not a field of a matching extension, and not blockstudio. The attribute names the render path reads.
missing_container attributes, disabled, name or editor is stored at the top level and attrs.blockstudio is missing. The message names the wrapper. blockstudio/<key>, the path it belongs at.

If blockstudio is present, a stray attributes next to it is a plain unknown_attribute.

Existing codes, paths, finding keys and the blockstudio/blocks/attributes/validate filter are unchanged. The new findings come before the container and value findings of the same block, and they are also reported when blockstudio has the wrong type.

What is not reported

The set of known keys is the same one the renderer works with:

  • WP_Block_Type->attributes of the registered block: its fields plus whatever WordPress added for supports (className, anchor, style, align, ...).
  • WP_Block_Type::GLOBAL_ATTRIBUTES (lock, metadata). WordPress adds them in WP_Block_Type::set_props(), so a block type registered from an object does not have them in ->attributes.
  • The fields of matching extensions, which Block::transform() also reads at the top level (they already produce extension_top_level).
  • blockstudio, the bookkeeping keys and the HTML attributes Blockstudio forwards to the root element (id, title, data-*, aria-*, html-*, ...), through Block::unhandled_attributes().

Blocks that carry only extension fields (a core block with an extension) are not checked for top-level keys. Blockstudio does not own their attributes, so null and [] keep their meaning.

Left out

Unknown keys inside the blockstudio object ({"blockstudio":{"attribute":{...}}}, disbled) are still not reported. The keys Blockstudio reads there are attributes, disabled, name and editor, but I did not want to widen this change into a second whitelist. Happy to add it here or in a follow-up if you prefer.

Docs and housekeeping

  • docs/docs/blocks/rendering.md: both codes in the findings table and a paragraph on top-level keys. wp bs blocks validate goes through Block_Validator::blocks(), so the CLI reports both without a change to cli.php.
  • LLM context rebuilt (npm run build:llm).
  • languages/blockstudio.pot and the de_DE and de_DE_formal files (.po, .mo, .l10n.php) have the two new messages. The other locales are not touched, as in Add bs_validate_block() to check stored field values #137. They need npm run i18n and npm run i18n:translate.
  • No changelog entry, per the changelog policy.

Tests

tests/unit/validation/BlockValidatorTest.php, 7 new tests:

  • a mistyped key is reported with the registered names in expected and never reaches the template;
  • attributes and disabled without the wrapper give missing_container with blockstudio/<key>; the same values with a wrapper present give unknown_attribute; the template gets nothing from the unwrapped values;
  • the new findings do not hide invalid_container or value findings;
  • className, anchor, lock, metadata, style, forwarded HTML attributes, key, declared fields, group children, extension fields and blockstudio produce no new finding;
  • blocks without Blockstudio fields are not checked (core/paragraph with an extension, core/group);
  • Block_Validator::blocks() (the wp bs blocks validate path) reports missing_container with the block path.

Run without Docker (PHP 8.5, WordPress 7.1.3 on SQLite, plugin symlinked, PHPUnit started from the CLI against that install):

  • BlockValidatorTest: 69 of 69 green. Before the change the file had 62 tests, all green. The new tests fail against the unchanged validator (5 of the 7 fail, the other 2 assert that nothing new is reported).
  • vendor/bin/phpcs on the changed class, PHPStan on the changed class, npm run docs:check and npm run format:check: clean.
  • Not run: the full PHP suites and the Playwright specs, which need wp-env. tests/e2e/cli.ts validates blockstudio/type-select with only blockstudio.attributes, so I expect it to be unaffected.

A Blockstudio block only reads its registered attributes, the fields of
matching extensions and the blockstudio object. Any other top-level key,
such as a mistyped field ID or a values object stored without its
blockstudio wrapper, is dropped and the block renders without the value,
but the validator reported no problem.

Add two findings for blocks that have Blockstudio fields:
unknown_attribute lists the attribute names the render path reads, and
missing_container names blockstudio/<key> when attributes, disabled, name
or editor is stored at the top level and blockstudio is missing. Reserved
keys, forwarded HTML attributes and the lock and metadata attributes
WordPress adds to every block are not reported. wp bs blocks validate
uses the same function and reports both.
@dnnsjsk
dnnsjsk merged commit 2d02366 into inline0:main Nov 2, 2026
@dnnsjsk

dnnsjsk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged and released in Blockstudio 7.7.11. The full CI and distribution checks passed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants