From 34bfad529dcdf1ebf5a68af57bade6a5edb2127d Mon Sep 17 00:00:00 2001 From: romalytvynenko Date: Fri, 28 Jun 2024 10:12:12 +0300 Subject: [PATCH] Added min/max rules support for strings (#433) --- src/Support/Generator/Types/ArrayType.php | 6 ++-- src/Support/Generator/Types/NumberType.php | 4 +-- src/Support/Generator/Types/StringType.php | 26 +++++++++++++++++ .../RulesExtractor/RulesMapper.php | 12 ++++++-- tests/ValidationRulesDocumentingTest.php | 28 +++++++++++++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/Support/Generator/Types/ArrayType.php b/src/Support/Generator/Types/ArrayType.php index be81dfa4..0f864e25 100644 --- a/src/Support/Generator/Types/ArrayType.php +++ b/src/Support/Generator/Types/ArrayType.php @@ -12,11 +12,11 @@ class ArrayType extends Type /** @var Type|Schema */ public $prefixItems = []; - private $minItems = null; + public $minItems = null; - private $maxItems = null; + public $maxItems = null; - private $additionalItems = null; + public $additionalItems = null; public function __construct() { diff --git a/src/Support/Generator/Types/NumberType.php b/src/Support/Generator/Types/NumberType.php index 4d560fe5..fd11bf56 100644 --- a/src/Support/Generator/Types/NumberType.php +++ b/src/Support/Generator/Types/NumberType.php @@ -4,9 +4,9 @@ class NumberType extends Type { - private $min = null; + public $min = null; - private $max = null; + public $max = null; public function __construct($type = 'number') { diff --git a/src/Support/Generator/Types/StringType.php b/src/Support/Generator/Types/StringType.php index ea326094..ee863ca3 100644 --- a/src/Support/Generator/Types/StringType.php +++ b/src/Support/Generator/Types/StringType.php @@ -4,8 +4,34 @@ class StringType extends Type { + public $min = null; + + public $max = null; + public function __construct() { parent::__construct('string'); } + + public function setMin($min) + { + $this->min = $min; + + return $this; + } + + public function setMax($max) + { + $this->max = $max; + + return $this; + } + + public function toArray() + { + return array_merge(parent::toArray(), array_filter([ + 'minLength' => $this->min, + 'maxLength' => $this->max, + ], fn ($v) => $v !== null)); + } } diff --git a/src/Support/OperationExtensions/RulesExtractor/RulesMapper.php b/src/Support/OperationExtensions/RulesExtractor/RulesMapper.php index 1d8269e4..ac47f824 100644 --- a/src/Support/OperationExtensions/RulesExtractor/RulesMapper.php +++ b/src/Support/OperationExtensions/RulesExtractor/RulesMapper.php @@ -115,7 +115,11 @@ public function required(Type $type) public function min(Type $type, $params) { - if ($type instanceof NumberType || $type instanceof ArrayType) { + if ( + $type instanceof NumberType + || $type instanceof ArrayType + || $type instanceof StringType + ) { $type->setMin((float) $params[0]); } @@ -124,7 +128,11 @@ public function min(Type $type, $params) public function max(Type $type, $params) { - if ($type instanceof NumberType || $type instanceof ArrayType) { + if ( + $type instanceof NumberType + || $type instanceof ArrayType + || $type instanceof StringType + ) { $type->setMax((float) $params[0]); } diff --git a/tests/ValidationRulesDocumentingTest.php b/tests/ValidationRulesDocumentingTest.php index c26b423b..6134761e 100644 --- a/tests/ValidationRulesDocumentingTest.php +++ b/tests/ValidationRulesDocumentingTest.php @@ -184,6 +184,34 @@ ->toHaveKey('maximum', 8); }); +it('converts min rule into "minLength" for string fields', function () { + $rules = [ + 'str' => ['string', 'min:8'], + ]; + + $params = app()->make(RulesToParameters::class, ['rules' => $rules])->handle(); + + expect($params = collect($params)->all()) + ->toHaveCount(1) + ->and($params[0]->schema->type) + ->toBeInstanceOf(\Dedoc\Scramble\Support\Generator\Types\StringType::class) + ->toHaveKey('minLength', 8); +}); + +it('converts max rule into "maxLength" for string fields', function () { + $rules = [ + 'str' => ['string', 'max:8'], + ]; + + $params = app()->make(RulesToParameters::class, ['rules' => $rules])->handle(); + + expect($params = collect($params)->all()) + ->toHaveCount(1) + ->and($params[0]->schema->type) + ->toBeInstanceOf(\Dedoc\Scramble\Support\Generator\Types\StringType::class) + ->toHaveKey('maxLength', 8); +}); + it('converts min rule into "minItems" for array fields', function () { $rules = [ 'num' => ['array', 'min:8'],