Bug fixes and new schema contributions are very welcome.
There is no fully automatic process to create these schemas. A schema for a PHP object is created from the public properties of its class, and a schema for a REST API response is created from an OPTIONS request to its REST API endpoint.
-
Install the PHP dependencies:
composer install
-
Install the Node dependencies:
npm install
-
If you want to run the tests locally, check the MySQL database credentials in the
tests/.env
file and amend them as necessary.
The WordPress REST API response doesn't fully adhere to the JSON schema spec, so we need to tweak its output in order to generate a valid schema.
- Perform an
OPTIONS
request to an endpoint that returns the object (or array of the objects) that you want to create a schema for. For example/wp-json/wp/v2/users
. - Use the
schema
property from the response as a basis for the schema. - Set the
$schema
,$id
,title
, anddescription
properties using the same format as existing schemas. - Remove any properties that have a
context
of[]
as these are write-only and not exposed. So far I've only seen this for thepassword
property of a user. - Remove any
properties
properties that have a value of[]
. - All properties that have a
context
ofview
should be added to therequired
property. - Remove the
context
,required
, andreadonly
properties from each property as these are not valid JSON schema properties. - Add
additionalProperties
information to anyobject
properties as appropriate. - Cross-reference the properties with those in the
get_item_schema()
method of the controller class. There may be properties that are conditionally added. Add them to the schema if so. - Add the schema to the
REST_API
property in the rootschema.json
using a$ref
to the schema file. - In
tests/mu-plugins/mu-plugin.php
add a newjson-dump
command for the new endpoint- Start by copy-pasting an existing command such as the
json-dump post
one for/wp/v2/posts
- The command should perform a REST API request to the endpoint and pass the result to the
save()
function which saves it as JSON during the tests
- Start by copy-pasting an existing command such as the
- In
package.json
add a new"test-{object-type}"
entry toscripts
if one doesn't already exist. - In
composer.json
add two entries to thetest
script if one doesn't already exist:"wp json-dump {object-type}"
"npm run test-{object-type}"
- Run
composer run test
to validate and test the schemas. - Run
npm run build-wp-types
and check the output ofpackages/wp-types/index.ts
. - Add documentation for the schema in both
readme.md
andpackages/wp-types/readme.md
.
The schema for a PHP object is created using the docblocks from its class properties and a lot of manual adjustment.
-
JSON encode an instance of the class you're interested in. For example:
echo json_encode( get_post( 123 ), JSON_PRETTY_PRINT );
-
Make a note of all of its property names.
-
Open the corresponding PHP file for the class.
-
Correlate this public property list with those defined in the class.
- Why? An object can have a public property that's not declared on the class. In this case, you'll need to provide the type and description yourself.
-
Wave your hands over your keyboard.
-
Add the schema to the root
schema.json
using a$ref
to the schema file. -
In
tests/mu-plugins/mu-plugin.php
add a newjson-dump
command for the new object type- Start by copy-pasting an existing command such as the
json-dump error
one - The command should pass an array of one or more objects of this type to the
save()
function which saves it as JSON during the tests
- Start by copy-pasting an existing command such as the
-
In
package.json
add two entries to thetest
script:"wp json-dump {object-type}"
"npm run test-{object-type}"
-
Run
composer run test
to validate and test the schemas. -
Run
npm run build-wp-types
and check the output ofpackages/wp-types/index.ts
. -
Add documentation for the schema in both
readme.md
andpackages/wp-types/readme.md
.
npm install
composer install
composer run test
npm run build-wp-types
npm version <major|minor|patch>
git push
git push --tags
When a tag is pushed, the following packages will be deployed to npm automatically via GitHub Actions: