Skip to content
Open
Changes from all commits
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
47 changes: 45 additions & 2 deletions bin/validate-json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,39 @@ require($composerAutoload);
$arOptions = [];
$arArgs = [];
array_shift($argv);//script itself
foreach ($argv as $arg) {

// options that take a value; each may be repeated
$multiValueOptions = [
'--allow-invalid-content-type-endpoint',
];

$argCount = count($argv);
for ($i = 0; $i < $argCount; $i++) {
$arg = $argv[$i];

foreach ($multiValueOptions as $valueOption) {
if ($arg === $valueOption) {
// space-separated form: --option <value>
if (!isset($argv[$i + 1]) || strpos($argv[$i + 1], '-') === 0) {
output("Missing value for $valueOption\n");
exit(1);
}
$arOptions[$valueOption][] = $argv[++$i];
continue 2;
}

if (strpos($arg, $valueOption . '=') === 0) {
// equals form: --option=<value>
$value = substr($arg, strlen($valueOption . '='));
if ($value === '') {
output("Missing value for $valueOption\n");
exit(1);
}
$arOptions[$valueOption][] = $value;
continue 2;
}
}

if ($arg[0] == '-') {
$arOptions[$arg] = true;
} else {
Expand All @@ -42,6 +74,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 +227,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 All @@ -214,7 +257,7 @@ if (isset($arOptions['--dump-schema'])) {
}

try {
$validator = new JsonSchema\Validator();
$validator = new JsonSchema\Validator(new JsonSchema\Constraints\Factory($refResolver, $retriever));
$validator->validate($data, $schema);

if ($validator->isValid()) {
Expand Down