-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add theme.json schema tests #44252
Add theme.json schema tests #44252
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @alecgeatches! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
} ); | ||
|
||
it( 'compiles in strict mode', async () => { | ||
const result = ajv.compile( themeSchema ); |
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.
See point 3 from this discussion about why compile()
is being used here instead of validate()
:
The validation code no longer uses an object to validate the schema. This ended up being trickier than expected. Your suggested code change using
validate
ran successfully, but did not report and strict errors ontheme.json
's schema. For example, I tried adding invalid properties like"test": "abc"
to the schema which should fail Ajv'sstrictSchema
, but no errors were reported. The only way I could get the meta-schema to be validated was by validating an actual data structure or using.compile()
:
const result = ajv.compile( themeSchema );
It's unclear from Ajv's documentation why.validate('.../draft-04#', $themeSchema)
or.validate($themeSchema)
weren't reporting errors. The best I could find was the documentation forcompile()
which mentions the meta-schema:The schema passed to this method will be validated against meta-schema unless
validateSchema
option is false. If schema is invalid, an error will be thrown. See options.It seems like the meta-schema is only validated during compilation, so the code has been changed to use
compile()
instead.
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.
@alecgeatches I found a couple notes about this in the Ajv docs and issues.
ajv.validateSchema(schema: object): boolean
Validates schema. This method should be used to validate schemas rather than
validate
due to the inconsistency ofuri
format in JSON Schema standard.
validateSchema
only checks “syntax” (it validated schema as JSON against metaschema), it may still be semantically incorrect.
Looks like I was wrong about using validate
for validating against a meta schema in the first place. And it's intentional that strict mode only applies to compile
, so that's why we should be using it instead of validateSchema
to validate our schema. 🙃
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.
Might be worth adding a note in the code about using compile
instead of validate
. If there isn't an issue on Ajv's GitHub repo, it could be good to create one and link it in the comment.
Otherwise this all looks great! Thanks for separating out the test stuff into its own PR. 👍
Congratulations on your first merged pull request, @alecgeatches! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: https://profiles.wordpress.org/me/profile/edit/ And if you don't have a WordPress.org account, you can create one on this page: https://login.wordpress.org/register Kudos! |
@ajlende Glad you were able to track down a source for the |
What?
See original PR #43801. This PR does not include nesting-specific changes, just the
theme.json
test additions and fixes to the current schema. Relevant portions of the original PR discussion have been copied here.These changes were made:
theme-schema.test.js
based on existing block schema tests. The existing schema failed validation with Ajv's strict mode againstdraft-04
, mostly due to some missingtype
identifiers and a handful of other small changes.Why?
From Ajv's strict mode documentation:
Strict mode testing fixes a few missing/unknown identifiers in the current schema, and should help prevent accidental mistakes going forward.
How
Some minor changes were made to the original schema to make it validate correctly against
draft-04
. To view the original errors, check out thetest/integration/theme-schema.test.js
test file from the fork repository branch againsttrunk
and use this command to run validation checks:Running the test gives this validation result:
Click to expand
Those individual fixes are documented in these commits:
Fix missing property object types for errors like:
Remove type conflict in fontWeight for this error:
Refactor fontSizes.fluid into non-union type for this error:
Fix core/archives non-property block having additionalProperties: false for this error:
Some additional fixes in Fix stylesProperties type and Fix styleProperties border direction properties that cropped up after the previous fixes, mostly addressing additional
properties
object types missing. This also fixes theborder
autogenerated documentation fortop
/bottom
/left
/right
.Remove sub-properties from
core/archive
in 3c32915 per this discussion.Testing Instructions
To test theme validation, run: