@@ -17,7 +17,6 @@ function __autoload($className)
1717{
1818 $ className = ltrim ($ className , '\\' );
1919 $ fileName = '' ;
20- $ namespace = '' ;
2120 if ($ lastNsPos = strrpos ($ className , '\\' )) {
2221 $ namespace = substr ($ className , 0 , $ lastNsPos );
2322 $ className = substr ($ className , $ lastNsPos + 1 );
@@ -29,6 +28,49 @@ function __autoload($className)
2928 }
3029}
3130
31+ // support running this tool from git checkout
32+ if (is_dir (__DIR__ . '/../src/JsonSchema ' )) {
33+ set_include_path (__DIR__ . '/../src ' . PATH_SEPARATOR . get_include_path ());
34+ }
35+
36+ $ arOptions = array ();
37+ $ arArgs = array ();
38+ array_shift ($ argv );//script itself
39+ foreach ($ argv as $ arg ) {
40+ if ($ arg {0 } == '- ' ) {
41+ $ arOptions [$ arg ] = true ;
42+ } else {
43+ $ arArgs [] = $ arg ;
44+ }
45+ }
46+
47+ if (count ($ arArgs ) == 0
48+ || isset ($ arOptions ['--help ' ]) || isset ($ arOptions ['-h ' ])
49+ ) {
50+ echo <<<HLP
51+ Validate schema
52+ Usage: validate-json data.json
53+ or: validate-json data.json schema.json
54+
55+ Options:
56+ --dump-schema Output full schema and exit
57+ --dump-schema-url Output URL of schema
58+ --verbose Show additional output
59+ --quiet Suppress all output
60+ -h --help Show this help
61+
62+ HLP ;
63+ exit (1 );
64+ }
65+
66+ if (count ($ arArgs ) == 1 ) {
67+ $ pathData = $ arArgs [0 ];
68+ $ pathSchema = null ;
69+ } else {
70+ $ pathData = $ arArgs [0 ];
71+ $ pathSchema = getUrlFromPath ($ arArgs [1 ]);
72+ }
73+
3274/**
3375 * Show the json parse error that happened last
3476 *
@@ -44,7 +86,7 @@ function showJsonError()
4486 }
4587 }
4688
47- echo 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ;
89+ output ( 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ) ;
4890}
4991
5092function getUrlFromPath ($ path )
@@ -84,48 +126,18 @@ function parseHeaderValue($headerValue)
84126 return $ arData ;
85127}
86128
87-
88- // support running this tool from git checkout
89- if (is_dir (__DIR__ . '/../src/JsonSchema ' )) {
90- set_include_path (__DIR__ . '/../src ' . PATH_SEPARATOR . get_include_path ());
91- }
92-
93- $ arOptions = array ();
94- $ arArgs = array ();
95- array_shift ($ argv );//script itself
96- foreach ($ argv as $ arg ) {
97- if ($ arg {0 } == '- ' ) {
98- $ arOptions [$ arg ] = true ;
99- } else {
100- $ arArgs [] = $ arg ;
129+ /**
130+ * Send a string to the output stream, but only if --quiet is not enabled
131+ *
132+ * @param $str A string output
133+ */
134+ function output ($ str ) {
135+ global $ arOptions ;
136+ if (!isset ($ arOptions ['--quiet ' ])) {
137+ echo $ str ;
101138 }
102139}
103140
104- if (count ($ arArgs ) == 0
105- || isset ($ arOptions ['--help ' ]) || isset ($ arOptions ['-h ' ])
106- ) {
107- echo <<<HLP
108- Validate schema
109- Usage: validate-json data.json
110- or: validate-json data.json schema.json
111-
112- Options:
113- --dump-schema Output full schema and exit
114- --dump-schema-url Output URL of schema
115- -h --help Show this help
116-
117- HLP ;
118- exit (1 );
119- }
120-
121- if (count ($ arArgs ) == 1 ) {
122- $ pathData = $ arArgs [0 ];
123- $ pathSchema = null ;
124- } else {
125- $ pathData = $ arArgs [0 ];
126- $ pathSchema = getUrlFromPath ($ arArgs [1 ]);
127- }
128-
129141$ urlData = getUrlFromPath ($ pathData );
130142
131143$ context = stream_context_create (
@@ -141,14 +153,14 @@ $context = stream_context_create(
141153);
142154$ dataString = file_get_contents ($ pathData , false , $ context );
143155if ($ dataString == '' ) {
144- echo "Data file is not readable or empty. \n" ;
156+ output ( "Data file is not readable or empty. \n" ) ;
145157 exit (3 );
146158}
147159
148160$ data = json_decode ($ dataString );
149161unset($ dataString );
150162if ($ data === null ) {
151- echo "Error loading JSON data file \n" ;
163+ output ( "Error loading JSON data file \n" ) ;
152164 showJsonError ();
153165 exit (5 );
154166}
@@ -182,9 +194,9 @@ if ($pathSchema === null) {
182194
183195 //autodetect schema
184196 if ($ pathSchema === null ) {
185- echo "JSON data must be an object and have a \$schema property. \n" ;
186- echo "You can pass the schema file on the command line as well. \n" ;
187- echo "Schema autodetection failed. \n" ;
197+ output ( "JSON data must be an object and have a \$schema property. \n" ) ;
198+ output ( "You can pass the schema file on the command line as well. \n" ) ;
199+ output ( "Schema autodetection failed. \n" ) ;
188200 exit (6 );
189201 }
190202}
@@ -202,9 +214,9 @@ try {
202214 exit ();
203215 }
204216} catch (Exception $ e ) {
205- echo "Error loading JSON schema file \n" ;
206- echo $ urlSchema . "\n" ;
207- echo $ e ->getMessage () . "\n" ;
217+ output ( "Error loading JSON schema file \n" ) ;
218+ output ( $ urlSchema . "\n" ) ;
219+ output ( $ e ->getMessage () . "\n" ) ;
208220 exit (2 );
209221}
210222$ refResolver = new JsonSchema \SchemaStorage ($ retriever , $ resolver );
@@ -221,17 +233,19 @@ try {
221233 $ validator ->check ($ data , $ schema );
222234
223235 if ($ validator ->isValid ()) {
224- echo "OK. The supplied JSON validates against the schema. \n" ;
236+ if (isset ($ arOptions ['--verbose ' ])) {
237+ output ("OK. The supplied JSON validates against the schema. \n" );
238+ }
225239 } else {
226- echo "JSON does not validate. Violations: \n" ;
240+ output ( "JSON does not validate. Violations: \n" ) ;
227241 foreach ($ validator ->getErrors () as $ error ) {
228- echo sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]);
242+ output ( sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]) );
229243 }
230244 exit (23 );
231245 }
232246} catch (Exception $ e ) {
233- echo "JSON does not validate. Error: \n" ;
234- echo $ e ->getMessage () . "\n" ;
235- echo "Error code: " . $ e ->getCode () . "\n" ;
247+ output ( "JSON does not validate. Error: \n" ) ;
248+ output ( $ e ->getMessage () . "\n" ) ;
249+ output ( "Error code: " . $ e ->getCode () . "\n" ) ;
236250 exit (24 );
237251}
0 commit comments