Skip to content

Add bs_validate_block() to check stored field values - #137

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/validate-block
Oct 19, 2026
Merged

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

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

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.

$findings = bs_validate_block( $block['blockName'], $block['attrs'] );
// null: no fields apply. []: valid. Otherwise a list of findings.

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 filter blockstudio/blocks/attributes/validate ($findings, $name, $attrs) can change them.

Code When Render path
invalid_container attrs.blockstudio, its attributes, or disabled has the wrong type not a supported shape
unknown_field no field of the block, its extensions, or the repeater rows stores the key unset at block level; passed through, but read by no field, in rows and for __size keys
shadowed_value a declared ID is set at the top level of attrs and nested, with different values nested wins (Block::transform())
extension_top_level an extension field is set at the top level of attrs of a Blockstudio block the template gets it, Extensions::render_blocks() reads only blockstudio.attributes
definition_conflict block and extension, or two row fields of one repeater, define a key with fields of different shapes each path uses its own definition; in rows the first one
wrong_type the value has no shape the field stores, or WordPress drops a top-level value whose type does not match the registered attribute varies; dropped by WP_Block_Type::prepare_attributes_for_render()
unknown_option a select, radio or checkbox value is not an option Block::get_option_value() knows false, or dropped from the list
unknown_attachment a files ID (return format other than id) names no stored post, or no attachment for url false

How it stays in line with the renderer

  • It reads the built attributes, not the raw definitions: WP_Block_Type->attributes of the block and ->attributes of each match from Extensions::get_matches(), later extensions replacing earlier ones, as in Block::extension_attributes() and Extensions::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 of Wpml_Config::definitions() as the issue suggested: the built map is the renderer's own source, and Declare files fields as attachment IDs in wp bs wpml config #134 touches wpml-config.php at the same time.
  • Top-level values first go through 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 a radio is reported: the attribute is registered as an object and WordPress drops it, so the template gets the default (asserted in test_top_level_values).
  • Keys a Blockstudio block forwards to its root element or keeps as bookkeeping are skipped through Block::unhandled_attributes().
  • Empty values use the same $is_value test as transform_attributes(); fields in disabled are skipped, also single IDs of a files list.
  • Multiple selects and checkboxes go through Block::multiple_option_values(), every option through Block::get_option_value() with the field's return format and populate. To share them, is_multiple_option_field() and multiple_option_values() are now public and marked @internal. Only a select with populate.type fetch is skipped, because only there the value is passed through.
  • Row keys use the first row attribute whose id equals the key, like Block::attribute_definition().
  • Custom field types are checked against the attribute type they declare (Field_Type_Registry::build_attribute()).

Differences from the issue, and two things I noticed

  • populate with {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, so get_option_value() only knows the static options and a value picked in the editor renders false (asserted in test_populated_options). The validator reports what the renderer does. If that render result is not intended, it is a separate fix.
  • Repeater min/max are not checked. The built repeater attribute carries neither (Container_Field_Handler::build_repeater()). A side effect: the min-row default in Block::transform() (block.php:2100, ! empty( $v['min'] )) never applies, so a repeater with min: 1 and no stored rows renders false rather than one default row. Fixed separately in Render min repeater rows when no rows are stored #138, since it changes what templates receive.
  • color / gradient options are not checked: the renderer does not look them up, an unknown palette value renders as stored. Only the shape is checked.
  • WP-CLI is left out to keep the surface small; wp bs blocks validate can 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.
  • LLM context rebuilt; bs_validate_block in the collision list in placeholders.php; the filter in HookCallbackRule::KNOWN_HOOKS and its runtime-families fixture; German strings in de_DE and de_DE_formal.
  • @since 7.7.5 is a placeholder, please adjust.

Tests

tests/unit/block/BlockValidatorTest.php, 16 tests. Each case asserts the finding and what do_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, populate from a filter and with {attributes.…}, a custom field type, an attributes field, a real attachment, and extensions on the block and on core/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.
  • Every other unit directory before and after the change: same results. What fails in Playground (some tests in block-tags, build, islands, register, runtime, settings, site-templates, storage, and three classes in block) fails the same way on unchanged main; suites that spawn separate processes cannot run there.
  • composer cs, PHPStan (phpstan.neon.dist, changed files), npm run check (includes i18n:check and docs:check), and HookCallbackRuleTest in packages/phpstan: clean.

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]>
@dnnsjsk
dnnsjsk merged commit 86a311c into inline0:main Oct 19, 2026
@dnnsjsk

dnnsjsk commented Oct 19, 2026

Copy link
Copy Markdown
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 url correction.

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.

Public API to validate stored field values against a block's fields

2 participants