Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Schemas/ArraySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public function __construct(

/**
* @inheritDoc
*
* @throws Exception
*/
protected function validateSchema(mixed $value): void
{
if (!is_array($value)) {
$this->error($this->customMessages['type'] ?? "Provided value must be an array");
$this->error($this->customMessages['type'] ?? 'Provided value must be an array');

return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Schemas/BaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function optional(): static
return $this;
}

public function refine(callable $callback, $customMessage = "Custom validation error"): static
public function refine(callable $callback, $customMessage = 'Custom validation error'): static
{
$this->customMessages['refine'] = $customMessage;
$this->callback = $callback(...);
Expand Down Expand Up @@ -100,7 +100,7 @@ public function validate(mixed $value): bool
*
* @return void
*/
protected final function error(string $message): void
final protected function error(string $message): void
{
$this->errorMessages[] = $message;
}
Expand All @@ -110,7 +110,7 @@ protected final function error(string $message): void
*
* @return void
*/
protected final function setErrorMessages(array $errors): void
final protected function setErrorMessages(array $errors): void
{
$this->errorMessages = $errors;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Schemas/NumericSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NumericSchema extends BaseSchema
/**
* @return $this
*/
public function integer(string $customMessage = "Value must be an integer"): static
public function integer(string $customMessage = 'Value must be an integer'): static
{
$this->integerOnly = true;
$this->customMessages['integerOnly'] = $customMessage;
Expand All @@ -28,7 +28,7 @@ public function integer(string $customMessage = "Value must be an integer"): sta
/**
* @return $this
*/
public function float(string $customMessage = "Value must be a float"): static
public function float(string $customMessage = 'Value must be a float'): static
{
$this->floatOnly = true;
$this->customMessages['floatOnly'] = $customMessage;
Expand All @@ -39,7 +39,7 @@ public function float(string $customMessage = "Value must be a float"): static
/**
* @return $this
*/
public function positive(string $customMessage = "Value must be positive"): static
public function positive(string $customMessage = 'Value must be positive'): static
{
$this->positiveOnly = true;
$this->customMessages['positiveOnly'] = $customMessage;
Expand All @@ -53,7 +53,7 @@ public function positive(string $customMessage = "Value must be positive"): stat
protected function validateSchema(mixed $value): void
{
if (!is_numeric($value)) {
$this->error($this->customMessages['type'] ?? "Value must be a number");
$this->error($this->customMessages['type'] ?? 'Value must be a number');
}

if (!is_null($this->min) && $value < $this->min) {
Expand Down
3 changes: 2 additions & 1 deletion src/Schemas/ObjectSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ObjectSchema extends ArraySchema
protected function validateSchema(mixed $value): void
{
if (!is_object($value)) {
$this->error($this->customMessages['type'] ?? "Provided value must be an object");
$this->error($this->customMessages['type'] ?? 'Provided value must be an object');

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Schemas/StringSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class StringSchema extends BaseSchema
protected function validateSchema(mixed $value): void
{
if (!is_string($value)) {
$this->error($this->customMessages['type'] ?? "Value must be a string");
$this->error($this->customMessages['type'] ?? 'Value must be a string');
}

if (!is_null($this->min)) {
Expand Down
1 change: 0 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Webdevcave\SchemaValidator;

use Webdevcave\SchemaValidator\Schemas\ArraySchema;
use Webdevcave\SchemaValidator\Schemas\BaseSchema;
use Webdevcave\SchemaValidator\Schemas\NumericSchema;
use Webdevcave\SchemaValidator\Schemas\ObjectSchema;
use Webdevcave\SchemaValidator\Schemas\StringSchema;
Expand Down
26 changes: 13 additions & 13 deletions tests/Schemas/ArraySchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ public function testTypeCheck(): void
{
$schema = Validator::array();

$this->assertFalse($schema->validate(1), "Array schema should not validate integers");
$this->assertFalse($schema->validate(1.1), "Array schema should not validate floats");
$this->assertFalse($schema->validate('str'), "Array schema should not validate strings");
$this->assertFalse($schema->validate(new stdClass()), "Array schema should not validate objects");
$this->assertFalse($schema->validate(1), 'Array schema should not validate integers');
$this->assertFalse($schema->validate(1.1), 'Array schema should not validate floats');
$this->assertFalse($schema->validate('str'), 'Array schema should not validate strings');
$this->assertFalse($schema->validate(new stdClass()), 'Array schema should not validate objects');

$this->assertTrue($schema->validate([1, 2, 3]), "Array schema should validate arrays");
$this->assertTrue($schema->validate([1, 2, 3]), 'Array schema should validate arrays');
}

public function testDataValidation(): void
{
$positiveIntCheckMessage = 'must be positive';
$schema = Validator::array(['name' => Validator::string()->min(3), 'age' => Validator::numeric()->integer()->positive($positiveIntCheckMessage),]);
$schema = Validator::array(['name' => Validator::string()->min(3), 'age' => Validator::numeric()->integer()->positive($positiveIntCheckMessage)]);

$validData = ['name' => 'Carlos', 'age' => 35,];
$this->assertTrue($schema->validate($validData), "Should return true when dataset is valid");
$validData = ['name' => 'Carlos', 'age' => 35];
$this->assertTrue($schema->validate($validData), 'Should return true when dataset is valid');

$invalidData = ['name' => 'John', 'age' => -10,];
$this->assertFalse($schema->validate($invalidData), "Should return false when dataset is not valid");
$invalidData = ['name' => 'John', 'age' => -10];
$this->assertFalse($schema->validate($invalidData), 'Should return false when dataset is not valid');
$this->assertEquals(['age' => [$positiveIntCheckMessage]], $schema->errorMessages(), 'Error messages doesn\'t match');
}

Expand All @@ -49,10 +49,10 @@ public function testWildcardIndexValidation()
$schema = Validator::array(['*' => Validator::numeric()->integer($errorMessage)]);

$validData = [1, 2, 3];
$this->assertTrue($schema->validate($validData), "Error evaluating wildcard index with valid data");
$this->assertTrue($schema->validate($validData), 'Error evaluating wildcard index with valid data');

$invalidData = [null];
$this->assertFalse($schema->validate($invalidData), "Error evaluating wildcard index with invalid data");
$this->assertFalse($schema->validate($invalidData), 'Error evaluating wildcard index with invalid data');
}

public function testOptionalCheckShouldPassOnEmptyArray(): void
Expand All @@ -74,6 +74,6 @@ public function testDoubleRuleValidation(): void
$data = [
'name' => str_repeat('a', 10),
];
$this->assertTrue($schema->validate($data), "Array schema should accept double rule");
$this->assertTrue($schema->validate($data), 'Array schema should accept double rule');
}
}
3 changes: 2 additions & 1 deletion tests/Schemas/BaseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testSetGetRule(): void

$this->schema->equals($value);
$this->assertEquals(
$value, $this->schema->equals(),
$value,
$this->schema->equals(),
'Schema should be able to set and get a rule value'
);
}
Expand Down
34 changes: 17 additions & 17 deletions tests/Schemas/NumericSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,57 @@ public function testMinValue(): void
$schema = new NumericSchema();
$schema->min(10);

$this->assertTrue($schema->validate(10), "Failed asserting equals than minimum value");
$this->assertTrue($schema->validate(11), "Failed asserting number greater than minimum value");
$this->assertFalse($schema->validate(9), "Failed asserting number lower than minimum value");
$this->assertTrue($schema->validate(10), 'Failed asserting equals than minimum value');
$this->assertTrue($schema->validate(11), 'Failed asserting number greater than minimum value');
$this->assertFalse($schema->validate(9), 'Failed asserting number lower than minimum value');
}

public function testMaxValue(): void
{
$schema = new NumericSchema();
$schema->max(10);

$this->assertTrue($schema->validate(10), "Failed asserting equals than maximum value");
$this->assertFalse($schema->validate(11), "Failed asserting number greater than maximum value");
$this->assertTrue($schema->validate(9), "Failed asserting number lower than maximum value");
$this->assertTrue($schema->validate(10), 'Failed asserting equals than maximum value');
$this->assertFalse($schema->validate(11), 'Failed asserting number greater than maximum value');
$this->assertTrue($schema->validate(9), 'Failed asserting number lower than maximum value');
}

public function testRequirePositive(): void
{
$schema = new NumericSchema();
$schema->positive();

$this->assertTrue($schema->validate(1), "Failed asserting positive value");
$this->assertFalse($schema->validate(-1), "Failed asserting negative value (positive required)");
$this->assertTrue($schema->validate(1), 'Failed asserting positive value');
$this->assertFalse($schema->validate(-1), 'Failed asserting negative value (positive required)');
}

public function testTypeValidation(): void
{
$schema = new NumericSchema();

$this->assertFalse($schema->validate('str'), "Numeric schema should not validate strings");
$this->assertFalse($schema->validate(new stdClass()), "Numeric schema should not validate objects");
$this->assertFalse($schema->validate([1,2,3]), "Numeric schema should validate arrays");
$this->assertFalse($schema->validate('str'), 'Numeric schema should not validate strings');
$this->assertFalse($schema->validate(new stdClass()), 'Numeric schema should not validate objects');
$this->assertFalse($schema->validate([1, 2, 3]), 'Numeric schema should validate arrays');

$this->assertTrue($schema->validate(1), "Numeric schema should validate integers");
$this->assertTrue($schema->validate(1.1), "Numeric schema should validate floats");
$this->assertTrue($schema->validate(1), 'Numeric schema should validate integers');
$this->assertTrue($schema->validate(1.1), 'Numeric schema should validate floats');
}

public function testExclusiveIntegerValidation(): void
{
$schema = new NumericSchema();
$schema->integer();

$this->assertTrue($schema->validate(1), "Numeric schema should validate integers (int only)");
$this->assertFalse($schema->validate(1.1), "Numeric schema should not validate floats (int only)");
$this->assertTrue($schema->validate(1), 'Numeric schema should validate integers (int only)');
$this->assertFalse($schema->validate(1.1), 'Numeric schema should not validate floats (int only)');
}

public function testExclusiveFloatValidation(): void
{
$schema = new NumericSchema();
$schema->float();

$this->assertFalse($schema->validate(1), "Numeric schema should not validate integers (floats only)");
$this->assertTrue($schema->validate(1.1), "Numeric schema should validate floats (floats only)");
$this->assertFalse($schema->validate(1), 'Numeric schema should not validate integers (floats only)');
$this->assertTrue($schema->validate(1.1), 'Numeric schema should validate floats (floats only)');
}
}
12 changes: 6 additions & 6 deletions tests/Schemas/ObjectSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Webdevcave\SchemaValidator\Tests\Schemas;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
use Webdevcave\SchemaValidator\Schemas\ArraySchema;
use Webdevcave\SchemaValidator\Schemas\BaseSchema;
use Webdevcave\SchemaValidator\Schemas\ObjectSchema;
use PHPUnit\Framework\TestCase;
use Webdevcave\SchemaValidator\Validator;

#[CoversClass(ObjectSchema::class)]
Expand All @@ -20,11 +20,11 @@ public function testTypeCheck(): void
{
$schema = Validator::object();

$this->assertFalse($schema->validate(1), "Array schema should not validate integers");
$this->assertFalse($schema->validate(1.1), "Array schema should not validate floats");
$this->assertFalse($schema->validate('str'), "Array schema should not validate strings");
$this->assertFalse($schema->validate([1, 2, 3]), "Array schema should not validate arrays");
$this->assertFalse($schema->validate(1), 'Array schema should not validate integers');
$this->assertFalse($schema->validate(1.1), 'Array schema should not validate floats');
$this->assertFalse($schema->validate('str'), 'Array schema should not validate strings');
$this->assertFalse($schema->validate([1, 2, 3]), 'Array schema should not validate arrays');

$this->assertTrue($schema->validate(new stdClass()), "Array schema should validate object");
$this->assertTrue($schema->validate(new stdClass()), 'Array schema should validate object');
}
}
4 changes: 2 additions & 2 deletions tests/Schemas/StringSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Webdevcave\SchemaValidator\Tests\Schemas;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
use Webdevcave\SchemaValidator\Schemas\BaseSchema;
use Webdevcave\SchemaValidator\Schemas\StringSchema;
use PHPUnit\Framework\TestCase;
use Webdevcave\SchemaValidator\Validator;

#[CoversClass(Validator::class)]
Expand All @@ -21,7 +21,7 @@ public function testMustValidateOnlyStrings(): void
$this->assertFalse($schema->validate(1), 'Should not validate integers');
$this->assertFalse($schema->validate(1.2), 'Should not validate floats');
$this->assertFalse($schema->validate([]), 'Should not validate arrays');
$this->assertFalse($schema->validate(new stdClass), 'Should not validate objects');
$this->assertFalse($schema->validate(new stdClass()), 'Should not validate objects');
$this->assertFalse($schema->validate(null), 'Should not validate null');
$this->assertTrue($schema->validate('my string'), 'Should validate strings');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Webdevcave\SchemaValidator\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Webdevcave\SchemaValidator\Schemas\ArraySchema;
use Webdevcave\SchemaValidator\Schemas\NumericSchema;
use Webdevcave\SchemaValidator\Schemas\ObjectSchema;
use Webdevcave\SchemaValidator\Schemas\StringSchema;
use Webdevcave\SchemaValidator\Validator;
use PHPUnit\Framework\TestCase;

#[CoversClass(Validator::class)]
#[CoversClass(StringSchema::class)]
Expand Down
Loading