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
17 changes: 17 additions & 0 deletions src/ProvidesValidationRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2019 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema;

interface ProvidesValidationRules
{
public static function validationRules(): array;
}
12 changes: 8 additions & 4 deletions src/RecordLogic/TypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static function getTypeFromClass(string $classOrType, bool $allowNestedSc
}

if($refObj->implementsInterface(JsonSchemaAwareCollection::class)) {
return JsonSchema::array(\call_user_func([$classOrType, '__itemSchema']));
$validation = is_callable([$classOrType, 'validationRules'])? \call_user_func([$classOrType, 'validationRules']) : null;
return JsonSchema::array(\call_user_func([$classOrType, '__itemSchema']), $validation);
}

if($scalarSchemaType = self::determineScalarTypeIfPossible($classOrType)) {
Expand All @@ -41,15 +42,18 @@ public static function getTypeFromClass(string $classOrType, bool $allowNestedSc
private static function determineScalarTypeIfPossible(string $class): ?Type
{
if(is_callable([$class, 'fromString'])) {
return JsonSchema::string();
$validation = is_callable([$class, 'validationRules'])? \call_user_func([$class, 'validationRules']) : null;
return JsonSchema::string($validation);
}

if(is_callable([$class, 'fromInt'])) {
return JsonSchema::integer();
$validation = is_callable([$class, 'validationRules'])? \call_user_func([$class, 'validationRules']) : null;
return JsonSchema::integer($validation);
}

if(is_callable([$class, 'fromFloat'])) {
return JsonSchema::float();
$validation = is_callable([$class, 'validationRules'])? \call_user_func([$class, 'validationRules']) : null;
return JsonSchema::float($validation);
}

if(is_callable([$class, 'fromBool'])) {
Expand Down
58 changes: 58 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use EventEngine\JsonSchema\AnnotatedType;
use EventEngine\JsonSchema\JsonSchema;
use EventEngine\JsonSchema\Type;
use EventEngine\Schema\PayloadSchema;
use EventEngine\Schema\TypeSchema;

Expand All @@ -21,6 +22,11 @@ final class ArrayType implements AnnotatedType, PayloadSchema
use NullableType,
HasAnnotations;

public const MAX_ITEMS = 'maxItems';
public const MIN_ITEMS = 'minItems';
public const UNIQUE_ITEMS = 'uniqueItems';
public const CONTAINS = 'contains';

/**
* @var string|array
*/
Expand All @@ -42,6 +48,58 @@ public function __construct(TypeSchema $itemSchema, array $validation = null)
$this->validation = $validation;
}

public function withMaxItems(int $maxItems): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::MAX_ITEMS] = $maxItems;

$cp->validation = $validation;

return $cp;
}

public function withMinItems(int $minItems): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::MIN_ITEMS] = $minItems;

$cp->validation = $validation;

return $cp;
}

public function withUniqueItems(): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::UNIQUE_ITEMS] = true;

$cp->validation = $validation;

return $cp;
}

public function withContains(TypeSchema $itemSchema): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::CONTAINS] = $itemSchema->toArray();

$cp->validation = $validation;

return $cp;
}

public function toArray(): array
{
return \array_merge([
Expand Down
2 changes: 2 additions & 0 deletions src/Type/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class BoolType implements AnnotatedType
use NullableType,
HasAnnotations;

public const CONST = 'const';

/**
* @var string|array
*/
Expand Down
55 changes: 51 additions & 4 deletions src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class FloatType implements AnnotatedType
use NullableType,
HasAnnotations;

public const MINIMUM = 'minimum';
public const MAXIMUM = 'maximum';
public const MULTIPLE_OF = 'multipleOf';
public const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
public const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
public const ENUM = 'enum';
public const CONST = 'const';

/**
* @var string|array
*/
Expand All @@ -45,7 +53,20 @@ public function withMinimum(float $min): self

$validation = (array) $this->validation;

$validation['minimum'] = $min;
$validation[self::MINIMUM] = $min;

$cp->validation = $validation;

return $cp;
}

public function withExclusiveMinimum(float $exclusiveMin): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::EXCLUSIVE_MINIMUM] = $exclusiveMin;

$cp->validation = $validation;

Expand All @@ -58,7 +79,20 @@ public function withMaximum(float $max): self

$validation = (array) $this->validation;

$validation['maximum'] = $max;
$validation[self::MAXIMUM] = $max;

$cp->validation = $validation;

return $cp;
}

public function withExclusiveMaximum(float $exclusiveMax): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::EXCLUSIVE_MAXIMUM] = $exclusiveMax;

$cp->validation = $validation;

Expand All @@ -71,8 +105,21 @@ public function withRange(float $min, float $max): self

$validation = (array) $this->validation;

$validation['minimum'] = $min;
$validation['maximum'] = $max;
$validation[self::MINIMUM] = $min;
$validation[self::MAXIMUM] = $max;

$cp->validation = $validation;

return $cp;
}

public function withMultipleOf(float $multipleOf): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::MULTIPLE_OF] = $multipleOf;

$cp->validation = $validation;

Expand Down
56 changes: 52 additions & 4 deletions src/Type/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class IntType implements AnnotatedType
use NullableType,
HasAnnotations;

public const MINIMUM = 'minimum';
public const MAXIMUM = 'maximum';
public const MULTIPLE_OF = 'multipleOf';
public const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
public const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
public const ENUM = 'enum';
public const CONST = 'const';


