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