Skip to content

Commit

Permalink
Prepare JSON schemas for Draft 7 update (#63582)
Browse files Browse the repository at this point in the history
Co-authored-by: ajlende <ajlende@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
  • Loading branch information
4 people authored and vcanales committed Sep 4, 2024
1 parent 9b9bbe8 commit 8bf8d2a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
/**
* External dependencies
*/
const path = require( 'path' );
const fs = require( 'fs' );
import path from 'path';
import fs from 'fs';
import url from 'url';
import $RefParser from '@apidevtools/json-schema-ref-parser';

const __dirname = path.dirname( url.fileURLToPath( import.meta.url ) );

/**
* Path to root project directory.
*
Expand Down Expand Up @@ -58,7 +63,7 @@ const END_TOKEN = '<!-- END TOKEN Autogenerated - DO NOT EDIT -->';
*/
const TOKEN_PATTERN = new RegExp( START_TOKEN + '[^]*' + END_TOKEN );

const themejson = require( THEME_JSON_SCHEMA_FILE );
const themejson = await $RefParser.dereference( THEME_JSON_SCHEMA_FILE );

/**
* Convert object keys to an array.
Expand All @@ -74,42 +79,6 @@ const keys = ( maybeObject ) => {
return Object.keys( maybeObject );
};

/**
* Get definition from ref.
*
* @param {string} ref
* @return {Object} definition
* @throws {Error} If the referenced definition is not found in 'themejson.definitions'.
*
* @example
* getDefinition( '#/definitions/typographyProperties/properties/fontFamily' )
* // returns themejson.definitions.typographyProperties.properties.fontFamily
*/
const resolveDefinitionRef = ( ref ) => {
const refParts = ref.split( '/' );
const definition = refParts[ refParts.length - 1 ];
if ( ! themejson.definitions[ definition ] ) {
throw new Error( `Can't resolve '${ ref }'. Definition not found` );
}
return themejson.definitions[ definition ];
};

/**
* Get properties from an array.
*
* @param {Object} items
* @return {Object} properties
*/
const getPropertiesFromArray = ( items ) => {
// if its a $ref resolve it
if ( items.$ref ) {
return resolveDefinitionRef( items.$ref ).properties;
}

// otherwise just return the properties
return items.properties;
};

/**
* Convert settings properties to markup.
*
Expand All @@ -133,9 +102,7 @@ const getSettingsPropertiesMarkup = ( struct ) => {
let type = props[ key ].type || '';
let ps =
props[ key ].type === 'array'
? keys( getPropertiesFromArray( props[ key ].items ) )
.sort()
.join( ', ' )
? keys( props[ key ].items.properties ).sort().join( ', ' )
: '';

/*
Expand All @@ -154,9 +121,7 @@ const getSettingsPropertiesMarkup = ( struct ) => {
.map( ( item ) =>
item?.type === 'object' && item?.properties
? '_{' +
keys( getPropertiesFromArray( item ) )
.sort()
.join( ', ' ) +
keys( item.properties ).sort().join( ', ' ) +
'}_'
: ''
)
Expand Down Expand Up @@ -244,10 +209,6 @@ const formatType = ( prop ) => {
if ( item.type ) {
types.push( item.type );
}
// refComplete is always an object
if ( item.$ref && item.$ref === '#/definitions/refComplete' ) {
types.push( 'object' );
}
} );

type = [ ...new Set( types ) ].join( ', ' );
Expand Down
88 changes: 88 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"devDependencies": {
"@actions/core": "1.9.1",
"@actions/github": "5.0.0",
"@apidevtools/json-schema-ref-parser": "11.6.4",
"@ariakit/test": "^0.3.7",
"@babel/core": "7.24.3",
"@babel/plugin-proposal-export-namespace-from": "7.18.9",
Expand Down Expand Up @@ -281,7 +282,7 @@
"docs:blocks": "node ./bin/api-docs/gen-block-lib-list.js",
"docs:build": "npm-run-all docs:gen docs:blocks docs:api-ref docs:theme-ref",
"docs:gen": "node ./docs/tool/index.js",
"docs:theme-ref": "node ./bin/api-docs/gen-theme-reference.js",
"docs:theme-ref": "node ./bin/api-docs/gen-theme-reference.mjs",
"env": "wp-env",
"fixtures:clean": "rimraf \"test/integration/fixtures/blocks/*.+(json|serialized.html)\"",
"fixtures:generate": "cross-env GENERATE_MISSING_FIXTURES=y npm run test:unit test/integration/full-content/ && npm run format test/integration/fixtures/blocks/*.json",
Expand Down
22 changes: 17 additions & 5 deletions schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"description": "A reference to another property value. e.g. `styles.color.text`",
"type": "string"
}
}
},
"additionalProperties": false
},
"settingsPropertiesAppearanceTools": {
"type": "object",
Expand Down Expand Up @@ -792,7 +793,6 @@
"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.",
"$ref": "#/definitions/settingsCustomAdditionalProperties"
}
}
Expand Down Expand Up @@ -839,6 +839,7 @@
]
},
"settingsBlocksPropertiesComplete": {
"description": "Settings defined on a per-block basis.",
"type": "object",
"properties": {
"core/archives": {
Expand Down Expand Up @@ -1144,6 +1145,7 @@
"additionalProperties": false
},
"settingsCustomAdditionalProperties": {
"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.",
"type": "object",
"additionalProperties": {
"oneOf": [
Expand Down Expand Up @@ -1843,6 +1845,7 @@
]
},
"stylesElementsPropertiesComplete": {
"description": "Styles defined on a per-element basis using the element's selector.",
"type": "object",
"properties": {
"button": {
Expand All @@ -1855,6 +1858,7 @@
"properties": {
"border": {},
"color": {},
"dimensions": {},
"filter": {},
"shadow": {},
"outline": {},
Expand Down Expand Up @@ -1892,10 +1896,16 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
"spacing": {},
"typography": {},
"filter": {},
"shadow": {},
"outline": {},
"css": {},
":hover": {
"$ref": "#/definitions/stylesPropertiesComplete"
},
Expand Down Expand Up @@ -1950,6 +1960,7 @@
"additionalProperties": false
},
"stylesBlocksPropertiesComplete": {
"description": "Styles defined on a per-block basis using the block's selector.",
"type": "object",
"properties": {
"variations": {
Expand Down Expand Up @@ -2311,6 +2322,7 @@
"type": "string"
}
},
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2347,6 +2359,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2680,6 +2693,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2753,7 +2767,6 @@
"dimensions": {},
"custom": {},
"blocks": {
"description": "Settings defined on a per-block basis.",
"$ref": "#/definitions/settingsBlocksPropertiesComplete"
}
},
Expand All @@ -2772,18 +2785,17 @@
"properties": {
"border": {},
"color": {},
"dimensions": {},
"spacing": {},
"typography": {},
"filter": {},
"shadow": {},
"outline": {},
"css": {},
"elements": {
"description": "Styles defined on a per-element basis using the element's selector.",
"$ref": "#/definitions/stylesElementsPropertiesComplete"
},
"blocks": {
"description": "Styles defined on a per-block basis using the block's selector.",
"$ref": "#/definitions/stylesBlocksPropertiesComplete"
},
"background": {
Expand Down
17 changes: 15 additions & 2 deletions schemas/json/wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,21 @@
"patternProperties": {
"[a-zA-Z]": {
"type": "object",
"$ref": "#/definitions/wpEnvProperties",
"additionalProperties": false
"allOf": [
{ "$ref": "#/definitions/wpEnvProperties" },
{
"properties": {
"core": {},
"phpVersion": {},
"plugins": {},
"themes": {},
"port": {},
"config": {},
"mappings": {}
},
"additionalProperties": false
}
]
}
}
}
Expand Down

0 comments on commit 8bf8d2a

Please sign in to comment.