-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathValidateSchema.php
60 lines (52 loc) · 1.74 KB
/
ValidateSchema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Swaggest\JsonCli;
use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;
use Yaoi\Command;
class ValidateSchema extends Base
{
public $schema;
public $data;
/**
* @param Command\Definition $definition
* @param \stdClass|static $options
*/
static function setUpDefinition(Command\Definition $definition, $options)
{
$options->data = Command\Option::create()->setIsUnnamed()->setIsRequired()
->setDescription('Path to data (JSON/YAML)');
$options->schema = Command\Option::create()->setIsUnnamed()
->setDescription('Path to schema, default JSON Schema');
}
/**
* @throws ExitCode
* @throws \Swaggest\JsonSchema\Exception
*/
public function performAction()
{
if ($this->schema) {
$schemaData = $this->readData($this->schema);
try {
$schema = Schema::import($schemaData);
} catch (InvalidValue $e) {
$this->response->error('Invalid schema');
$this->response->addContent($e->getMessage());
throw new ExitCode('', 1);
} catch (\Exception $e) {
$this->response->error('Failed to import schema:' . $e->getMessage());
throw new ExitCode('', 1);
}
} else {
$schema = Schema::schema();
}
$data = $this->readData($this->data);
try {
$schema->in($data);
$this->response->success('Data is valid');
} catch (InvalidValue $exception) {
$this->response->error('Data is invalid');
$this->response->addContent($exception->getMessage());
throw new ExitCode('', 1);
}
}
}