@@ -17,6 +17,7 @@ function __autoload($className)
1717{
1818 $ className = ltrim ($ className , '\\' );
1919 $ fileName = '' ;
20+ $ namespace = '' ;
2021 if ($ lastNsPos = strrpos ($ className , '\\' )) {
2122 $ namespace = substr ($ className , 0 , $ lastNsPos );
2223 $ className = substr ($ className , $ lastNsPos + 1 );
@@ -28,49 +29,6 @@ function __autoload($className)
2829 }
2930}
3031
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-
7432/**
7533 * Show the json parse error that happened last
7634 *
@@ -86,7 +44,7 @@ function showJsonError()
8644 }
8745 }
8846
89- output ( 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ) ;
47+ echo 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ;
9048}
9149
9250function getUrlFromPath ($ path )
@@ -126,18 +84,48 @@ function parseHeaderValue($headerValue)
12684 return $ arData ;
12785}
12886
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 ;
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 ;
138101 }
139102}
140103
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+
141129$ urlData = getUrlFromPath ($ pathData );
142130
143131$ context = stream_context_create (
@@ -153,14 +141,14 @@ $context = stream_context_create(
153141);
154142$ dataString = file_get_contents ($ pathData , false , $ context );
155143if ($ dataString == '' ) {
156- output ( "Data file is not readable or empty. \n" ) ;
144+ echo "Data file is not readable or empty. \n" ;
157145 exit (3 );
158146}
159147
160148$ data = json_decode ($ dataString );
161149unset($ dataString );
162150if ($ data === null ) {
163- output ( "Error loading JSON data file \n" ) ;
151+ echo "Error loading JSON data file \n" ;
164152 showJsonError ();
165153 exit (5 );
166154}
@@ -194,9 +182,9 @@ if ($pathSchema === null) {
194182
195183 //autodetect schema
196184 if ($ pathSchema === null ) {
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" ) ;
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" ;
200188 exit (6 );
201189 }
202190}
@@ -214,9 +202,9 @@ try {
214202 exit ();
215203 }
216204} catch (Exception $ e ) {
217- output ( "Error loading JSON schema file \n" ) ;
218- output ( $ urlSchema . "\n" ) ;
219- output ( $ e ->getMessage () . "\n" ) ;
205+ echo "Error loading JSON schema file \n" ;
206+ echo $ urlSchema . "\n" ;
207+ echo $ e ->getMessage () . "\n" ;
220208 exit (2 );
221209}
222210$ refResolver = new JsonSchema \SchemaStorage ($ retriever , $ resolver );
@@ -233,19 +221,17 @@ try {
233221 $ validator ->check ($ data , $ schema );
234222
235223 if ($ validator ->isValid ()) {
236- if (isset ($ arOptions ['--verbose ' ])) {
237- output ("OK. The supplied JSON validates against the schema. \n" );
238- }
224+ echo "OK. The supplied JSON validates against the schema. \n" ;
239225 } else {
240- output ( "JSON does not validate. Violations: \n" ) ;
226+ echo "JSON does not validate. Violations: \n" ;
241227 foreach ($ validator ->getErrors () as $ error ) {
242- output ( sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]) );
228+ echo sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]);
243229 }
244230 exit (23 );
245231 }
246232} catch (Exception $ e ) {
247- output ( "JSON does not validate. Error: \n" ) ;
248- output ( $ e ->getMessage () . "\n" ) ;
249- output ( "Error code: " . $ e ->getCode () . "\n" ) ;
233+ echo "JSON does not validate. Error: \n" ;
234+ echo $ e ->getMessage () . "\n" ;
235+ echo "Error code: " . $ e ->getCode () . "\n" ;
250236 exit (24 );
251237}
0 commit comments