Repository navigation
Add bs_block_schema() to describe stored field values as JSON Schema - #199
Merged
Merged
Conversation
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.
Contributor
|
Merged and released in 7.7.13, included in 12440a6. The public |
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()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 inblockstudio.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
validate()uses (now shared throughdefinition_sets()), including fields of matching extensions, group prefixes, anonymous groups and tabs, repeater rows and the<id>__sizecompanion of files fields.additionalPropertiesisfalseon every level.enum(static options and populate options resolved at registration; noenumforfetch,{attributes.*}arguments or open palettes), attachment IDs,linkandiconobjects,attributespairs, repeaters withminItems/maxItems, custom field types with their registered attribute type.labelbecomestitle,helpbecomesdescription,defaultstays an annotation, and the block title is the schema title.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 nopropertieskeyword (an empty PHP array would encode as a JSON list).nullfor unmanaged blocks, likebs_validate_block(), and ablockstudio/blocks/attributes/schemafilter next to the existingattributes/validatefilter.Tests
Five tests in
BlockValidatorTest, on the existing fixture with every field type:rest_validate_value_from_schema()andbs_validate_block(), and an unknown key, an unknown option and too many rows fail both;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
BlockValidatorTestpasses excepttest_blocks_without_blockstudio_fields_do_not_check_top_level_keys, which fails the same way on unchangedmainin that environment. WithadditionalPropertiesswitched totrue, three of the new tests fail.composer csand PHPStan are clean for the changed source files, the PHPStan package type test covers the new stub, andnpm run build:llmonly 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.13is a guess for the next release.