Repository navigation
Report unknown top-level attributes in bs_validate_block() - #194
Merged
dnnsjsk merged 1 commit intoNov 2, 2026
Merged
Conversation
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.
Contributor
|
Merged and released in Blockstudio 7.7.11. The full CI and distribution checks passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bs_validate_block()andwp bs blocks validatecheckattrs.blockstudioand the declared top-level fields. Any other top-level key is ignored. A block stored as(the
blockstudiowrapper 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 inblock.jsonandfield.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:expectedunknown_attributeblockstudio.missing_containerattributes,disabled,nameoreditoris stored at the top level andattrs.blockstudiois missing. The message names the wrapper.blockstudio/<key>, the path it belongs at.If
blockstudiois present, a strayattributesnext to it is a plainunknown_attribute.Existing codes, paths, finding keys and the
blockstudio/blocks/attributes/validatefilter are unchanged. The new findings come before the container and value findings of the same block, and they are also reported whenblockstudiohas the wrong type.What is not reported
The set of known keys is the same one the renderer works with:
WP_Block_Type->attributesof the registered block: its fields plus whatever WordPress added forsupports(className,anchor,style,align, ...).WP_Block_Type::GLOBAL_ATTRIBUTES(lock,metadata). WordPress adds them inWP_Block_Type::set_props(), so a block type registered from an object does not have them in->attributes.Block::transform()also reads at the top level (they already produceextension_top_level).blockstudio, the bookkeeping keys and the HTML attributes Blockstudio forwards to the root element (id,title,data-*,aria-*,html-*, ...), throughBlock::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
nulland[]keep their meaning.Left out
Unknown keys inside the
blockstudioobject ({"blockstudio":{"attribute":{...}}},disbled) are still not reported. The keys Blockstudio reads there areattributes,disabled,nameandeditor, 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 validategoes throughBlock_Validator::blocks(), so the CLI reports both without a change tocli.php.npm run build:llm).languages/blockstudio.potand thede_DEandde_DE_formalfiles (.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 neednpm run i18nandnpm run i18n:translate.Tests
tests/unit/validation/BlockValidatorTest.php, 7 new tests:expectedand never reaches the template;attributesanddisabledwithout the wrapper givemissing_containerwithblockstudio/<key>; the same values with a wrapper present giveunknown_attribute; the template gets nothing from the unwrapped values;invalid_containeror value findings;className,anchor,lock,metadata,style, forwarded HTML attributes,key, declared fields, group children, extension fields andblockstudioproduce no new finding;core/paragraphwith an extension,core/group);Block_Validator::blocks()(thewp bs blocks validatepath) reportsmissing_containerwith 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/phpcson the changed class, PHPStan on the changed class,npm run docs:checkandnpm run format:check: clean.tests/e2e/cli.tsvalidatesblockstudio/type-selectwith onlyblockstudio.attributes, so I expect it to be unaffected.