From 7f5097adba3e24dd2223900c75a2fdf9855a9b63 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 09:43:53 -0800 Subject: [PATCH 1/6] add quiet option --- bin/validate-json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/validate-json b/bin/validate-json index e9c18095..3572db28 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); @@ -112,6 +111,7 @@ Usage: validate-json data.json Options: --dump-schema Output full schema and exit --dump-schema-url Output URL of schema + --quiet Suppress output unless there are errors -h --help Show this help HLP; @@ -221,7 +221,9 @@ try { $validator->check($data, $schema); if ($validator->isValid()) { - echo "OK. The supplied JSON validates against the schema.\n"; + if(!isset($arOptions['--quiet'])) { + echo "OK. The supplied JSON validates against the schema.\n"; + } } else { echo "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { From eee03bbfb8a1bd3d35dae6e82fe6f7faad3c5eb0 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 10:18:13 -0800 Subject: [PATCH 2/6] use verbose instead of quiet --- bin/validate-json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/validate-json b/bin/validate-json index 3572db28..820e6e41 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -111,7 +111,7 @@ Usage: validate-json data.json Options: --dump-schema Output full schema and exit --dump-schema-url Output URL of schema - --quiet Suppress output unless there are errors + --verbose Show additional output -h --help Show this help HLP; @@ -221,7 +221,7 @@ try { $validator->check($data, $schema); if ($validator->isValid()) { - if(!isset($arOptions['--quiet'])) { + if(isset($arOptions['--verbose'])) { echo "OK. The supplied JSON validates against the schema.\n"; } } else { From 8173115956bb665b7db84ee420cc397dd1f038d0 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 11:05:23 -0800 Subject: [PATCH 3/6] add quiet option --- bin/validate-json | 126 +++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/bin/validate-json b/bin/validate-json index 820e6e41..7d1f5b99 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -28,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 <<resolve($pathSchema, $urlData); if (isset($arOptions['--dump-schema-url'])) { - echo $urlSchema . "\n"; + output($urlSchema . "\n"); exit(); } } catch (Exception $e) { - echo "Error loading JSON schema file\n"; - echo $urlSchema . "\n"; - echo $e->getMessage() . "\n"; + output("Error loading JSON schema file\n"); + output($urlSchema . "\n"); + output($e->getMessage() . "\n"); exit(2); } $refResolver = new JsonSchema\SchemaStorage($retriever, $resolver); @@ -212,7 +224,7 @@ $schema = $refResolver->resolveRef($urlSchema); if (isset($arOptions['--dump-schema'])) { $options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; - echo json_encode($schema, $options) . "\n"; + output(json_encode($schema, $options) . "\n"); exit(); } @@ -222,18 +234,18 @@ try { if ($validator->isValid()) { if(isset($arOptions['--verbose'])) { - echo "OK. The supplied JSON validates against the schema.\n"; + 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); } From 53a65e0724ed2565c004cee15e80b2a81b914d93 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 11:19:23 -0800 Subject: [PATCH 4/6] always output dump-schema --- bin/validate-json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/validate-json b/bin/validate-json index 7d1f5b99..d5ab2ad2 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -224,7 +224,7 @@ $schema = $refResolver->resolveRef($urlSchema); if (isset($arOptions['--dump-schema'])) { $options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; - output(json_encode($schema, $options) . "\n"); + echo json_encode($schema, $options) . "\n"; exit(); } From 51865ab32e8f1cc0aeb93a56fdd53ca717a4b10f Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 11:21:16 -0800 Subject: [PATCH 5/6] always output dump-schema-url --- bin/validate-json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/validate-json b/bin/validate-json index d5ab2ad2..0e3d40a5 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -210,7 +210,7 @@ try { $urlSchema = $resolver->resolve($pathSchema, $urlData); if (isset($arOptions['--dump-schema-url'])) { - output($urlSchema . "\n"); + echo $urlSchema . "\n"; exit(); } } catch (Exception $e) { From aad0b753e6f656930de29d3f709d588b6ad50b19 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Sat, 11 Mar 2017 11:41:46 -0800 Subject: [PATCH 6/6] fix typo and ws --- bin/validate-json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/validate-json b/bin/validate-json index 0e3d40a5..421ebcde 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -56,7 +56,7 @@ Options: --dump-schema Output full schema and exit --dump-schema-url Output URL of schema --verbose Show additional output - --quiet Supress all output + --quiet Suppress all output -h --help Show this help HLP;