Skip to content

Commit

Permalink
fixed validations of relative paths against json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch committed Jun 8, 2017
1 parent 28b5f4d commit 71b1ab0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ At the moment, the integration with frictionlessdata repo is not working, you ca
* Login with GitHub which has permissions
* click "Update"
* all releases from GitHub appear as releases on Packagist

## updating frictionlessdata schemas

The json schemas for the frictionlessdata specs are stored locally as part of the package.

They might change from time to time, to donwnload the latest specs run `composer update_registry`
16 changes: 16 additions & 0 deletions src/Validators/DatapackageValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ protected function getValidationProfile()
return Registry::getDatapackageValidationProfile($this->descriptor);
}

protected function getDescriptorForValidation()
{
// add base path to uri fields
// TODO: find a more elegant way to do it with support for registring custom url fields
$descriptor = clone $this->descriptor;
if (isset($descriptor->resources)) {
foreach ($descriptor->resources as &$resource) {
$resource = clone $resource;
foreach ($resource->data as &$url) {
$url = "file://".$url;
}
}
}
return $descriptor;
}

protected function validateSchema()
{
parent::validateSchema();
Expand Down
8 changes: 7 additions & 1 deletion src/Validators/ResourceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ protected function getValidationProfile()

protected function getDescriptorForValidation()
{
return $this->descriptor;
// add base path to uri fields
// TODO: find a more elegant way to do it with support for registring custom url fields
$descriptor = clone $this->descriptor;
foreach ($descriptor->data as &$url) {
$url = "file://".$url;
}
return $descriptor;
}

protected function getValidationErrorMessage($error)
Expand Down
29 changes: 22 additions & 7 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,39 @@ class FactoryTest extends TestCase
{
public function testRegisterDatapackageClass()
{
// the MyCustomDatapackage datapackage class checks for a myCustomDatapackage property on the descriptor
// it is only used when this attribute exists
Factory::registerDatapackageClass(
"frictionlessdata\\datapackage\\tests\\Mocks\\MyCustomDatapackage"
);
// descriptor without the myCustomDatapackage property
$descriptor = (object)[
"name" => "my-custom-datapackage",
"resources" => [
(object)["name" => "my-custom-resource", "data" => ["tests/fixtures/foo.txt"]]
]
];
Factory::registerDatapackageClass(
"frictionlessdata\\datapackage\\tests\\Mocks\\MyCustomDatapackage"
);
// descriptor without the custom property
// get a datapackage object based on this descriptor
$datapackage = Factory::datapackage($descriptor);
// the custom datapackage is not used (because the myCustomDatapackage property didn't exist)
$this->assertEquals(
// custom datapackage is not used
"frictionlessdata\\datapackage\\Datapackages\\DefaultDatapackage",
get_class($datapackage)
);
// add the custom property - which is checked by MyCustomDatapackage
// add the myCustomDatapackage property to the descriptor
$descriptor->myCustomDatapackage = true;
// get a datapackage object
$datapackage = Factory::datapackage($descriptor);
// voila - we got a MyCustomDatapackage class
$this->assertEquals(
"frictionlessdata\\datapackage\\tests\\Mocks\\MyCustomDatapackage",
get_class($datapackage)
);
// make sure to clear the custom datapackage class we registered
Factory::clearRegisteredDatapackageClasses();
// create a datapackage object from the descriptor with the myCustomDatapackage property
$datapackage = Factory::datapackage($descriptor);
// got the normal default datapackage class (because we cleared the custom registered classes)
$this->assertEquals(
"frictionlessdata\\datapackage\\Datapackages\\DefaultDatapackage",
get_class($datapackage)
Expand All @@ -41,21 +49,28 @@ public function testRegisterDatapackageClass()

public function testRegisterResourceClass()
{
$descriptor = (object)["name" => "my-custom-resource", "data" => ["tests/fixtures/foo.txt"]];
// register a custom resource class which is used for resources that have the goGoPowerRangers property
Factory::registerResourceClass(
"frictionlessdata\\datapackage\\tests\\Mocks\\MyCustomResource"
);
// a descriptor without the goGoPowerRangers property
$descriptor = (object)["name" => "my-custom-resource", "data" => ["tests/fixtures/foo.txt"]];
// create a resource object based on the descriptor
$resource = Factory::resource($descriptor);
// got a normal resource
$this->assertEquals(
"frictionlessdata\\datapackage\\Resources\DefaultResource",
get_class($resource)
);
// add the goGoPowerRangers property
$descriptor->goGoPowerRangers = true;
$resource = Factory::resource($descriptor);
// got the custom resource
$this->assertEquals(
"frictionlessdata\\datapackage\\tests\\Mocks\\MyCustomResource",
get_class($resource)
);
// clear the registered classes and ensure it's cleared
Factory::clearRegisteredResourceClasses();
$resource = Factory::resource($descriptor);
$this->assertEquals(
Expand Down

0 comments on commit 71b1ab0

Please sign in to comment.