diff --git a/bin/validate-json b/bin/validate-json index e9c18095..421ebcde 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -17,7 +17,6 @@ function __autoload($className) { $className = ltrim($className, '\\'); $fileName = ''; - $namespace = ''; if ($lastNsPos = strrpos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); @@ -29,6 +28,49 @@ function __autoload($className) } } +// support running this tool from git checkout +if (is_dir(__DIR__ . '/../src/JsonSchema')) { + set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path()); +} + +$arOptions = array(); +$arArgs = array(); +array_shift($argv);//script itself +foreach ($argv as $arg) { + if ($arg{0} == '-') { + $arOptions[$arg] = true; + } else { + $arArgs[] = $arg; + } +} + +if (count($arArgs) == 0 + || isset($arOptions['--help']) || isset($arOptions['-h']) +) { + echo <<getMessage() . "\n"; + output("Error loading JSON schema file\n"); + output($urlSchema . "\n"); + output($e->getMessage() . "\n"); exit(2); } $refResolver = new JsonSchema\SchemaStorage($retriever, $resolver); @@ -221,17 +233,19 @@ try { $validator->check($data, $schema); if ($validator->isValid()) { - echo "OK. The supplied JSON validates against the schema.\n"; + if(isset($arOptions['--verbose'])) { + output("OK. The supplied JSON validates against the schema.\n"); + } } else { - echo "JSON does not validate. Violations:\n"; + output("JSON does not validate. Violations:\n"); foreach ($validator->getErrors() as $error) { - echo sprintf("[%s] %s\n", $error['property'], $error['message']); + output(sprintf("[%s] %s\n", $error['property'], $error['message'])); } exit(23); } } catch (Exception $e) { - echo "JSON does not validate. Error:\n"; - echo $e->getMessage() . "\n"; - echo "Error code: " . $e->getCode() . "\n"; + output("JSON does not validate. Error:\n"); + output($e->getMessage() . "\n"); + output("Error code: " . $e->getCode() . "\n"); exit(24); }