From d0c0de709fbd4d18b313eba9c38923a8f73e892a Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Mon, 19 Sep 2022 13:02:26 +0800 Subject: [PATCH 01/10] Add failing theme.json schema test --- test/integration/theme-schema.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/integration/theme-schema.test.js diff --git a/test/integration/theme-schema.test.js b/test/integration/theme-schema.test.js new file mode 100644 index 00000000000000..e721e5eca05e46 --- /dev/null +++ b/test/integration/theme-schema.test.js @@ -0,0 +1,23 @@ +/** + * External dependencies + */ +import Ajv from 'ajv-draft-04'; + +/** + * Internal dependencies + */ +import themeSchema from '../../schemas/json/theme.json'; + +describe( 'theme.json schema', () => { + const ajv = new Ajv( { + // Used for matching unknown blocks without repeating core blocks names + // with patternProperties in settings.blocks and settings.styles + allowMatchingProperties: true, + } ); + + it( 'compiles in strict mode', async () => { + const result = ajv.compile( themeSchema ); + + expect( result.errors ).toBe( null ); + } ); +} ); From 5ed79cd1cde532304ad28fdfa85d32588bc18732 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:26:20 +0800 Subject: [PATCH 02/10] Fix missing property object types --- schemas/json/theme.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 58cfe1ea001f08..903229cef49772 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -8,6 +8,7 @@ "reference": "https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-json/" }, "settingsPropertiesAppearanceTools": { + "type": "object", "properties": { "appearanceTools": { "description": "Setting that enables the following UI tools:\n\n- border: color, radius, style, width\n- color: link\n- spacing: blockGap, margin, padding\n- typography: lineHeight", @@ -26,6 +27,7 @@ } }, "settingsPropertiesBorder": { + "type": "object", "properties": { "border": { "description": "Settings related to borders.", @@ -57,6 +59,7 @@ } }, "settingsPropertiesColor": { + "type": "object", "properties": { "color": { "description": "Settings related to colors.", @@ -186,6 +189,7 @@ } }, "settingsPropertiesLayout": { + "type": "object", "properties": { "layout": { "description": "Settings related to layout.", @@ -205,6 +209,7 @@ } }, "settingsPropertiesSpacing": { + "type": "object", "properties": { "spacing": { "description": "Settings related to spacing.", @@ -305,6 +310,7 @@ } }, "settingsPropertiesTypography": { + "type": "object", "properties": { "typography": { "description": "Settings related to typography.", @@ -515,6 +521,7 @@ } }, "settingsPropertiesCustom": { + "type": "object", "properties": { "custom": { "description": "Generate custom CSS custom properties of the form `--wp--custom--{key}--{nested-key}: {value};`. `camelCased` keys are transformed to `kebab-case` as to follow the CSS property naming schema. Keys at different depth levels are separated by `--`, so keys should not include `--` in the name.", @@ -570,11 +577,13 @@ "$ref": "#/definitions/settingsPropertiesComplete" }, "core/button": { + "type": "object", "allOf": [ { "$ref": "#/definitions/settingsPropertiesAppearanceTools" }, { + "type": "object", "properties": { "border": { "description": "Settings related to borders.\nGutenberg plugin required.", From 968f0df7299f7b30b515d286ca1e7c46e135b641 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:30:58 +0800 Subject: [PATCH 03/10] Remove type conflict in fontWeight --- schemas/json/theme.json | 1 - 1 file changed, 1 deletion(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 903229cef49772..4c3f30ab90bdbb 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -433,7 +433,6 @@ }, "fontWeight": { "description": "List of available font weights, separated by a space.", - "type": "string", "default": "400", "oneOf": [ { From 98d948f04ec02518a55221c5b09f1c8fee795a62 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:36:50 +0800 Subject: [PATCH 04/10] Refactor fontSizes.fluid into non-union type --- schemas/json/theme.json | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 4c3f30ab90bdbb..7f57a01244114b 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -380,18 +380,25 @@ }, "fluid": { "description": "Specifics the minimum and maximum font size value of a fluid font size. Set to `false` to bypass fluid calculations and use the static `size` value.", - "type": [ "object", "boolean" ], - "properties": { - "min": { - "description": "A min font size for fluid font size calculations in px, rem or em.", - "type": "string" + "oneOf": [ + { + "type": "object", + "properties": { + "min": { + "description": "A min font size for fluid font size calculations in px, rem or em.", + "type": "string" + }, + "max": { + "description": "A max font size for fluid font size calculations in px, rem or em.", + "type": "string" + } + }, + "additionalProperties": false }, - "max": { - "description": "A max font size for fluid font size calculations in px, rem or em.", - "type": "string" + { + "type": "boolean" } - }, - "additionalProperties": false + ] } }, "additionalProperties": false From 54c074db1e05e8196fde21cfbf38a9a361b885b3 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:45:00 +0800 Subject: [PATCH 05/10] Fix core/archives non-property block having additionalProperties: false --- schemas/json/theme.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 7f57a01244114b..6e156237c30da5 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -570,8 +570,7 @@ "type": "object", "properties": { "core/archives": { - "description": "Archive block. Display a monthly archive of your posts. This block has no block-level settings", - "additionalProperties": false + "description": "Archive block. Display a monthly archive of your posts. This block has no block-level settings" }, "core/audio": { "$ref": "#/definitions/settingsPropertiesComplete" From 373515ac05a54c06673dc12d414f669631127207 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:52:13 +0800 Subject: [PATCH 06/10] Fix stylesProperties type --- schemas/json/theme.json | 1 + 1 file changed, 1 insertion(+) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 6e156237c30da5..6dd7e686621ccb 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -871,6 +871,7 @@ } }, "stylesProperties": { + "type": "object", "properties": { "border": { "description": "Border styles.", From d49897c76adbe6d39987325545e2f6d996971912 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Tue, 30 Aug 2022 17:52:47 +0800 Subject: [PATCH 07/10] Fix styleProperties border direction properties --- schemas/json/theme.json | 104 +++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 44 deletions(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 6dd7e686621ccb..14df5dd4aaf0f8 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -919,60 +919,76 @@ "type": "string" }, "top": { - "color": { - "description": "Sets the `border-top-color` CSS property.", - "type": "string" - }, - "style": { - "description": "Sets the `border-top-style` CSS property.", - "type": "string" + "type": "object", + "properties": { + "color": { + "description": "Sets the `border-top-color` CSS property.", + "type": "string" + }, + "style": { + "description": "Sets the `border-top-style` CSS property.", + "type": "string" + }, + "width": { + "description": "Sets the `border-top-width` CSS property.", + "type": "string" + } }, - "width": { - "description": "Sets the `border-top-width` CSS property.", - "type": "string" - } + "additionalProperties": false }, "right": { - "color": { - "description": "Sets the `border-right-color` CSS property.", - "type": "string" - }, - "style": { - "description": "Sets the `border-right-style` CSS property.", - "type": "string" + "type": "object", + "properties": { + "color": { + "description": "Sets the `border-right-color` CSS property.", + "type": "string" + }, + "style": { + "description": "Sets the `border-right-style` CSS property.", + "type": "string" + }, + "width": { + "description": "Sets the `border-right-width` CSS property.", + "type": "string" + } }, - "width": { - "description": "Sets the `border-right-width` CSS property.", - "type": "string" - } + "additionalProperties": false }, "bottom": { - "color": { - "description": "Sets the `border-bottom-color` CSS property.", - "type": "string" - }, - "style": { - "description": "Sets the `border-bottom-style` CSS property.", - "type": "string" + "type": "object", + "properties": { + "color": { + "description": "Sets the `border-bottom-color` CSS property.", + "type": "string" + }, + "style": { + "description": "Sets the `border-bottom-style` CSS property.", + "type": "string" + }, + "width": { + "description": "Sets the `border-bottom-width` CSS property.", + "type": "string" + } }, - "width": { - "description": "Sets the `border-bottom-width` CSS property.", - "type": "string" - } + "additionalProperties": false }, "left": { - "color": { - "description": "Sets the `border-left-color` CSS property.", - "type": "string" - }, - "style": { - "description": "Sets the `border-left-style` CSS property.", - "type": "string" + "type": "object", + "properties": { + "color": { + "description": "Sets the `border-left-color` CSS property.", + "type": "string" + }, + "style": { + "description": "Sets the `border-left-style` CSS property.", + "type": "string" + }, + "width": { + "description": "Sets the `border-left-width` CSS property.", + "type": "string" + } }, - "width": { - "description": "Sets the `border-left-width` CSS property.", - "type": "string" - } + "additionalProperties": false } }, "additionalProperties": false From 3c3291593f7926705e1efd1af5ae6bc232022f33 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Mon, 5 Sep 2022 10:24:45 +0800 Subject: [PATCH 08/10] Fix core/archives block to have object type --- schemas/json/theme.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/schemas/json/theme.json b/schemas/json/theme.json index 14df5dd4aaf0f8..0e658ad2666241 100644 --- a/schemas/json/theme.json +++ b/schemas/json/theme.json @@ -570,7 +570,9 @@ "type": "object", "properties": { "core/archives": { - "description": "Archive block. Display a monthly archive of your posts. This block has no block-level settings" + "type": "object", + "description": "Archive block. Display a monthly archive of your posts. This block has no block-level settings", + "additionalProperties": false }, "core/audio": { "$ref": "#/definitions/settingsPropertiesComplete" From 88b8a087da908876344cc211a395d987bb3a643f Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Mon, 19 Sep 2022 13:05:14 +0800 Subject: [PATCH 09/10] Regenerate schema docs --- .../theme-json-reference/theme-json-living.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/reference-guides/theme-json-reference/theme-json-living.md b/docs/reference-guides/theme-json-reference/theme-json-living.md index 0096a41be04baf..061c1db655729c 100644 --- a/docs/reference-guides/theme-json-reference/theme-json-living.md +++ b/docs/reference-guides/theme-json-reference/theme-json-living.md @@ -139,10 +139,10 @@ Border styles. | radius | undefined | | | style | string | | | width | string | | -| top | undefined | | -| right | undefined | | -| bottom | undefined | | -| left | undefined | | +| top | object | color, style, width | +| right | object | color, style, width | +| bottom | object | color, style, width | +| left | object | color, style, width | --- From c19d390eab814b30f1b0c8edf76fdb619ff5fdf2 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Wed, 21 Sep 2022 11:46:20 -0700 Subject: [PATCH 10/10] Add note about using compile instead of validateSchema --- test/integration/theme-schema.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/integration/theme-schema.test.js b/test/integration/theme-schema.test.js index e721e5eca05e46..0e5ed88a1441eb 100644 --- a/test/integration/theme-schema.test.js +++ b/test/integration/theme-schema.test.js @@ -15,7 +15,11 @@ describe( 'theme.json schema', () => { allowMatchingProperties: true, } ); - it( 'compiles in strict mode', async () => { + it( 'strictly adheres to the draft-04 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( themeSchema ); expect( result.errors ).toBe( null );