feat: Add --allow-invalid-content-type-endpoint option to validate-json - #932
feat: Add --allow-invalid-content-type-endpoint option to validate-json#932zoliszabo wants to merge 3 commits into
Conversation
The validate-json CLI now accepts a repeatable `--allow-invalid-content-type-endpoint=<url>` option. Each URL prefix is registered on the UriRetriever via the existing `addInvalidContentTypeEndpoint()` API, bypassing the schema media type check for schemas fetched from that host. WordPress's `theme.json` uses `https://schemas.wp.org/trunk/theme.json` as its JSON schema (docs here: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/). That URL redirects to `https://raw.githubusercontent.com/WordPress/gutenberg/trunk/schemas/json/theme.json`, which serves `Content-Type: text/plain; charset=utf-8`, so running `vendor/bin/validate-json theme.json https://schemas.wp.org/trunk/theme.json` failed with `InvalidSchemaMediaTypeException`. With the included changes, passing `--allow-invalid-content-type-endpoint=https://schemas.wp.org/` whitelists the requested host and lets the validation run.
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
DannyvdSluijs
left a comment
There was a problem hiding this comment.
Thanks for the addition. This is a good improvement, going through the changes I've made some comments to fix some small issues being introduced.
Can you take a look?
| array_shift($argv);//script itself | ||
| foreach ($argv as $arg) { | ||
| if ($arg[0] == '-') { | ||
| if (strpos($arg, '--allow-invalid-content-type-endpoint=') === 0) { |
There was a problem hiding this comment.
The parsing should be improved. This introduces two unwanted edge cases:
-
Empty value error (
--allow-invalid-content-type-endpoint=) is silently ignored. This should result in an non success exit code. -
Value passing without a equals sign (
--allow-invalid-content-type-endpoint https://schemas.wp.org) would set$arArgs['https://schemas.wp.org'] = trueresulting in an error
./bin/validate-json --allow-invalid-content-type-endpoint https://schemas.wp.org/ theme.json
PHP Warning: file_get_contents(https://schemas.wp.org/): Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in ./bin/validate-json on line 148
PHP Stack trace:
PHP 1. {main}() ./bin/validate-json:0
PHP 2. file_get_contents($filename = 'https://schemas.wp.org/', $use_include_path = FALSE, $context = resource(24) of type (stream-context)) ./bin/validate-json:148
Warning: file_get_contents(https://schemas.wp.org/): Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in ./bin/validate-json on line 148
Call Stack:
0.0004 506208 1. {main}() ./bin/validate-json:0
0.0040 814392 2. file_get_contents($filename = 'https://schemas.wp.org/', $use_include_path = FALSE, $context = resource(24) of type (stream-context)) ./bin/validate-json:148
Data file is not readable or empty.| && is_array($arOptions['--allow-invalid-content-type-endpoint']) | ||
| ) { | ||
| foreach ($arOptions['--allow-invalid-content-type-endpoint'] as $endpoint) { | ||
| $retriever->addInvalidContentTypeEndpoint($endpoint); |
There was a problem hiding this comment.
This is only applied to the schema storage which isn't passed to the validator on line 233
Could you pass it using a factory, this way it would also be solved for nested schemas.
new JsonSchema\Validator(new JsonSchema\Constraints\Factory($refResolver, $retriever))Accept the value in both --option=value and --option value forms. Exit with a non-zero code when the value is missing instead of silently ignoring an empty value. Add CLI coverage for the option.
|
@DannyvdSluijs Thanks for the review. I pushed new commits fixing the highlighted issues. |
Description
The
validate-jsonCLI now accepts a repeatable--allow-invalid-content-type-endpoint=<url>option. Each URL prefix is registered on the UriRetriever via the existingaddInvalidContentTypeEndpoint()method, bypassing the schema media type check for schemas fetched from that host.Related Issue
N/A
Type of Change
Checklist
Additional Notes
Example use case:
WordPress's
theme.jsonuseshttps://schemas.wp.org/trunk/theme.jsonas its JSON schema (docs here: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/). That URL redirects tohttps://raw.githubusercontent.com/WordPress/gutenberg/trunk/schemas/json/theme.json, which servesContent-Type: text/plain; charset=utf-8, so runningfails with
InvalidSchemaMediaTypeException.With the included changes, passing
--allow-invalid-content-type-endpoint=https://schemas.wp.org/whitelists the requested host and lets the validation run.