-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[In progress] Fix: top level default is not valid json schema. #10508
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
[In progress] Fix: top level default is not valid json schema. #10508
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
It seems 04 may support top level default, so I may follow a different path here. |
|
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": {}
}
|
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. |
|
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 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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $result = array(); | |
| $result = array(); |
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. |
|
Nice, thank you for taking care of it. |
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):
Paste the following code on the browser console:
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)