|
16 | 16 | exit(1); // exit if not run via CLI |
17 | 17 | } |
18 | 18 |
|
19 | | -echo 'JSON Schema Validator @git-version@ by Andreas Wahlen'.PHP_EOL.PHP_EOL; |
20 | | - |
21 | | -if(!class_exists(\Opis\JsonSchema\Validator::class)){ |
22 | | - echo "\033[31mFATAL: Opis\JsonSchema\Validator not found, is it required in your composer.json?\033[0m".PHP_EOL.PHP_EOL; |
23 | | - exit(10); |
24 | | -} |
| 19 | +echo 'JSON Schema Validator @git-version@ by nerou GmbH'.PHP_EOL.PHP_EOL; |
25 | 20 |
|
26 | 21 | $cliArgs = new CLIParser($_SERVER['argv']); |
27 | 22 | $cliArgs->setAllowedOptions([]); |
|
36 | 31 | // collect schema files to validate |
37 | 32 | try { |
38 | 33 | $files = collectSchemaFiles($cliArgs->getCommands()); |
39 | | -} catch(InvalidArgumentException $ex){ |
| 34 | +} catch(\InvalidArgumentException $ex){ |
40 | 35 | echo "\033[31m".$ex->getMessage()."\033[0m".PHP_EOL; |
41 | 36 | exit(20); |
42 | 37 | } |
43 | 38 |
|
44 | 39 | $cwd = getcwd(); |
| 40 | +if($cwd === false){ |
| 41 | + throw new \UnexpectedValueException('unable to get current working directory'); |
| 42 | +} |
45 | 43 | $validator = new Validator(); |
46 | 44 | $validator->resolver()->registerPrefix('https://json-schema.org/', dirname(__DIR__).'/resource'); |
47 | 45 | $validator->resolver()->registerPrefix('http://json-schema.org/', dirname(__DIR__).'/resource'); |
|
52 | 50 | echo '['.str_pad(strval($index+1), $countChars, ' ', STR_PAD_LEFT).'/'.count($files).'] '. |
53 | 51 | $formattedFilename; |
54 | 52 | try { |
| 53 | + $fileContent = file_get_contents($file); |
| 54 | + if($fileContent === false){ |
| 55 | + throw new \UnexpectedValueException('file '.$file.' could not be read'); |
| 56 | + } |
55 | 57 | /** |
56 | 58 | * @var scalar|object $json |
57 | 59 | */ |
58 | | - $json = json_decode(file_get_contents($file), flags: JSON_THROW_ON_ERROR); |
| 60 | + $json = json_decode($fileContent, flags: JSON_THROW_ON_ERROR); |
59 | 61 | } catch(JsonException $ex){ |
60 | 62 | echo "[\033[31mSYNTAX ERROR\033[0m]".PHP_EOL; |
61 | 63 | $errors++; |
|
65 | 67 | try { |
66 | 68 | /** |
67 | 69 | * @psalm-suppress PossiblyInvalidPropertyFetch |
| 70 | + * @var string $schema |
68 | 71 | */ |
69 | | - $result = $validator->validate($json, $json->{'$schema'} ?? 'https://json-schema.org/draft/2020-12/schema'); |
| 72 | + $schema = $json->{'$schema'} ?? 'https://json-schema.org/draft/2020-12/schema'; |
| 73 | + $result = $validator->validate($json, $schema); |
70 | 74 | if(!$result->isValid()){ |
71 | 75 | $errorMessage = json_encode(((new ErrorFormatter())->format($result->error())), |
72 | 76 | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); |
|
86 | 90 | } |
87 | 91 | } |
88 | 92 | } |
89 | | -echo 'consumed '.number_format(microtime(true) - $startTime, 3).' s of processing time and '. |
90 | | - (memory_get_peak_usage(true) / 1024 / 1024).' MiB of memory'.PHP_EOL.PHP_EOL; |
| 93 | +echo sprintf('Time: %.3s s, Memory: %.2f MiB'.PHP_EOL.PHP_EOL, |
| 94 | + microtime(true) - $startTime, |
| 95 | + ((float) memory_get_peak_usage(true)) / 1024. / 1024. |
| 96 | +); |
91 | 97 | echo 'JSON Schemas: '.count($files).', errors: '.$errors.PHP_EOL; |
92 | 98 | if($errors > 0){ |
93 | 99 | exit(30); |
|
0 commit comments