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

Prepare JSON schemas for Draft 7 update #63582

Merged
merged 7 commits into from
Jul 16, 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
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 @@ -282,7 +283,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 @@ -1892,6 +1894,7 @@
]
},
"stylesElementsPropertiesComplete": {
"description": "Styles defined on a per-element basis using the element's selector.",
"type": "object",
"properties": {
"button": {
Expand All @@ -1905,6 +1908,7 @@
"border": {},
"background": {},
"color": {},
"dimensions": {},
"filter": {},
"shadow": {},
"outline": {},
Expand Down Expand Up @@ -1942,10 +1946,16 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
"spacing": {},
"typography": {},
"filter": {},
"shadow": {},
"outline": {},
"css": {},
":hover": {
"$ref": "#/definitions/stylesPropertiesComplete"
},
Expand Down Expand Up @@ -2000,6 +2010,7 @@
"additionalProperties": false
},
"stylesBlocksPropertiesComplete": {
"description": "Styles defined on a per-block basis using the block's selector.",
"type": "object",
"properties": {
"core/archives": {
Expand Down Expand Up @@ -2349,6 +2360,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2385,6 +2397,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2718,6 +2731,7 @@
},
{
"properties": {
"background": {},
"border": {},
"color": {},
"dimensions": {},
Expand Down Expand Up @@ -2791,7 +2805,6 @@
"dimensions": {},
"custom": {},
"blocks": {
"description": "Settings defined on a per-block basis.",
"$ref": "#/definitions/settingsBlocksPropertiesComplete"
}
},
Expand All @@ -2811,18 +2824,17 @@
"background": {},
"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"
},
"variations": {
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
Loading