@@ -17,7 +17,6 @@ function __autoload($className)
17
17
{
18
18
$ className = ltrim ($ className , '\\' );
19
19
$ fileName = '' ;
20
- $ namespace = '' ;
21
20
if ($ lastNsPos = strrpos ($ className , '\\' )) {
22
21
$ namespace = substr ($ className , 0 , $ lastNsPos );
23
22
$ className = substr ($ className , $ lastNsPos + 1 );
@@ -29,6 +28,49 @@ function __autoload($className)
29
28
}
30
29
}
31
30
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
+
32
74
/**
33
75
* Show the json parse error that happened last
34
76
*
@@ -44,7 +86,7 @@ function showJsonError()
44
86
}
45
87
}
46
88
47
- echo 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ;
89
+ output ( 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ) ;
48
90
}
49
91
50
92
function getUrlFromPath ($ path )
@@ -84,48 +126,18 @@ function parseHeaderValue($headerValue)
84
126
return $ arData ;
85
127
}
86
128
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 ;
101
138
}
102
139
}
103
140
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
-
129
141
$ urlData = getUrlFromPath ($ pathData );
130
142
131
143
$ context = stream_context_create (
@@ -141,14 +153,14 @@ $context = stream_context_create(
141
153
);
142
154
$ dataString = file_get_contents ($ pathData , false , $ context );
143
155
if ($ dataString == '' ) {
144
- echo "Data file is not readable or empty. \n" ;
156
+ output ( "Data file is not readable or empty. \n" ) ;
145
157
exit (3 );
146
158
}
147
159
148
160
$ data = json_decode ($ dataString );
149
161
unset($ dataString );
150
162
if ($ data === null ) {
151
- echo "Error loading JSON data file \n" ;
163
+ output ( "Error loading JSON data file \n" ) ;
152
164
showJsonError ();
153
165
exit (5 );
154
166
}
@@ -182,9 +194,9 @@ if ($pathSchema === null) {
182
194
183
195
//autodetect schema
184
196
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" ) ;
188
200
exit (6 );
189
201
}
190
202
}
@@ -202,9 +214,9 @@ try {
202
214
exit ();
203
215
}
204
216
} 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" ) ;
208
220
exit (2 );
209
221
}
210
222
$ refResolver = new JsonSchema \SchemaStorage ($ retriever , $ resolver );
@@ -221,17 +233,19 @@ try {
221
233
$ validator ->check ($ data , $ schema );
222
234
223
235
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
+ }
225
239
} else {
226
- echo "JSON does not validate. Violations: \n" ;
240
+ output ( "JSON does not validate. Violations: \n" ) ;
227
241
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 ' ]) );
229
243
}
230
244
exit (23 );
231
245
}
232
246
} 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" ) ;
236
250
exit (24 );
237
251
}
0 commit comments