Skip to content

Conversation

@jorgefilipecosta
Copy link
Member

@jorgefilipecosta jorgefilipecosta commented Nov 12, 2025

JSON schame does not supports a top level default as we are currently doing, this causes problems when the schema is used in a validator.

cc: @gziolo I think it would be good to have this in 6.9 as the current input scheme we are using is not valid and does not work with a validator.

Testing

Enable the abilities api package so you have the client side wp.abilities object and validation.
Apply the following patch (it fixes a different issue in the client code which is being proposed at WordPress/abilities-api#142):

diff --git a/packages/client/src/validation.ts b/packages/client/src/validation.ts
index 030da40..62d3c1a 100644
--- a/packages/client/src/validation.ts
+++ b/packages/client/src/validation.ts
@@ -168,9 +168,9 @@ export function validateValueFromSchema(
 	}
 
 	try {
 		const validate = ajv.compile( args );
-		const valid = validate( value );
+		const valid = validate( value || {} );
 
 		if ( valid ) {
 			return true;
 		}

Paste the following code on the browser console:

const envInfoAbility = await wp.abilities.getAbility(
	'core/get-site-info'
);
const envInfoResult = await wp.abilities.executeAbility(
	'core/get-site-info'
);
await wp.abilities.registerAbility( {
	...envInfoAbility,
	name: 'core/get-site-info-cli',
	callback: () => envInfoResult,
} );
await wp.abilities.executeAbility( 'core/get-site-info-cli' );

It registers a 'core/get-site-info-cli client ability exactly the same as the server one.
Verify things work well.
Paste the same code on trunk and verify there is this error and the ability does not works:

validation.ts:195 Schema compilation error: Error: strict mode: default is ignored in the schema root
at checkStrictMode (util.js:174:1)
at checkNoDefault (index.js:139:1)
at index.js:65:1
at CodeGen.code (index.js:439:1)
at index.js:37:101
at CodeGen.code (index.js:439:1)
at CodeGen.func (index.js:587:1)
at validateFunction (index.js:37:1)
at topSchemaObjCode (index.js:62:1)
at validateFunctionCode (index.js:21:1)

@github-actions
Copy link

github-actions bot commented Nov 12, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jorgefilipecosta, shailu25, gziolo.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@jorgefilipecosta
Copy link
Member Author

It seems 04 may support top level default, so I may follow a different path here.

@jorgefilipecosta jorgefilipecosta changed the title Fix: top level default is not valid json schema. [In progress] Fix: top level default is not valid json schema. Nov 12, 2025
@gziolo
Copy link
Member

gziolo commented Nov 12, 2025

I'm not sure I follow. It's perfectly fine to define the following:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "default": {}
}

default is a hint, not a validation requirement. It doesn't mean that this default will get automatically applied, and that's why there is custom code that injects the top-level default when the value is not set.

@jorgefilipecosta
Copy link
Member Author

jorgefilipecosta commented Nov 12, 2025

validation.ts:195 Schema compilation error: Error: strict mode: default is ignored in the schema root

I'm not sure I follow. It's perfectly fine to define the following:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "default": {}
}

default is a hint, not a validation requirement. It doesn't mean that this default will get automatically applied, and that's why there is custom code that injects the top-level default when the value is not set.

The problem is that strict validators (e.g: ajv that we use on the client), don't accept a top level default, they considers schemas with them invalid. They only accept at the property level, that is the reason now if we use our schema in the validator we see the JS error shared above. But given that it seems we will keep draft-04 version and remove the examples from schemas which are not complient, I think we can follow another path and remove the top level default from the structure we pass to the validator. I will give a try in another PR.

@gziolo
Copy link
Member

gziolo commented Nov 12, 2025

There will be more issues like that when using the strict mode. It's worth evaluating whether we need a transformer that extracts what WordPress currently supports and makes it fully compatible with draft-04 before passing it to the validator?

Another good example is the required property expressed with version 3 syntax:

https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#version-3-syntax

// It is a very basic implementation and does not cover all JSON Schema features.
// It only looks for `default` values in the top-level `properties`.
if ( ! empty( $input_schema ) && array_key_exists( 'properties', $input_schema ) ) {
$result = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$result = array();
$result = array();

@jorgefilipecosta
Copy link
Member Author

There will be more issues like that when using the strict mode. It's worth evaluating whether we need a transformer that extracts what WordPress currently supports and makes it fully compatible with draft-04 before passing it to the validator?

Another good example is the required property expressed with version 3 syntax:

https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#version-3-syntax

Hi @gziolo, I removed examples at #10510 with another minor fix. And on the client I'm adding another small fix so strict validation handles the top level default WordPress/abilities-api#144. I think with both PR's we are in a good place for now, and both ended up being simple changes.

@gziolo
Copy link
Member

gziolo commented Nov 12, 2025

Nice, thank you for taking care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants