for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace frictionlessdata\tableschema;
/**
* validates a table schema descriptor object
* returns a list of validation errors
*/
class SchemaValidator
{
* @param object $descriptor
* @return SchemaValidationError[]
public static function validate($descriptor)
$validator = new self($descriptor);
return $validator->getValidationErrors();
}
public function __construct($descriptor)
$this->descriptor = $descriptor;
descriptor
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
$this->errors = [];
errors
public function getValidationErrors()
$this->validateSchema();
return $this->errors;
* @param integer $code
* @param mixed $extraDetails
protected function addError($code, $extraDetails=null)
$this->errors[] = new SchemaValidationError($code, $extraDetails);
protected function validateSchema()
// Validate
$validator = new \JsonSchema\Validator();
$validator->validate(
$this->descriptor,
(object)['$ref' => 'file://' . realpath(dirname(__FILE__)).'/schemas/table-schema.json']
);
if (!$validator->isValid()) {
foreach ($validator->getErrors() as $error) {
$this->addError(
SchemaValidationError::SCHEMA_VIOLATION,
sprintf("[%s] %s", $error['property'], $error['message'])
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: