Skip to content

Commit

Permalink
.wp-env.json schema: Fix schema and add unit tests (#63281)
Browse files Browse the repository at this point in the history
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: ajlende <ajlende@git.wordpress.org>
  • Loading branch information
3 people committed Jul 25, 2024
1 parent 0c2704c commit e434ec4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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 );
} );
} );

0 comments on commit e434ec4

Please sign in to comment.