|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace frictionlessdata\tableschema; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* validates a table schema descriptor object |
|
7
|
|
|
* returns a list of validation errors. |
|
8
|
|
|
*/ |
|
9
|
|
|
class SchemaValidator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param object $descriptor |
|
13
|
|
|
* |
|
14
|
|
|
* @return SchemaValidationError[] |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function validate($descriptor) |
|
17
|
|
|
{ |
|
18
|
|
|
$validator = new self($descriptor); |
|
19
|
|
|
|
|
20
|
|
|
return $validator->getValidationErrors(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param object $descriptor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($descriptor) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->descriptor = $descriptor; |
|
|
|
|
|
|
29
|
|
|
$this->errors = []; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return SchemaValidationError[] |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getValidationErrors() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->errors = []; |
|
38
|
|
|
$this->validateSchema(); |
|
39
|
|
|
if (count($this->errors) == 0) { |
|
40
|
|
|
$this->validateKeys(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->errors; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $code |
|
48
|
|
|
* @param mixed $extraDetails |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function addError($code, $extraDetails = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->errors[] = new SchemaValidationError($code, $extraDetails); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function validateSchema() |
|
56
|
|
|
{ |
|
57
|
|
|
// Validate |
|
58
|
|
|
$validator = new \JsonSchema\Validator(); |
|
59
|
|
|
$descriptor = json_decode(json_encode($this->descriptor)); |
|
60
|
|
|
$validator->validate( |
|
61
|
|
|
$descriptor, |
|
62
|
|
|
(object) ['$ref' => 'file://'.realpath(dirname(__FILE__)).'/schemas/table-schema.json'] |
|
63
|
|
|
); |
|
64
|
|
|
if (!$validator->isValid()) { |
|
65
|
|
|
foreach ($validator->getErrors() as $error) { |
|
66
|
|
|
$this->addError( |
|
67
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
|
68
|
|
|
sprintf('[%s] %s', $error['property'], $error['message']) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function validateKeys() |
|
75
|
|
|
{ |
|
76
|
|
|
$fieldNames = array_map(function ($field) { |
|
77
|
|
|
return $field->name; |
|
78
|
|
|
}, $this->descriptor->fields); |
|
79
|
|
|
if (isset($this->descriptor->primaryKey)) { |
|
80
|
|
|
$primaryKey = is_array($this->descriptor->primaryKey) ? $this->descriptor->primaryKey : [$this->descriptor->primaryKey]; |
|
81
|
|
|
foreach ($primaryKey as $primaryKeyField) { |
|
82
|
|
|
if (!in_array($primaryKeyField, $fieldNames)) { |
|
83
|
|
|
$this->addError( |
|
84
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
|
85
|
|
|
"primary key must refer to a field name ({$primaryKeyField})" |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
if (isset($this->descriptor->foreignKeys)) { |
|
91
|
|
|
foreach ($this->descriptor->foreignKeys as $foreignKey) { |
|
92
|
|
|
$fields = is_array($foreignKey->fields) ? $foreignKey->fields : [$foreignKey->fields]; |
|
93
|
|
|
foreach ($fields as $field) { |
|
94
|
|
|
if (!in_array($field, $fieldNames)) { |
|
95
|
|
|
$this->addError( |
|
96
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
|
97
|
|
|
"foreign key fields must refer to a field name ({$field})" |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
if ($foreignKey->reference->resource == '') { |
|
102
|
|
|
// empty resource = reference to self |
|
103
|
|
|
foreach ($foreignKey->reference->fields as $field) { |
|
104
|
|
|
if (!in_array($field, $fieldNames)) { |
|
105
|
|
|
$this->addError( |
|
106
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
|
107
|
|
|
"foreign key reference to self must refer to a field name ({$field})" |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: