-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
a551b1c
fdbab6f
5803ec5
5b52e53
f166d3c
a744f0e
7c8284f
435e4bf
3d47915
e97a911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||||
"apiVersion": 3, | ||||
"name": "core/widget-group", | ||||
"title": "Widget Group", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
"category": "widgets", | ||||
"attributes": { | ||||
"title": { | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
const ajv = new Ajv(); | ||
|
||
test( 'strictly adheres to the draft-04 meta schema', () => { | ||
|
@@ -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 ); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, we're now saying, any 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 | ||
|
@@ -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 ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gutenberg/packages/edit-widgets/src/blocks/widget-area/index.js
Line 16 in db5bbf2