Skip to content

Commit

Permalink
Added min/max rules support for strings (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko authored Jun 28, 2024
1 parent 90f7661 commit 34bfad5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Support/Generator/Types/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Generator/Types/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
{
Expand Down
26 changes: 26 additions & 0 deletions src/Support/Generator/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
12 changes: 10 additions & 2 deletions src/Support/OperationExtensions/RulesExtractor/RulesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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]);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/ValidationRulesDocumentingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 34bfad5

Please sign in to comment.