Repository navigation
Add bs_validate_block() to check stored field values - #137
Merged
Merged
Conversation
bs_validate_block( $name, $attrs ) checks the values a block stores against the fields of the block and of every matching extension, the way the render path reads them, and returns findings with a stable code, the storage path, the source of the field, a message, the stored value and what was expected. It returns null when no fields apply. The checks read the built attributes the attribute builder registered on the block type and on the matching extensions, so storage keys, groups, tabs and repeater rows are the renderer's own. Top-level values first go through rest_validate_value_from_schema() as WordPress does before render. Options are looked up with Block::get_option_value() and multiple values read with Block::multiple_option_values(), both shared with the renderer (two helpers made public and marked internal). Adds the blockstudio/blocks/attributes/validate filter, docs under Inspecting Blocks, the hook in the PHPStan hook inventory, German strings and unit tests that assert each finding next to what do_blocks() hands the template for the same value. Refs inline0#135
dnnsjsk
added a commit
that referenced
this pull request
Oct 19, 2026
Port PR #138 and incorporate the render-path corrections from PR #137 into the existing #135 validator. Preserve read-only validation, original stored values, palette strings, dynamic-option exclusions and the CLI. Document repeater initial rows and storage precedence, with unit/render and CI-only HTTP coverage. Co-authored-by: Contributolo <[email protected]>
Contributor
|
Integrated and released in 7.7.5 (86a311c1). The release branch already had the read-only validator and CLI implementation for #135, so I incorporated the additional render-path checks into that implementation: WordPress schema-dropped values, extension values outside nested storage, first-definition precedence in repeater rows, HTML attribute-pair validation, and forwarded/root bookkeeping attributes. Added regression coverage for those paths, including renderer comparisons. The validator remains opt-in and read-only, keeps the CLI report contract, and does not rerun dynamic option resolvers. full CI passed all 14 jobs. PHPStan 0.1.10 also provides the helper's typed finding reports and the link-field |
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.
Closes #135.
Adds
bs_validate_block( $name, $attrs ). It checks the values a block stores against the fields of the block and of every matching extension, the way the render path reads them.Each finding:
code,path(storage path, row index for repeater rows),source(block,extension, or null),message(translatable),value(as stored),expected(where it applies). A filterblockstudio/blocks/attributes/validate($findings, $name, $attrs) can change them.invalid_containerattrs.blockstudio, itsattributes, ordisabledhas the wrong typeunknown_field__sizekeysshadowed_valueattrsand nested, with different valuesBlock::transform())extension_top_levelattrsof a Blockstudio blockExtensions::render_blocks()reads onlyblockstudio.attributesdefinition_conflictwrong_typeWP_Block_Type::prepare_attributes_for_render()unknown_optionselect,radioorcheckboxvalue is not an optionBlock::get_option_value()knowsfalse, or dropped from the listunknown_attachmentfilesID (return format other thanid) names no stored post, or no attachment forurlfalseHow it stays in line with the renderer
WP_Block_Type->attributesof the block and->attributesof each match fromExtensions::get_matches(), later extensions replacing earlier ones, as inBlock::extension_attributes()andExtensions::render_blocks(). Storage keys, flattened id-less groups and tabs, dropped nested groups and row attributes are what the attribute builder registered. I did not move the walk out ofWpml_Config::definitions()as the issue suggested: the built map is the renderer's own source, and Declare files fields as attachment IDs inwp bs wpml config#134 toucheswpml-config.phpat the same time.rest_validate_value_from_schema()against the registered attribute, as WordPress does before render. That is why a plain"b"at the top level of aradiois reported: the attribute is registered as an object and WordPress drops it, so the template gets the default (asserted intest_top_level_values).Block::unhandled_attributes().$is_valuetest astransform_attributes(); fields indisabledare skipped, also single IDs of afileslist.Block::multiple_option_values(), every option throughBlock::get_option_value()with the field's return format andpopulate. To share them,is_multiple_option_field()andmultiple_option_values()are now public and marked@internal. Only aselectwithpopulate.typefetchis skipped, because only there the value is passed through.idequals the key, likeBlock::attribute_definition().Field_Type_Registry::build_attribute()).Differences from the issue, and two things I noticed
populatewith{attributes.…}is checked, not skipped as the issue said. The build does not resolve those options (Select_Field_Handler::build_options()), and nothing re-populates on render, soget_option_value()only knows the static options and a value picked in the editor rendersfalse(asserted intest_populated_options). The validator reports what the renderer does. If that render result is not intended, it is a separate fix.min/maxare not checked. The built repeater attribute carries neither (Container_Field_Handler::build_repeater()). A side effect: the min-row default inBlock::transform()(block.php:2100,! empty( $v['min'] )) never applies, so a repeater withmin: 1and no stored rows rendersfalserather than one default row. Fixed separately in Render min repeater rows when no rows are stored #138, since it changes what templates receive.color/gradientoptions are not checked: the renderer does not look them up, an unknown palette value renders as stored. Only the shape is checked.wp bs blocks validatecan follow if you want it.Docs and housekeeping
docs/docs/blocks/rendering.md: "Validating stored values" under "Inspecting Blocks", replacing the sentence that there is no validator.docs/docs/blocks/hooks/php.md: the filter.bs_validate_blockin the collision list inplaceholders.php; the filter inHookCallbackRule::KNOWN_HOOKSand its runtime-families fixture; German strings inde_DEandde_DE_formal.@since 7.7.5is a placeholder, please adjust.Tests
tests/unit/block/BlockValidatorTest.php, 16 tests. Each case asserts the finding and whatdo_blocks()hands the template for the same stored value. The test block covers named and id-less groups, tabs, a group with an ID inside a group and inside rows, a message field, a repeater with a duplicate row ID,populatefrom a filter and with{attributes.…}, a custom field type, anattributesfield, a real attachment, and extensions on the block and oncore/paragraph.Run without Docker in WordPress Playground (PHP 8.4, WordPress latest, plugin and test theme mounted as in
tests/wp-env), PHPUnit started inside the request:BlockValidatorTest: 16 of 16 green.block) fails the same way on unchangedmain; suites that spawn separate processes cannot run there.composer cs, PHPStan (phpstan.neon.dist, changed files),npm run check(includesi18n:checkanddocs:check), andHookCallbackRuleTestinpackages/phpstan: clean.