Skip to content

Commit

Permalink
Validate more block.json and theme.json files (WordPress#57374)
Browse files Browse the repository at this point in the history
Validate block.json and theme.json files.
Fix a few existing minor schema violations.

Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>
  • Loading branch information
6 people authored and cbravobernal committed Apr 9, 2024
1 parent a7f825c commit 2fc6fef
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
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",
"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",
"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 }
);
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 }
);
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 );
} );
} );

0 comments on commit 2fc6fef

Please sign in to comment.