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

.wp-env.json schema: Fix schema and add unit tests #63281

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion schemas/json/wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"$ref": "#/definitions/wpEnvProperties"
},
{
"type": "object",
"properties": {
"$schema": {
"type": "string"
Expand All @@ -91,10 +92,10 @@
"type": "object",
"patternProperties": {
"[a-zA-Z]": {
"type": "object",
"allOf": [
{ "$ref": "#/definitions/wpEnvProperties" },
{
"type": "object",
"propertyNames": {
"$ref": "#/definitions/wpEnvPropertyNames"
}
Expand All @@ -107,6 +108,7 @@
}
},
{
"type": "object",
"propertyNames": {
"anyOf": [
{
Expand Down
38 changes: 38 additions & 0 deletions test/integration/wp-env-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import Ajv from 'ajv';

/**
* Internal dependencies
*/
import wpEnvSchema from '../../schemas/json/wp-env.json';
import wpEnvJsonFile from '../../.wp-env.json';

describe( '.wp-env.json schema', () => {
const ajv = new Ajv( {
allowMatchingProperties: true,
} );

test( 'strictly adheres to the draft-07 meta schema', () => {
// Use ajv.compile instead of ajv.validateSchema to validate the schema
// because validateSchema only checks syntax, whereas, compile checks
// if the schema is semantically correct with strict mode.
// See https://github.com/ajv-validator/ajv/issues/1434#issuecomment-822982571
const result = ajv.compile( wpEnvSchema );

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

test( 'validates schema for .wp-env.json', () => {
// We want to validate the .wp-env.json file using the local schema.
const { $schema, ...metadata } = wpEnvJsonFile;

// we expect the $schema property to be present in the .wp-env.json file
expect( $schema ).toBeTruthy();

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

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