Skip to content

Commit

Permalink
Partial validation support added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Nov 7, 2018
1 parent 4f8aac4 commit 2d7be4e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
18 changes: 16 additions & 2 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,20 @@ public function __unset($name)

public function validate() : bool
{
$validation = (new Validator())->schemaValidation($this->data, $this->model->getSchema());
return $this->validateData($this->data, $this->model->getSchema());
}

public function validatePartially(array $requiredFields) : bool
{
$model = $this->model->getModel();
$model->required = $requiredFields;
$schema = $this->model->getSchema($model);
return $this->validateData($this->data, $schema);
}

private function validateData($data, $schema) : bool
{
$validation = (new Validator())->schemaValidation($data, $schema);
if (!$validation->isValid()) {
$errors = $validation->getErrors();
$message = 'Data validation failed.' . PHP_EOL;
Expand All @@ -78,6 +91,7 @@ public function validate() : bool
return true;
}


public function equals($rightHandedObject) : bool
{
return (string) $this === (string) $rightHandedObject;
Expand All @@ -104,6 +118,6 @@ public static function createFromJsonFile($filePath) : Entity

public static function createFromJson($json) : Entity
{
return new static(new Model($json));
return new self(new Model($json));
}
}
28 changes: 22 additions & 6 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,39 @@

final class Model
{
private $schema;
private $model;
private $requiredFields;

public function __construct(string $jsonSchema)
{
$this->schema = Schema::fromJsonString($jsonSchema);
$this->model = json_decode($jsonSchema);
if (json_last_error() === JSON_ERROR_NONE) {
$this->requiredFields = $this->model->required;
}
}

public function getSchema() : Schema
public function getModel() : \stdClass
{
return $this->model;
}
public function getRequiredFields() : array
{
return $this->schema;
return $this->requiredFields;
}

public function getSchema(?\stdClass $model = null) : Schema
{
if ($model !== null) {
return new Schema($model);
}
return new Schema($this->model);
}

public static function fromJsonFile(string $filePath) : Model
public static function createFromJsonFile(string $filePath) : Model
{
if (!file_exists($filePath)) {
throw new UnexpectedValueException(sprintf('Model definition file (%s) does not exist!', $filePath));
}
return new static(file_get_contents($filePath));
return new self(file_get_contents($filePath));
}
}
5 changes: 4 additions & 1 deletion tests/resources/test-schema-invalid-json.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"test": }
{
"required": ["id"],
"test":
}
18 changes: 17 additions & 1 deletion tests/unit/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function shouldReturnEntityObjectSuccessfully() : void
$this->assertFalse(isset($entity->name));
}

/**
* @test
*/
public function shouldValidatePartiallySuccessfully() : void
{
$entity = Entity::createFromJsonFile(__DIR__.'/../resources/test-schema.json');
$entity->name = 'John Doe';
$entity->age = 31;
$requiredFields = ['name', 'age'];
$this->assertTrue($entity->validatePartially($requiredFields));
$requiredFields = ['name', 'age', 'email'];
$this->expectException(\Selami\Entity\Exception\InvalidArgumentException::class);
$entity->validatePartially($requiredFields);
}
/**
* @test
*/
Expand Down Expand Up @@ -80,13 +94,15 @@ public function shouldCompareTwoEntityObjectSuccessfully() : void
}




/**
* @test
* @expectedException \Selami\Entity\Exception\InvalidArgumentException
*/
public function shouldFailForRequiredInput() : void
{
$model = Model::fromJsonFile(__DIR__.'/../resources/test-schema.json');
$model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema.json');
$entity = new Entity($model);
$entity->name = 'John Doe';
$entity->age = 31;
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ protected function _after()
*/
public function shouldReturnModelObjectSuccessfully() : void
{
$model = Model::fromJsonFile(__DIR__.'/../resources/test-schema.json');
$model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema.json');
$schema = $model->getSchema();
$this->assertEquals('http://api.example.com/profile.json#', $schema->id());

$jsonSchema = file_get_contents(__DIR__.'/../resources/test-schema.json');
$tmpSchema = json_decode($jsonSchema);
$requiredFields = $tmpSchema->required;
$model = new Model($jsonSchema);
$this->assertEquals($tmpSchema, $model->getModel());
$modelRequiredFields = $model->getRequiredFields();
$this->assertEquals($modelRequiredFields, $requiredFields);
$schema = $model->getSchema($tmpSchema);
$this->assertEquals('http://api.example.com/profile.json#', $schema->id());
}

/**
Expand All @@ -34,7 +44,7 @@ public function shouldReturnModelObjectSuccessfully() : void
*/
public function shouldFailForAFileThatDoesNotExist() : void
{
$model = Model::fromJsonFile(__DIR__.'/../resources/test-schema-does-not-exist.json');
$model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema-does-not-exist.json');
$model->getSchema();
}

Expand All @@ -44,7 +54,7 @@ public function shouldFailForAFileThatDoesNotExist() : void
*/
public function shouldFailForAFileThatContainsInvalidJson() : void
{
$model = Model::fromJsonFile(__DIR__.'/../resources/test-schema-invalid-json.json');
$model = Model::createFromJsonFile(__DIR__.'/../resources/test-schema-invalid-json.json');
$model->getSchema();
}
}

0 comments on commit 2d7be4e

Please sign in to comment.