/**
* @var string|array
*/
Expand All @@ -45,7 +54,20 @@ public function withMinimum(int $min): self

$validation = (array) $this->validation;

$validation['minimum'] = $min;
$validation[self::MINIMUM] = $min;

$cp->validation = $validation;

return $cp;
}

public function withExclusiveMinimum(int $exclusiveMin): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::EXCLUSIVE_MINIMUM] = $exclusiveMin;

$cp->validation = $validation;

Expand All @@ -58,7 +80,20 @@ public function withMaximum(int $max): self

$validation = (array) $this->validation;

$validation['maximum'] = $max;
$validation[self::MAXIMUM] = $max;

$cp->validation = $validation;

return $cp;
}

public function withExclusiveMaximum(int $exclusiveMax): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::EXCLUSIVE_MAXIMUM] = $exclusiveMax;

$cp->validation = $validation;

Expand All @@ -71,8 +106,21 @@ public function withRange(int $min, int $max): self

$validation = (array) $this->validation;

$validation['minimum'] = $min;
$validation['maximum'] = $max;
$validation[self::MINIMUM] = $min;
$validation[self::MAXIMUM] = $max;

$cp->validation = $validation;

return $cp;
}

public function withMultipleOf(float $multipleOf): self
{
$cp = clone $this;

$validation = (array) $this->validation;

$validation[self::MULTIPLE_OF] = $multipleOf;

$cp->validation = $validation;

Expand Down
20 changes: 18 additions & 2 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class StringType implements AnnotatedType
use NullableType,
HasAnnotations;

public const PATTERN = 'pattern';
public const FORMAT = 'format';
public const MIN_LENGTH = 'minLength';
public const MAX_LENGTH = 'maxLength';
public const ENUM = 'enum';
public const CONST = 'const';


private $type = JsonSchema::TYPE_STRING;

/**
Expand All @@ -34,15 +42,23 @@ public function __construct(array $validation = null)
public function withMinLength(int $minLength): self
{
$cp = clone $this;
$cp->validation['minLength'] = $minLength;
$cp->validation[self::MIN_LENGTH] = $minLength;

return $cp;
}

public function withMaxLength(int $maxLength): self
{
$cp = clone $this;
$cp->validation[self::MAX_LENGTH] = $maxLength;

return $cp;
}

public function withPattern(string $pattern): self
{
$cp = clone $this;
$cp->validation['pattern'] = $pattern;
$cp->validation[self::PATTERN] = $pattern;

return $cp;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/JsonSchemaAwareRecordLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace EventEngineTest\JsonSchema;

use EventEngine\JsonSchema\JsonSchema;
use EventEngine\JsonSchema\Type\TypeRef;
use EventEngineTest\JsonSchema\Stub\ArrayItemRecord;
use EventEngineTest\JsonSchema\Stub\CollectionItemAllowNestedRecord;
use EventEngineTest\JsonSchema\Stub\CollectionItemRecord;
Expand All @@ -13,6 +12,7 @@
use EventEngineTest\JsonSchema\Stub\NullableScalarPropsRecord;
use EventEngineTest\JsonSchema\Stub\ScalarPropsRecord;
use EventEngineTest\JsonSchema\Stub\VoOptionalPropsRecord;
use EventEngineTest\JsonSchema\Stub\VoProp\UserId;
use EventEngineTest\JsonSchema\Stub\VoPropsRecord;

final class JsonSchemaAwareRecordLogicTest extends BasicTestCase
Expand Down Expand Up @@ -100,7 +100,7 @@ public function it_uses_item_schema_from_collection()
$schema = CollectionItemRecord::__schema();

$expected = JsonSchema::object([
'friends' => JsonSchema::array(JsonSchema::typeRef(ScalarPropsRecord::__type()))
'friends' => JsonSchema::array(JsonSchema::typeRef(ScalarPropsRecord::__type()))->withMaxItems(10)
]);

$this->assertEquals($expected->toArray(), $schema->toArray());
Expand All @@ -127,10 +127,10 @@ public function it_detects_scalar_types_through_method_analysis_of_vo_classes()
$schema = VoPropsRecord::__schema();

$expected = JsonSchema::object([
'userId' => JsonSchema::string(),
'age' => JsonSchema::integer(),
'userId' => JsonSchema::string()->withPattern(UserId::PATTERN),
'age' => JsonSchema::integer()->withRange(0, 150),
'member' => JsonSchema::boolean(),
'score' => JsonSchema::float()
'score' => JsonSchema::float()->withRange(0.1, 1),
]);

$this->assertEquals($expected->toArray(), $schema->toArray());
Expand All @@ -144,11 +144,11 @@ public function it_respects_optional_properties()
$schema = VoOptionalPropsRecord::__schema();

$expected = JsonSchema::object([
'userId' => JsonSchema::string(),
'age' => JsonSchema::integer(),
'userId' => JsonSchema::string()->withPattern(UserId::PATTERN),
'age' => JsonSchema::integer()->withRange(0, 150),
'member' => JsonSchema::boolean(),
], [
'score' => JsonSchema::float()
'score' => JsonSchema::float()->withRange(0.1, 1),
]);

$this->assertEquals($expected->toArray(), $schema->toArray());
Expand Down
Loading