Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion bin/validate-json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ $arOptions = [];
$arArgs = [];
array_shift($argv);//script itself
foreach ($argv as $arg) {
if ($arg[0] == '-') {
if (strpos($arg, '--allow-invalid-content-type-endpoint=') === 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The parsing should be improved. This introduces two unwanted edge cases:

  1. Empty value error (--allow-invalid-content-type-endpoint=) is silently ignored. This should result in an non success exit code.

  2. Value passing without a equals sign (--allow-invalid-content-type-endpoint https://schemas.wp.org) would set $arArgs['https://schemas.wp.org'] = true resulting 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.

$endpoint = substr($arg, strlen('--allow-invalid-content-type-endpoint='));
if ($endpoint !== '') {
$arOptions['--allow-invalid-content-type-endpoint'][] = $endpoint;
}
} elseif ($arg[0] == '-') {
$arOptions[$arg] = true;
} else {
$arArgs[] = $arg;
Expand All @@ -42,6 +47,10 @@ Usage: validate-json data.json
Options:
--dump-schema Output full schema and exit
--dump-schema-url Output URL of schema
--allow-invalid-content-type-endpoint=<url>
Skip the Content-Type check for schemas fetched
from the given URL prefix. May be given multiple
times.
--verbose Show additional output
--quiet Suppress all output
-h --help Show this help
Expand Down Expand Up @@ -191,6 +200,13 @@ if ($pathSchema[0] == '/') {

$resolver = new JsonSchema\Uri\UriResolver();
$retriever = new JsonSchema\Uri\UriRetriever();
if (isset($arOptions['--allow-invalid-content-type-endpoint'])
&& is_array($arOptions['--allow-invalid-content-type-endpoint'])
) {
foreach ($arOptions['--allow-invalid-content-type-endpoint'] as $endpoint) {
$retriever->addInvalidContentTypeEndpoint($endpoint);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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))

}
}
try {
$urlSchema = $resolver->resolve($pathSchema, $urlData);

Expand Down
Loading