Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Validate block & theme json #57374

Merged
merged 10 commits into from
Apr 3, 2024
7 changes: 6 additions & 1 deletion lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@
"background": "#32373c"
},
"spacing": {
"padding": "calc(0.667em + 2px) calc(1.333em + 2px)"
"padding": {
"top": "calc(0.667em + 2px)",
"right": "calc(1.333em + 2px)",
"bottom": "calc(0.667em + 2px)",
"left": "calc(1.333em + 2px)"
}
},
"typography": {
"fontSize": "inherit",
Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/src/blocks/widget-area/block.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"name": "core/widget-area",
"title": "Widget Area",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"category": "widgets",
"attributes": {
"id": {
Expand Down
1 change: 1 addition & 0 deletions packages/widgets/src/blocks/widget-group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "core/widget-group",
"title": "Widget Group",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"category": "widgets",
"attributes": {
"title": {
Expand Down
35 changes: 13 additions & 22 deletions test/integration/blocks-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
*/
import Ajv from 'ajv-draft-04';
import glob from 'fast-glob';
import path from 'path';

/**
* Internal dependencies
*/
import blockSchema from '../../schemas/json/block.json';

describe( 'block.json schema', () => {
const blockFolders = glob.sync( 'packages/block-library/src/*', {
onlyDirectories: true,
ignore: [ 'packages/block-library/src/utils' ],
} );
const testData = blockFolders.map( ( blockFolder ) => [
'core/' + path.basename( blockFolder ),
path.join( blockFolder, 'block.json' ),
] );
const jsonFiles = glob.sync(
[ 'packages/*/src/**/block.json', '{lib,phpunit,test}/**/block.json' ],
{ onlyFiles: true }
);
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure I'm following along: we're expanding the search to block.json files anywhere within packages or tests, so that we pick up the block.json files in the widgets and edit-widgets packages, and anywhere else we might add them in the future, so we don't need to remember to explicitly add those directories? Sounds good to me 👍

const ajv = new Ajv();

test( 'strictly adheres to the draft-04 meta schema', () => {
Expand All @@ -31,22 +26,18 @@ describe( 'block.json schema', () => {
expect( result.errors ).toBe( null );
} );

test( 'found block folders', () => {
expect( blockFolders.length ).toBeGreaterThan( 0 );
test( 'found block.json files', () => {
expect( jsonFiles.length ).toBeGreaterThan( 0 );
} );

test.each( testData )(
'validates schema for `%s`',
( blockName, filepath ) => {
// We want to validate the block.json file using the local schema.
const { $schema, ...blockMetadata } = require( filepath );
test.each( jsonFiles )( 'validates schema for `%s`', ( filepath ) => {
// We want to validate the block.json file using the local schema.
const { $schema, ...blockMetadata } = require( filepath );

expect( $schema ).toBe( 'https://schemas.wp.org/trunk/block.json' );
expect( $schema ).toBe( 'https://schemas.wp.org/trunk/block.json' );

const result =
ajv.validate( blockSchema, blockMetadata ) || ajv.errors;
const result = ajv.validate( blockSchema, blockMetadata ) || ajv.errors;

expect( result ).toBe( true );
}
);
expect( result ).toBe( true );
} );
} );
20 changes: 20 additions & 0 deletions test/integration/theme-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
* External dependencies
*/
import Ajv from 'ajv-draft-04';
import glob from 'fast-glob';

/**
* Internal dependencies
*/
import themeSchema from '../../schemas/json/theme.json';

describe( 'theme.json schema', () => {
const jsonFiles = glob.sync(
[ 'packages/*/src/**/theme.json', '{lib,phpunit,test}/**/theme.json' ],
{ onlyFiles: true }
);
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, we're now saying, any theme.json file anywhere within any of the packages should conform to the https://schemas.wp.org/trunk/theme.json schema. That also sounds good to me 👍

This is just thinking out loud, but I suppose there could theoretically be a use case for referring to a different schema, i.e. a relative path, or something? That seems pretty edge-casey to me, though, and if someone is attempting to do that, then hopefully the schema validation error here will signal to them what's going on, so I don't that's a blocker here.

const ajv = new Ajv( {
// Used for matching unknown blocks without repeating core blocks names
// with patternProperties in settings.blocks and settings.styles
Expand All @@ -24,4 +29,19 @@ describe( 'theme.json schema', () => {

expect( result.errors ).toBe( null );
} );

test( 'found theme.json files', () => {
expect( jsonFiles.length ).toBeGreaterThan( 0 );
} );

test.each( jsonFiles )( 'validates schema for `%s`', ( filepath ) => {
// We want to validate the block.json file using the local schema.
const { $schema, ...metadata } = require( filepath );

expect( $schema ).toBe( 'https://schemas.wp.org/trunk/theme.json' );

const result = ajv.validate( themeSchema, metadata ) || ajv.errors;

expect( result ).toBe( true );
} );
} );
Loading